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>
82 static int lfs_find(int argc, char **argv);
83 static int lfs_getstripe(int argc, char **argv);
84 static int lfs_getdirstripe(int argc, char **argv);
85 static int lfs_setdirstripe(int argc, char **argv);
86 static int lfs_rmentry(int argc, char **argv);
87 static int lfs_unlink_foreign(int argc, char **argv);
88 static int lfs_osts(int argc, char **argv);
89 static int lfs_mdts(int argc, char **argv);
90 static int lfs_df(int argc, char **argv);
91 static int lfs_getname(int argc, char **argv);
92 static int lfs_check(int argc, char **argv);
93 #ifdef HAVE_SYS_QUOTA_H
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_project(int argc, char **argv);
98 static int lfs_flushctx(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_changelog(int argc, char **argv);
101 static int lfs_changelog_clear(int argc, char **argv);
102 static int lfs_fid2path(int argc, char **argv);
103 static int lfs_path2fid(int argc, char **argv);
104 static int lfs_rmfid(int argc, char **argv);
105 static int lfs_data_version(int argc, char **argv);
106 static int lfs_hsm_state(int argc, char **argv);
107 static int lfs_hsm_set(int argc, char **argv);
108 static int lfs_hsm_clear(int argc, char **argv);
109 static int lfs_hsm_action(int argc, char **argv);
110 static int lfs_hsm_archive(int argc, char **argv);
111 static int lfs_hsm_restore(int argc, char **argv);
112 static int lfs_hsm_release(int argc, char **argv);
113 static int lfs_hsm_remove(int argc, char **argv);
114 static int lfs_hsm_cancel(int argc, char **argv);
115 static int lfs_swap_layouts(int argc, char **argv);
116 static int lfs_mv(int argc, char **argv);
117 static int lfs_ladvise(int argc, char **argv);
118 static int lfs_getsom(int argc, char **argv);
119 static int lfs_heat_get(int argc, char **argv);
120 static int lfs_heat_set(int argc, char **argv);
121 static int lfs_mirror(int argc, char **argv);
122 static int lfs_mirror_list_commands(int argc, char **argv);
123 static int lfs_list_commands(int argc, char **argv);
124 static inline int lfs_mirror_resync(int argc, char **argv);
125 static inline int lfs_mirror_verify(int argc, char **argv);
126 static inline int lfs_mirror_read(int argc, char **argv);
127 static inline int lfs_mirror_write(int argc, char **argv);
128 static inline int lfs_mirror_copy(int argc, char **argv);
129 static int lfs_pcc_attach(int argc, char **argv);
130 static int lfs_pcc_attach_fid(int argc, char **argv);
131 static int lfs_pcc_detach(int argc, char **argv);
132 static int lfs_pcc_detach_fid(int argc, char **argv);
133 static int lfs_pcc_state(int argc, char **argv);
134 static int lfs_pcc(int argc, char **argv);
135 static int lfs_pcc_list_commands(int argc, char **argv);
136 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
137 __u64 migration_flags);
139 struct pool_to_id_cbdata {
143 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
144 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
146 enum setstripe_origin {
156 static int lfs_setstripe_internal(int argc, char **argv,
157 enum setstripe_origin opc);
159 static inline int lfs_setstripe(int argc, char **argv)
161 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
164 static inline int lfs_setstripe_migrate(int argc, char **argv)
166 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
169 static inline int lfs_mirror_create(int argc, char **argv)
171 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
174 static inline int lfs_mirror_extend(int argc, char **argv)
176 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
179 static inline int lfs_mirror_split(int argc, char **argv)
181 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
184 static inline int lfs_mirror_delete(int argc, char **argv)
186 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
189 /* Setstripe and migrate share mostly the same parameters */
190 #define SSM_CMD_COMMON(cmd) \
191 "usage: "cmd" [--component-end|-E COMP_END]\n" \
192 " [--copy=LUSTRE_SRC]\n" \
193 " [--extension-size|--ext-size|-z SIZE]\n" \
194 " [--help|-h] [--layout|-L PATTERN]\n" \
195 " [--layout|-L PATTERN]\n" \
196 " [--mirror-count|-N[MIRROR_COUNT]]\n" \
197 " [--ost|-o OST_INDICES]\n" \
198 " [--overstripe-count|-C STRIPE_COUNT]\n" \
199 " [--pool|-p POOL_NAME]\n" \
200 " [--stripe-count|-c STRIPE_COUNT]\n" \
201 " [--stripe-index|-i START_OST_IDX]\n" \
202 " [--stripe-size|-S STRIPE_SIZE]\n" \
203 " [--yaml|-y YAML_TEMPLATE_FILE]\n"
205 #define MIRROR_EXTEND_USAGE \
206 " {--mirror-count|-N[MIRROR_COUNT]}\n" \
207 " [SETSTRIPE_OPTIONS|-f|--file VICTIM_FILE]\n" \
210 #define SETSTRIPE_USAGE \
211 SSM_CMD_COMMON("setstripe") \
212 MIRROR_EXTEND_USAGE \
213 " DIRECTORY|FILENAME\n"
215 #define MIGRATE_USAGE \
216 SSM_CMD_COMMON("migrate ") \
217 " [--block|-b] [--non-block|-n]\n" \
218 " [--non-direct|-D] [--verbose|-v]\n" \
221 #define SETDIRSTRIPE_USAGE \
222 " [--mdt-count|-c stripe_count>\n" \
223 " [--help|-h] [--mdt-hash|-H mdt_hash]\n" \
224 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
225 " [--default|-D] [--mode|-o mode]\n" \
226 " [--max-inherit|-X max_inherit]\n" \
227 " [--max-inherit-rr max_inherit_rr] <dir>\n" \
228 "To create dir with a foreign (free format) layout :\n" \
229 "setdirstripe|mkdir --foreign[=FOREIGN_TYPE] -x|-xattr STRING " \
230 " [--mode|-o MODE] [--flags HEX] DIRECTORY\n"
233 * command_t mirror_cmdlist - lfs mirror commands.
235 command_t mirror_cmdlist[] = {
236 { .pc_name = "create", .pc_func = lfs_mirror_create,
237 .pc_help = "Create a mirrored file.\n"
238 "usage: lfs mirror create --mirror-count|-N[MIRROR_COUNT]\n"
239 " [SETSTRIPE_OPTIONS] ... FILENAME|DIRECTORY ...\n" },
240 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
241 .pc_help = "Delete a mirror from a file.\n"
242 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
243 "\t --component-id|--comp-id|-I COMP_ID |\n"
244 "\t -p <pool>} MIRRORED_FILE ...\n"
246 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
247 .pc_help = "Extend a mirrored file.\n"
248 "usage: lfs mirror extend "
249 "{--mirror-count|-N[MIRROR_COUNT]} [--no-verify] "
250 "[SETSTRIPE_OPTIONS|-f VICTIM_FILE] ... FILENAME ...\n" },
251 { .pc_name = "split", .pc_func = lfs_mirror_split,
252 .pc_help = "Split a mirrored file.\n"
253 "usage: lfs mirror split {--mirror-id MIRROR_ID |\n"
254 "\t --component-id|-I COMP_ID|-p POOL} [--destroy|-d]\n"
255 "\t [-f NEW_FILE] MIRRORED_FILE ...\n" },
256 { .pc_name = "read", .pc_func = lfs_mirror_read,
257 .pc_help = "Read the content of a specified mirror of a file.\n"
258 "usage: lfs mirror read {--mirror-id|-N MIRROR_ID}\n"
259 "\t\t[--outfile|-o <output_file>] <mirrored_file>\n" },
260 { .pc_name = "write", .pc_func = lfs_mirror_write,
261 .pc_help = "Write to a specified mirror of a file.\n"
262 "usage: lfs mirror write {--mirror-id|-N MIRROR_ID}\n"
263 "\t\t[--inputfile|-i <input_file>] <mirrored_file>\n" },
264 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
265 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
266 "usage: lfs mirror copy {--read-mirror|-i MIRROR_ID0}\n"
267 "\t\t{--write-mirror|-o MIRROR_ID1[,...]} <mirrored_file>\n" },
268 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
269 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
270 "usage: lfs mirror resync [--only MIRROR_ID[,...]>]\n"
271 "\t\t<mirrored_file> [<mirrored_file2>...]\n" },
272 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
273 .pc_help = "Verify mirrored file(s).\n"
274 "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
275 "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
276 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
277 .pc_help = "list commands supported by lfs mirror"},
278 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
279 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
280 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
285 * command_t pcc_cmdlist - lfs pcc commands.
287 command_t pcc_cmdlist[] = {
288 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
289 .pc_help = "Attach given files to the Persistent Client Cache.\n"
290 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
291 "\t-i: archive id for RW-PCC\n" },
292 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
293 .pc_help = "Attach given files into PCC by FID(s).\n"
294 "usage: lfs pcc attach_id {--id|-i NUM} {--mnt|-m MOUNTPOINT} FID ...\n"
295 "\t-i: archive id for RW-PCC\n"
296 "\t-m: Lustre mount point\n" },
297 { .pc_name = "state", .pc_func = lfs_pcc_state,
298 .pc_help = "Display the PCC state for given files.\n"
299 "usage: lfs pcc state <file> ...\n" },
300 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
301 .pc_help = "Detach given files from the Persistent Client Cache.\n"
302 "usage: lfs pcc detach <file> ...\n" },
303 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
304 .pc_help = "Detach given files from PCC by FID(s).\n"
305 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
306 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
307 .pc_help = "list commands supported by lfs pcc"},
308 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
309 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
310 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
314 /* all available commands */
315 command_t cmdlist[] = {
316 {"setstripe", lfs_setstripe, 0,
317 "To create a file with specified striping/composite layout, or\n"
318 "create/replace the default layout on an existing directory:\n"
319 SSM_CMD_COMMON("setstripe")
321 " <directory|filename>\n"
323 "To add component(s) to an existing composite file:\n"
324 SSM_CMD_COMMON("setstripe --component-add")
325 "To totally delete the default striping from an existing directory:\n"
326 "usage: setstripe [--delete|-d] <directory>\n"
328 "To create a mirrored file or set s default mirror layout on a directory:\n"
329 "usage: setstripe {--mirror-count|-N}[MIRROR_COUNT] [SETSTRIPE_OPTIONS] <directory|filename>\n"
331 "To delete the last component(s) from an existing composite file\n"
332 "(note that this will also delete any data in those components):\n"
333 "usage: setstripe --component-del [--component-id|-I COMP_ID]\n"
334 " [--component-flags|-F COMP_FLAGS]\n"
336 "\tCOMP_ID: Unique component ID to delete\n"
337 "\tCOMP_FLAGS: 'init' indicating all instantiated components\n"
338 "\t '^init' indicating all uninstantiated components\n"
339 "\t-I and -F cannot be specified at the same time\n"
341 "To set or clear flags on a specific component\n"
342 "(note that this command can only be applied to mirrored files:\n"
343 "usage: setstripe --comp-set {-I COMP_ID|--comp-flags=COMP_FLAGS}\n"
346 "To create a file with a foreign (free format) layout:\n"
347 "usage: setstripe --foreign[=FOREIGN_TYPE]\n"
348 " --xattr|-x LAYOUT_STRING [--flags HEX]\n"
349 " [--mode MODE] <filename>\n"},
350 {"getstripe", lfs_getstripe, 0,
351 "To list the layout pattern for a given file or files in a\n"
352 "directory or recursively for all files in a directory tree.\n"
353 "usage: getstripe [--ost|-O UUID] [--quiet|-q] [--verbose|-v]\n"
354 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
355 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
356 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
357 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
358 " [--help|-h] [--component-id|-I[=COMP_ID]]\n"
359 " [--component-flags[=COMP_FLAGS]]\n"
360 " [--component-count]\n"
361 " [--extension-size|--ext-size|-z]\n"
362 " [--component-start[=[+-]COMP_START]]\n"
363 " [--component-end[=[+-]COMP_END]|-E[[+-]comp_end]]\n"
364 " [[!] --mirror-index=[+-]INDEX |\n"
365 " [!] --mirror-id=[+-]MIRROR_ID] [--mirror-count|-N]\n"
367 " <directory|filename> ..."},
368 {"setdirstripe", lfs_setdirstripe, 0,
369 "Create striped directory on specified MDT, same as mkdir.\n"
370 "May be restricted to root or group users, depending on settings.\n"
371 "usage: setdirstripe [OPTION] <directory>\n"
373 {"getdirstripe", lfs_getdirstripe, 0,
374 "To list the layout pattern info for a given directory\n"
375 "or recursively for all directories in a directory tree.\n"
376 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
377 " [--help|-h] [--mdt-hash|-H] [--obd|-O UUID]\n"
378 " [--recursive|-r] [--yaml|-y]\n"
379 " [--verbose|-v] [--default|-D]\n"
380 " [--max-inherit|-X]\n"
381 " [--max-inherit-rr] <dir> ..."},
382 {"mkdir", lfs_setdirstripe, 0,
383 "Create striped directory on specified MDT, same as setdirstripe.\n"
384 "usage: mkdir [OPTION] <directory>\n"
386 {"rm_entry", lfs_rmentry, 0,
387 "To remove the name entry of the remote directory. Note: This\n"
388 "command will only delete the name entry, i.e. the remote directory\n"
389 "will become inaccessable after this command. This can only be done\n"
390 "by the administrator\n"
391 "usage: rm_entry <dir>\n"},
392 {"rmentry", lfs_rmentry, 0, "remove a dir entry, same as 'rm_entry'\n"},
393 {"unlink_foreign", lfs_unlink_foreign, 0,
394 "To remove the foreign file/dir.\n"
395 "Note: This is for files/dirs prevented to be removed using\n"
396 "unlink/rmdir, but works also for regular ones\n"
397 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
398 {"pool_list", lfs_poollist, 0,
399 "List pools or pool OSTs\n"
400 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
401 {"find", lfs_find, 0,
402 "find files matching given attributes recursively in directory tree.\n"
403 "usage: find <directory|filename> ...\n"
404 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --btime|-B [+-]N[smhdwy]]\n"
405 " [[!] --ctime|-C [+-]N[smhdwy]] [[!] --mtime|-M [+-]N[smhdwy]]\n"
406 " [[!] --blocks|-b N] [[!] --component-count [+-]<comp_cnt>]\n"
407 " [[!] --component-start [+-]N[kMGTPE]]\n"
408 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
409 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
410 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
411 " [[!] --foreign[=<foreign_type>]]\n"
412 " [[!] --gid|-g|--group|-G <gid>|<gname>] [--help|-h]\n"
413 " [[!] --layout|-L released,raid0,mdt] [--lazy]\n"
414 " [--maxdepth|-D N] [[!] --mdt-count|-T [+-]<stripes>]\n"
415 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
416 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
417 " [[!] --mirror-count|-N [+-]<n>]\n"
418 " [[!] --mirror-state <[^]state>]\n"
419 " [[!] --name|-n <pattern>] [[!] --newer[XY] <reference>]\n"
420 " [[!] --ost|-O <uuid|index,...>] [[!] --perm [/-]mode]\n"
421 " [[!] --pool <pool>] [--print|-P] [--print0|-0] [--printf <format>]\n"
422 " [[!] --projid <projid>] [[!] --size|-s [+-]N[bkMGTPE]]\n"
423 " [[!] --stripe-count|-c [+-]<stripes>]\n"
424 " [[!] --stripe-index|-i <index,...>]\n"
425 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
426 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
427 "\t !: used before an option indicates 'NOT' requested attribute\n"
428 "\t -: used before a value indicates less than requested value\n"
429 "\t +: used before a value indicates more than requested value\n"
430 "\t ^: used before a flag indicates to exclude it\n"},
431 {"check", lfs_check, 0,
432 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
433 "or all the servers (MGTs, MDTs and OSTs) [for specified path only].\n"
434 "usage: check {mgts|osts|mdts|all} [path]"},
435 {"osts", lfs_osts, 0, "list OSTs connected to client "
436 "[for specified path only]\n" "usage: osts [path]"},
437 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
438 "[for specified path only]\n" "usage: mdts [path]"},
440 "report filesystem disk space usage or inodes usage "
441 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
442 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
443 " [--pool|-p <fsname>[.<pool>]] [path]"},
444 {"getname", lfs_getname, 0,
445 "list instances and specified mount points [for specified path only]\n"
446 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
447 #ifdef HAVE_SYS_QUOTA_H
448 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
449 "usage: setquota [-t][-D] {-u|-U|-g|-G|-p|-P} {-b|-B|-i|-I LIMIT} [--pool POOL] FILESYSTEM\n"
450 " setquota {-u|-g|-p} --delete FILESYSTEM\n"},
451 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
452 "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
453 " [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID]\n"
454 " [--pool <OST pool name>] <filesystem>\n"
455 " quota -t <-u|-g|-p> [--pool <OST pool name>] <filesystem>\n"
456 " quota [-q] [-v] [h] {-U|-G|-P} [--pool <OST pool name>] <filesystem>"},
457 {"project", lfs_project, 0,
458 "Change or list project attribute for specified file or directory.\n"
459 "usage: project [-d|-r] <file|directory...>\n"
460 " list project ID and flags on file(s) or directories\n"
461 " project [-p id] [-s] [-r] <file|directory...>\n"
462 " set project ID and/or inherit flag for specified file(s) or directories\n"
463 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
464 " check project ID and flags on file(s) or directories, print outliers\n"
465 " project -C [-d|-r] [-k] <file|directory...>\n"
466 " clear the project inherit flag and ID on the file or directory\n"
469 {"flushctx", lfs_flushctx, 0,
470 "Flush security context for current user.\n"
471 "usage: flushctx [-k] [-r] [mountpoint...]"},
472 {"changelog", lfs_changelog, 0,
473 "Show the metadata changes on an MDT."
474 "\nusage: changelog <mdtname> [startrec [endrec]]"},
475 {"changelog_clear", lfs_changelog_clear, 0,
476 "Indicate that old changelog records up to <endrec> are no longer of "
477 "interest to consumer <id>, allowing the system to free up space.\n"
478 "An <endrec> of 0 means all records.\n"
479 "usage: changelog_clear <mdtname> <id> <endrec>"},
480 {"fid2path", lfs_fid2path, 0,
481 "Resolve the full path(s) for given FID(s). For a specific hardlink "
482 "specify link number <linkno>.\n"
483 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
484 "<fsname|root> <fid>..."},
485 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
486 "usage: path2fid [--parents] <path> ..."},
487 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
488 "usage: rmfid <fsname|rootpath> <fid> ..."},
489 {"data_version", lfs_data_version, 0, "Display file data version for "
490 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
491 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
492 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
493 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
494 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
495 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
496 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
498 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
499 "[--archived] [--lost] <file> ..."},
500 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
501 "given files.\n" "usage: hsm_action <file> ..."},
502 {"hsm_archive", lfs_hsm_archive, 0,
503 "Archive file to external storage.\n"
504 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
506 {"hsm_restore", lfs_hsm_restore, 0,
507 "Restore file from external storage.\n"
508 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
509 {"hsm_release", lfs_hsm_release, 0,
510 "Release files from Lustre.\n"
511 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
512 {"hsm_remove", lfs_hsm_remove, 0,
513 "Remove file copy from external storage.\n"
514 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
516 " (FILE [FILE ...] | "
517 "--mntpath MOUNTPATH FID [FID ...])\n"
519 "Note: To remove an archived copy of a file already deleted from a "
521 "--mntpath option and a list of FIDs must be specified"
523 {"hsm_cancel", lfs_hsm_cancel, 0,
524 "Cancel requests related to specified files.\n"
525 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
526 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
527 "usage: swap_layouts <path1> <path2>"},
528 {"migrate", lfs_setstripe_migrate, 0,
529 "migrate directories and their inodes between MDTs.\n"
530 "usage: migrate [--mdt-count|-c STRIPE_COUNT] [--directory|-d]\n"
531 " [--mdt-hash|-H HASH_TYPE]\n"
532 " [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
535 "migrate file objects from one OST layout to another\n"
536 "(may be not safe with concurent writes).\n"
539 "To move directories between MDTs. This command is deprecated, "
540 "use \"migrate\" instead.\n"
541 "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
543 {"ladvise", lfs_ladvise, 0,
544 "Provide servers with advice about access patterns for a file.\n"
545 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
546 " [--background|-b] [--unset|-u]\n\n"
547 " {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
548 " {[--mode|-m [READ,WRITE]}\n"
550 {"mirror", lfs_mirror, mirror_cmdlist,
551 "lfs commands used to manage files with mirrored components:\n"
552 "lfs mirror create - create a mirrored file or directory\n"
553 "lfs mirror extend - add mirror(s) to an existing file\n"
554 "lfs mirror split - split a mirror from an existing mirrored file\n"
555 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
556 "lfs mirror read - read a mirror content of a mirrored file\n"
557 "lfs mirror write - write to a mirror of a mirrored file\n"
558 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
559 "lfs mirror verify - verify mirrored file(s)\n"},
560 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
561 "usage: getsom [-s] [-b] [-f] <path>\n"
562 "\t-s: Only show the size value of the SOM data for a given file\n"
563 "\t-b: Only show the blocks value of the SOM data for a given file\n"
564 "\t-f: Only show the flags value of the SOM data for a given file\n"},
565 {"heat_get", lfs_heat_get, 0,
566 "To get heat of files.\n"
567 "usage: heat_get <file> ...\n"},
568 {"heat_set", lfs_heat_set, 0,
569 "To set heat flags of files.\n"
570 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
571 "\t--clear|-c: Clear file heat for given files\n"
572 "\t--off|-o: Turn off file heat for given files\n"
573 "\t--on|-O: Turn on file heat for given files\n"},
574 {"pcc", lfs_pcc, pcc_cmdlist,
575 "lfs commands used to interact with PCC features:\n"
576 "lfs pcc attach - attach given files to Persistent Client Cache\n"
577 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
578 "lfs pcc state - display the PCC state for given files\n"
579 "lfs pcc detach - detach given files from Persistent Client Cache\n"
580 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
581 {"help", Parser_help, 0, "help"},
582 {"exit", Parser_quit, 0, "quit"},
583 {"quit", Parser_quit, 0, "quit"},
584 {"--version", Parser_version, 0,
585 "output build version of the utility and exit"},
586 {"--list-commands", lfs_list_commands, 0,
587 "list commands supported by the utility and exit"},
591 static int check_hashtype(const char *hashtype)
593 int type_num = atoi(hashtype);
596 /* numeric hash type */
597 if (hashtype && lmv_is_known_hash_type(type_num))
599 /* string hash type */
600 for (i = LMV_HASH_TYPE_ALL_CHARS; i < ARRAY_SIZE(mdt_hash_name); i++)
601 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
607 static uint32_t check_foreign_type_name(const char *foreign_type_name)
611 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
612 if (!lu_foreign_types[i].lft_name)
614 if (strcmp(foreign_type_name,
615 lu_foreign_types[i].lft_name) == 0)
616 return lu_foreign_types[i].lft_type;
619 return LU_FOREIGN_TYPE_UNKNOWN;
622 static const char *error_loc = "syserror";
625 migrate_open_files(const char *name, __u64 migration_flags,
626 const struct llapi_stripe_param *param,
627 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
634 char parent[PATH_MAX];
635 char volatile_file[PATH_MAX];
641 if (!param && !layout) {
642 error_loc = "layout information";
646 /* search for file directory pathname */
647 if (strlen(name) > sizeof(parent) - 1) {
648 error_loc = "source file name";
652 strncpy(parent, name, sizeof(parent));
653 ptr = strrchr(parent, '/');
655 if (!getcwd(parent, sizeof(parent))) {
656 error_loc = "getcwd";
660 if (ptr == parent) /* leading '/' */
665 /* even if the file is only read, WR mode is nedeed to allow
668 /* Allow migrating even without the key on encrypted files */
669 rflags = O_RDWR | O_NOATIME | O_FILE_ENC;
670 if (!(migration_flags & LLAPI_MIGRATION_NONDIRECT))
673 fd = open(name, rflags);
675 /* If encrypted file without the key,
676 * retry mirror extend in O_DIRECT.
678 if (errno == ENOKEY && !(rflags & O_DIRECT) &&
679 migration_flags & LLAPI_MIGRATION_MIRROR) {
684 error_loc = "cannot open source file";
688 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
690 error_loc = "cannot get MDT index";
695 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW |
696 /* Allow migrating without the key on encrypted files */
698 mode_t open_mode = S_IRUSR | S_IWUSR;
700 if (rflags & O_DIRECT)
701 open_flags |= O_DIRECT;
702 random_value = random();
703 rc = snprintf(volatile_file, sizeof(volatile_file),
704 "%s/%s:%.4X:%.4X:fd=%.2d", parent,
705 LUSTRE_VOLATILE_HDR, mdt_index,
707 if (rc >= sizeof(volatile_file)) {
712 /* create, open a volatile file, use caching (ie no directio) */
714 /* Returns -1 and sets errno on error: */
715 fdv = llapi_layout_file_open(volatile_file, open_flags,
720 /* Does the right thing on error: */
721 fdv = llapi_file_open_param(volatile_file, open_flags,
724 } while (fdv < 0 && (rc = fdv) == -EEXIST);
727 error_loc = "cannot create volatile file";
732 * In case the MDT does not support creation of volatile files
733 * we should try to unlink it.
735 (void)unlink(volatile_file);
738 * Not-owner (root?) special case.
739 * Need to set owner/group of volatile file like original.
740 * This will allow to pass related check during layout_swap.
745 error_loc = "cannot stat source file";
749 rc = fstat(fdv, &stv);
752 error_loc = "cannot stat volatile";
756 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
757 rc = fchown(fdv, st.st_uid, st.st_gid);
760 error_loc = "cannot change ownwership of volatile";
779 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
781 struct llapi_layout *layout;
782 size_t buf_size = 4 * 1024 * 1024;
786 size_t page_size = sysconf(_SC_PAGESIZE);
790 layout = llapi_layout_get_by_fd(fd_src, 0);
792 uint64_t stripe_size;
794 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
796 buf_size = stripe_size;
798 llapi_layout_free(layout);
801 /* Use a page-aligned buffer for direct I/O */
802 rc = posix_memalign(&buf, page_size, buf_size);
806 sparse = llapi_file_is_sparse(fd_src);
808 rc = ftruncate(fd_dst, pos);
817 size_t to_read, to_write;
820 if (sparse && pos >= data_end) {
823 data_off = llapi_data_seek(fd_src, pos, &data_size);
825 /* Non-fatal, switch to full copy */
829 /* hole at the end of file, truncate up to it */
831 rc = ftruncate(fd_dst, data_off);
835 pos = data_off & ~(page_size - 1);
836 data_end = data_off + data_size;
837 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
838 to_read = MIN(to_read, buf_size);
844 rc = check_file(fd_src);
849 rsize = pread(fd_src, buf, to_read, pos);
859 while (to_write > 0) {
862 written = pwrite(fd_dst, buf, to_write, pos);
870 if (rc || rsize < to_read)
878 /* Try to avoid page cache pollution after migration. */
879 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
880 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
886 static int migrate_set_timestamps(int fd, const struct stat *st)
888 struct timeval tv[2] = {
889 {.tv_sec = st->st_atime},
890 {.tv_sec = st->st_mtime}
893 return futimes(fd, tv);
896 static int migrate_block(int fd, int fdv)
906 error_loc = "cannot stat source file";
910 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
912 error_loc = "cannot get dataversion";
921 * The grouplock blocks all concurrent accesses to the file.
922 * It has to be taken after llapi_get_data_version as it would
925 rc = llapi_group_lock(fd, gid);
927 error_loc = "cannot get group lock";
931 rc = migrate_copy_data(fd, fdv, NULL);
933 error_loc = "data copy failed";
937 /* Make sure we keep original atime/mtime values */
938 rc = migrate_set_timestamps(fdv, &st);
940 error_loc = "set target file timestamp failed";
946 * for a migration we need to check data version on file did
949 * Pass in gid=0 since we already own grouplock.
951 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
952 SWAP_LAYOUTS_CHECK_DV1);
954 error_loc = "file changed";
957 error_loc = "cannot swap layout";
962 rc2 = llapi_group_unlock(fd, gid);
963 if (rc2 < 0 && rc == 0) {
964 error_loc = "unlock group lock";
972 * Internal helper for migrate_copy_data(). Check lease and report error if
975 * \param[in] fd File descriptor on which to check the lease.
977 * \retval 0 Migration can keep on going.
978 * \retval -errno Error occurred, abort migration.
980 static int check_lease(int fd)
984 rc = llapi_lease_check(fd);
986 return 0; /* llapi_check_lease returns > 0 on success. */
991 static int migrate_nonblock(int fd, int fdv)
1000 error_loc = "cannot stat source file";
1004 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1006 error_loc = "cannot get data version";
1010 rc = migrate_copy_data(fd, fdv, check_lease);
1012 error_loc = "data copy failed";
1016 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1018 error_loc = "cannot get data version";
1024 error_loc = "source file changed";
1028 /* Make sure we keep original atime/mtime values */
1029 rc = migrate_set_timestamps(fdv, &st);
1031 error_loc = "set target file timestamp failed";
1038 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1040 struct pool_to_id_cbdata data = { .pool = pool };
1041 struct llapi_layout *layout = NULL;
1044 layout = llapi_layout_get_by_path(fname, 0);
1047 "error %s: file '%s' couldn't get layout: rc=%d\n",
1048 progname, fname, errno);
1052 rc = llapi_layout_sanity(layout, false, true);
1054 llapi_layout_sanity_perror(errno);
1057 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1066 llapi_layout_free(layout);
1070 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1071 __u32 flags, __u32 neg_flags)
1074 __u32 flags_array[2];
1081 "error %s: neither component id nor pool is specified\n",
1085 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1091 ids[count] = comp_id;
1092 flags_array[count] = flags;
1097 if (neg_flags & LCME_FL_STALE) {
1099 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1104 ids[count] = comp_id;
1105 flags_array[count] = neg_flags | LCME_FL_NEG;
1109 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1111 if (errno == EUCLEAN) {
1114 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1115 progname, comp_id, fname);
1118 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1119 progname, comp_id, fname, flags, neg_flags);
1126 static int lfs_component_del(char *fname, __u32 comp_id,
1127 __u32 flags, __u32 neg_flags)
1131 if (flags && neg_flags) {
1133 "%s: cannot specify both positive and negative flags\n",
1138 if (!flags && neg_flags)
1139 flags = neg_flags | LCME_FL_NEG;
1141 if (flags && comp_id) {
1143 "%s: cannot specify component ID and flags at the same time\n",
1148 if (!flags && !comp_id) {
1150 "%s: neither flags nor component ID is specified\n",
1156 if (flags & ~LCME_KNOWN_FLAGS) {
1158 "%s setstripe: unknown flags %#x\n",
1162 } else if (comp_id > LCME_ID_MAX) {
1163 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1168 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1171 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1172 progname, comp_id, fname, strerror(errno));
1176 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1183 rc = llapi_layout_file_comp_add(fname, layout);
1185 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1186 fname, strerror(errno));
1190 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1191 struct llapi_layout *layout)
1199 fd = lstat(fname, &st);
1200 if (fd == 0 && S_ISDIR(st.st_mode))
1201 open_flags = O_DIRECTORY | O_RDONLY;
1203 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1205 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1206 S_ISDIR(st.st_mode) ?
1207 "set default composite layout for" :
1208 "create composite file",
1209 fname, strerror(errno));
1213 static int lfs_migrate(char *name, __u64 migration_flags,
1214 struct llapi_stripe_param *param,
1215 struct llapi_layout *layout)
1217 struct llapi_layout *existing;
1218 uint64_t dom_new, dom_cur;
1223 rc = migrate_open_files(name, migration_flags, param, layout,
1228 rc = llapi_layout_dom_size(layout, &dom_new);
1230 error_loc = "cannot get new layout DoM size";
1233 /* special case for migration to DOM layout*/
1234 existing = llapi_layout_get_by_fd(fd, 0);
1236 error_loc = "cannot get existing layout";
1240 rc = llapi_layout_dom_size(existing, &dom_cur);
1242 error_loc = "cannot get current layout DoM size";
1247 * if file has DoM layout already then migration is possible to
1248 * the new layout with the same DoM component via swap layout,
1249 * if new layout used bigger DOM size, then mirroring is used
1251 if (dom_new > dom_cur) {
1252 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags);
1254 error_loc = "cannot migrate to DOM layout";
1258 if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1260 * Blocking mode (forced if servers do not support file lease).
1261 * It is also the default mode, since we cannot distinguish
1262 * between a broken lease and a server that does not support
1263 * atomic swap/close (LU-6785)
1265 rc = migrate_block(fd, fdv);
1269 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1271 error_loc = "cannot get lease";
1275 rc = migrate_nonblock(fd, fdv);
1277 llapi_lease_release(fd);
1282 * Atomically put lease, swap layouts and close.
1283 * for a migration we need to check data version on file did
1286 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1288 error_loc = "cannot swap layout";
1300 fprintf(stderr, "error: %s: %s: %s: %s\n",
1301 progname, name, error_loc, strerror(-rc));
1302 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1303 printf("%s\n", name);
1308 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1311 char *dup_string = NULL;
1317 if (!string || !string[0])
1320 dup_string = strdup(string);
1322 llapi_printf(LLAPI_MSG_ERROR,
1323 "%s: insufficient memory\n",
1328 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1332 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1333 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1334 const char *comp_name = comp_flags_table[i].cfn_name;
1336 if (strcmp(name, comp_name) == 0) {
1337 *flags |= comp_flag;
1339 } else if (strncmp(name, "^", 1) == 0 &&
1340 strcmp(name + 1, comp_name) == 0) {
1341 *neg_flags |= comp_flag;
1346 llapi_printf(LLAPI_MSG_ERROR,
1347 "%s: component flag '%s' not supported\n",
1354 if (!*flags && !*neg_flags)
1357 /* don't allow to set and exclude the same flag */
1358 if (*flags & *neg_flags)
1366 static int mdthash_input(char *string, __u32 *inflags,
1367 __u32 *exflags, __u32 *type)
1374 {"migrating", LMV_HASH_FLAG_MIGRATION},
1375 {"bad_type", LMV_HASH_FLAG_BAD_TYPE},
1376 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1377 {"lost_lmv", LMV_HASH_FLAG_LOST_LMV},
1378 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1387 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1391 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1392 if (strcmp(name, mhflist[i].name) == 0 ||
1393 name[0] == mhflist[i].name[0]) {
1394 *inflags |= mhflist[i].flag;
1396 } else if (name[0] == '^' &&
1397 (strcmp(name + 1, mhflist[i].name) == 0 ||
1398 name[1] == mhflist[i].name[0])) {
1399 *exflags |= mhflist[i].flag;
1404 i = check_hashtype(name);
1409 llapi_printf(LLAPI_MSG_ERROR,
1410 "%s: invalid mdt_hash value '%s'\n",
1416 /* don't allow to include and exclude the same flag */
1417 if (*inflags & *exflags) {
1418 llapi_printf(LLAPI_MSG_ERROR,
1419 "%s: include and exclude same flag '%s'\n",
1427 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1435 if (strncmp(string, "^", 1) == 0) {
1436 *neg_state = llapi_layout_string_flags(string + 1);
1437 if (*neg_state != 0)
1440 *state = llapi_layout_string_flags(string);
1445 llapi_printf(LLAPI_MSG_ERROR,
1446 "%s: mirrored file state '%s' not supported\n",
1452 * struct mirror_args - Command-line arguments for mirror(s).
1453 * @m_count: Number of mirrors to be created with this layout.
1454 * @m_flags: Mirror level flags, only 'prefer' is supported.
1455 * @m_layout: Mirror layout.
1456 * @m_file: A victim file. Its layout will be split and used as a mirror.
1457 * @m_next: Point to the next node of the list.
1459 * Command-line arguments for mirror(s) will be parsed and stored in
1460 * a linked list that consists of this structure.
1462 struct mirror_args {
1465 struct llapi_layout *m_layout;
1467 struct mirror_args *m_next;
1472 * enum mirror_flags - Flags for extending a mirrored file.
1473 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1474 * in case the victim file(s) contains the same data as the
1475 * original mirrored file.
1476 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1477 * @MF_COMP_ID: specified component id instead of mirror id
1479 * Flags for extending a mirrored file.
1489 * mirror_create_sanity_check() - Check mirror list.
1490 * @list: A linked list that stores the mirror arguments.
1492 * This function does a sanity check on @list for creating
1495 * Return: 0 on success or a negative error code on failure.
1497 static int mirror_create_sanity_check(const char *fname,
1498 struct mirror_args *list,
1502 bool has_m_file = false;
1503 bool has_m_layout = false;
1508 if (fname && check_fname) {
1509 struct llapi_layout *layout;
1511 layout = llapi_layout_get_by_path(fname, 0);
1514 "error: %s: file '%s' couldn't get layout\n",
1519 rc = llapi_layout_sanity(layout, false, true);
1521 llapi_layout_free(layout);
1524 llapi_layout_sanity_perror(rc);
1532 llapi_layout_free(list->m_layout);
1535 llapi_layout_get_by_path(list->m_file, 0);
1536 if (!list->m_layout) {
1538 "error: %s: file '%s' has no layout\n",
1539 progname, list->m_file);
1543 has_m_layout = true;
1544 if (!list->m_layout) {
1545 fprintf(stderr, "error: %s: no mirror layout\n",
1551 rc = llapi_layout_sanity(list->m_layout, false, true);
1553 llapi_layout_sanity_perror(rc);
1557 list = list->m_next;
1560 if (has_m_file && has_m_layout) {
1562 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1570 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1572 __u32 mirror_flags = *(__u32 *)cbdata;
1576 rc = llapi_layout_comp_flags_get(layout, &flags);
1581 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1586 return LLAPI_LAYOUT_ITER_CONT;
1590 * mirror_create() - Create a mirrored file.
1591 * @fname: The file to be created.
1592 * @mirror_list: A linked list that stores the mirror arguments.
1594 * This function creates a mirrored file @fname with the mirror(s)
1595 * from @mirror_list.
1597 * Return: 0 on success or a negative error code on failure.
1599 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1601 struct llapi_layout *layout = NULL;
1602 struct mirror_args *cur_mirror = NULL;
1603 uint16_t mirror_count = 0;
1607 rc = mirror_create_sanity_check(fname, mirror_list, false);
1611 cur_mirror = mirror_list;
1612 while (cur_mirror) {
1613 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1615 &cur_mirror->m_flags);
1618 fprintf(stderr, "%s: failed to set mirror flags\n",
1623 for (i = 0; i < cur_mirror->m_count; i++) {
1624 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1628 "error: %s: merge layout failed: %s\n",
1629 progname, strerror(errno));
1633 mirror_count += cur_mirror->m_count;
1634 cur_mirror = cur_mirror->m_next;
1638 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1642 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1645 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1646 progname, strerror(errno));
1650 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1658 llapi_layout_free(layout);
1663 * Compare files and check lease on @fd.
1665 * \retval bytes number of bytes are the same
1667 static ssize_t mirror_file_compare(int fd, int fdv)
1669 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1671 ssize_t bytes_done = 0;
1672 ssize_t bytes_read = 0;
1674 buf = malloc(buflen * 2);
1679 if (!llapi_lease_check(fd)) {
1680 bytes_done = -EBUSY;
1684 bytes_read = read(fd, buf, buflen);
1685 if (bytes_read <= 0)
1688 if (bytes_read != read(fdv, buf + buflen, buflen))
1692 * XXX: should compute the checksum on each buffer and then
1693 * compare checksum to avoid cache collision
1695 if (memcmp(buf, buf + buflen, bytes_read))
1698 bytes_done += bytes_read;
1706 static int mirror_extend_file(const char *fname, const char *victim_file,
1707 enum mirror_flags mirror_flags)
1712 struct stat stbuf_v;
1713 struct ll_ioc_lease *data = NULL;
1716 fd = open(fname, O_RDWR);
1718 error_loc = "open source file";
1723 fdv = open(victim_file, O_RDWR);
1725 error_loc = "open target file";
1730 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1731 error_loc = "stat source or target file";
1736 if (stbuf.st_dev != stbuf_v.st_dev) {
1737 error_loc = "stat source and target file";
1742 /* mirrors should be of the same size */
1743 if (stbuf.st_size != stbuf_v.st_size) {
1744 error_loc = "file sizes don't match";
1749 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1751 error_loc = "cannot get lease";
1755 if (!(mirror_flags & MF_NO_VERIFY)) {
1757 /* mirrors should have the same contents */
1758 ret = mirror_file_compare(fd, fdv);
1759 if (ret != stbuf.st_size) {
1760 error_loc = "file busy or contents don't match";
1761 rc = ret < 0 ? ret : -EINVAL;
1766 /* Get rid of caching pages from clients */
1767 rc = llapi_file_flush(fd);
1769 error_loc = "cannot get data version";
1773 rc = llapi_file_flush(fdv);
1775 error_loc = "cannot get data version";
1779 rc = migrate_set_timestamps(fd, &stbuf);
1781 error_loc = "cannot set source file timestamp";
1785 /* Atomically put lease, merge layouts and close. */
1786 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1788 error_loc = "memory allocation";
1791 data->lil_mode = LL_LEASE_UNLCK;
1792 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1793 data->lil_count = 1;
1794 data->lil_ids[0] = fdv;
1795 rc = llapi_lease_set(fd, data);
1797 error_loc = "cannot merge layout";
1799 } else if (rc == 0) {
1801 error_loc = "lost lease lock";
1814 (void) unlink(victim_file);
1816 fprintf(stderr, "error: %s: %s: %s: %s\n",
1817 progname, fname, error_loc, strerror(-rc));
1821 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1822 bool inherit, uint32_t flags)
1824 struct llapi_layout *f_layout = NULL;
1825 struct ll_ioc_lease *data = NULL;
1832 f_layout = llapi_layout_get_by_path(name, 0);
1835 fprintf(stderr, "%s: cannot get layout\n", progname);
1838 rc = llapi_layout_get_last_init_comp(f_layout);
1840 fprintf(stderr, "%s: cannot get the last init comp\n",
1844 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1847 "%s: cannot inherit from the last init comp\n",
1853 llapi_layout_comp_flags_set(m_layout, flags);
1854 rc = migrate_open_files(name,
1855 LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
1856 NULL, m_layout, &fd, &fdv);
1860 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1862 error_loc = "cannot get lease";
1866 rc = fstat(fd, &st);
1868 error_loc = "cannot stat source file";
1872 rc = migrate_nonblock(fd, fdv);
1874 llapi_lease_release(fd);
1878 rc = migrate_set_timestamps(fd, &st);
1880 error_loc = "cannot set source file timestamp";
1884 /* Atomically put lease, merge layouts and close. */
1885 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1887 error_loc = "memory allocation";
1890 data->lil_mode = LL_LEASE_UNLCK;
1891 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1892 data->lil_count = 1;
1893 data->lil_ids[0] = fdv;
1894 rc = llapi_lease_set(fd, data);
1896 error_loc = "cannot merge layout";
1898 } else if (rc == 0) {
1900 error_loc = "lost lease lock";
1913 fprintf(stderr, "error: %s: %s: %s: %s\n",
1914 progname, name, error_loc, strerror(-rc));
1918 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1919 enum mirror_flags mirror_flags)
1923 while (mirror_list) {
1924 if (mirror_list->m_file) {
1925 rc = mirror_extend_file(fname, mirror_list->m_file,
1928 __u32 mirror_count = mirror_list->m_count;
1930 while (mirror_count > 0) {
1931 rc = mirror_extend_layout(fname,
1932 mirror_list->m_layout,
1933 mirror_list->m_inherit,
1934 mirror_list->m_flags);
1944 mirror_list = mirror_list->m_next;
1950 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1955 rc = llapi_layout_mirror_id_get(layout, &id);
1959 if ((__u16)id == *(__u16 *)cbdata)
1960 return LLAPI_LAYOUT_ITER_STOP;
1962 return LLAPI_LAYOUT_ITER_CONT;
1965 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1970 rc = llapi_layout_comp_id_get(layout, &id);
1974 if (id == *(__u32 *)cbdata)
1975 return LLAPI_LAYOUT_ITER_STOP;
1977 return LLAPI_LAYOUT_ITER_CONT;
1980 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
1982 char buf[LOV_MAXPOOLNAME + 1];
1983 struct pool_to_id_cbdata *d = (void *)cbdata;
1987 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1990 if (strcmp(d->pool, buf))
1991 return LLAPI_LAYOUT_ITER_CONT;
1993 rc = llapi_layout_mirror_id_get(layout, &id);
1998 return LLAPI_LAYOUT_ITER_STOP;
2001 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2003 char buf[LOV_MAXPOOLNAME + 1];
2004 struct pool_to_id_cbdata *d = (void *)cbdata;
2008 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2011 if (strcmp(d->pool, buf))
2012 return LLAPI_LAYOUT_ITER_CONT;
2014 rc = llapi_layout_comp_id_get(layout, &id);
2019 return LLAPI_LAYOUT_ITER_STOP;
2022 struct collect_ids_data {
2028 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2030 struct collect_ids_data *cid = cbdata;
2034 rc = llapi_layout_mirror_id_get(layout, &id);
2038 if ((__u16)id != cid->cid_exclude) {
2041 for (i = 0; i < cid->cid_count; i++) {
2042 /* already collected the mirror id */
2043 if (id == cid->cid_ids[i])
2044 return LLAPI_LAYOUT_ITER_CONT;
2046 cid->cid_ids[cid->cid_count] = id;
2050 return LLAPI_LAYOUT_ITER_CONT;
2054 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2055 * @mirror_id: Mirror id to be checked.
2056 * @layout: Mirror component list.
2058 * This function checks if a mirror with specified @mirror_id is the last
2059 * non-stale mirror of a layout @layout.
2061 * Return: true or false.
2064 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2066 __u16 mirror_ids[128] = { 0 };
2067 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2069 .cid_exclude = mirror_id, };
2072 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2074 for (i = 0; i < cid.cid_count; i++) {
2075 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2078 comp_size = llapi_mirror_find_stale(layout, comp_array,
2079 ARRAY_SIZE(comp_array),
2088 static int mirror_split(const char *fname, __u32 id, const char *pool,
2089 enum mirror_flags mflags, const char *victim_file)
2091 struct llapi_layout *layout;
2092 char parent[PATH_MAX];
2093 char victim[PATH_MAX];
2094 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2096 struct ll_ioc_lease *data;
2097 uint16_t mirror_count;
2101 bool purge = true; /* delete mirror by setting fdv=fd */
2105 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2107 "error %s: the source file '%s' and -f file are the same\n",
2112 /* check fname contains mirror with mirror_id/comp_id */
2113 layout = llapi_layout_get_by_path(fname, 0);
2116 "error %s: file '%s' couldn't get layout\n",
2121 rc = llapi_layout_sanity(layout, false, true);
2123 llapi_layout_sanity_perror(rc);
2127 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2130 "error %s: file '%s' couldn't get mirror count\n",
2134 if (mirror_count < 2) {
2136 "error %s: file '%s' has %d component, cannot split\n",
2137 progname, fname, mirror_count);
2141 if (mflags & MF_COMP_POOL) {
2142 struct pool_to_id_cbdata data = { .pool = pool };
2144 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2146 mirror_id = data.id;
2147 } else if (mflags & MF_COMP_ID) {
2148 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2149 mirror_id = mirror_id_of(id);
2151 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2155 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2158 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2159 if (mflags & MF_COMP_POOL) {
2161 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2162 progname, fname, pool);
2164 } else if (mflags & MF_COMP_ID) {
2166 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2167 progname, fname, id);
2171 "error %s: file '%s' does not contain mirror with id %u\n",
2172 progname, fname, id);
2177 if (!victim_file && mflags & MF_DESTROY)
2178 /* Allow mirror split even without the key on encrypted files,
2179 * and in this case of a 'split -d', open file with O_DIRECT
2180 * (no IOs will be done).
2182 fd = open(fname, O_RDWR | O_DIRECT | O_FILE_ENC);
2184 fd = open(fname, O_RDWR);
2188 "error %s: open file '%s' failed: %s\n",
2189 progname, fname, strerror(errno));
2193 /* get victim file directory pathname */
2194 if (strlen(fname) > sizeof(parent) - 1) {
2195 fprintf(stderr, "error %s: file name of '%s' too long\n",
2200 strncpy(parent, fname, sizeof(parent));
2201 ptr = strrchr(parent, '/');
2203 if (!getcwd(parent, sizeof(parent))) {
2204 fprintf(stderr, "error %s: getcwd failed: %s\n",
2205 progname, strerror(errno));
2215 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2217 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2222 rc = llapi_file_is_encrypted(fd);
2224 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2225 progname, fname, rc);
2232 /* use a temp file to store the splitted layout */
2233 if (mflags & MF_DESTROY) {
2234 char file_path[PATH_MAX];
2235 unsigned int rnumber;
2238 if (last_non_stale_mirror(mirror_id, layout)) {
2241 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2247 /* don't use volatile file for mirror destroy */
2251 * try the old way to delete mirror using
2256 rc = snprintf(file_path,
2258 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2262 rc >= sizeof(file_path)) {
2263 fdv = -ENAMETOOLONG;
2267 open_flags = O_RDWR |
2268 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2269 O_CREAT | O_EXCL | O_NOFOLLOW |
2270 /* O_DIRECT for mirror split -d */
2272 /* Allow split without the key */
2274 fdv = open(file_path, open_flags,
2278 } while (fdv < 0 && rc == -EEXIST);
2284 "error %s: not permitted on encrypted file '%s': %d\n",
2285 progname, fname, rc);
2289 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2291 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2294 /* user specified victim file */
2298 "error %s: not permitted on encrypted file '%s': %d\n",
2299 progname, fname, rc);
2302 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2307 "error %s: create victim file failed: %s\n",
2308 progname, strerror(errno));
2312 /* get lease lock of fname */
2313 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2316 "error %s: cannot get lease of file '%s': %d\n",
2317 progname, fname, rc);
2321 /* Atomatically put lease, split layouts and close. */
2322 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2328 data->lil_mode = LL_LEASE_UNLCK;
2329 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2330 data->lil_count = 2;
2331 data->lil_ids[0] = fdv;
2332 data->lil_ids[1] = mirror_id;
2333 rc = llapi_lease_set(fd, data);
2335 if ((rc == -EINVAL || rc == -EBUSY) && purge) {
2336 /* could be old MDS which prohibit fd==fdv */
2341 if (rc == 0) /* lost lease lock */
2344 "error %s: cannot split '%s': %s\n",
2345 progname, fname, strerror(-rc));
2357 llapi_layout_free(layout);
2362 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2363 __u16 *mirror_ids, int ids_nr);
2365 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2366 __u64 migration_flags)
2368 struct ll_ioc_lease *data = NULL;
2371 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2373 error_loc = "cannot get lease";
2377 rc = migrate_nonblock(fd, fdv);
2381 /* Atomically put lease, merge layouts, resync and close. */
2382 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2384 error_loc = "memory allocation";
2387 data->lil_mode = LL_LEASE_UNLCK;
2388 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2389 data->lil_count = 1;
2390 data->lil_ids[0] = fdv;
2391 rc = llapi_lease_set(fd, data);
2393 error_loc = "cannot merge layout";
2395 } else if (rc == 0) {
2397 error_loc = "lost lease lock";
2403 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2405 error_loc = "cannot resync file";
2409 /* delete first mirror now */
2410 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2412 error_loc = "cannot delete old layout";
2416 llapi_lease_release(fd);
2422 fprintf(stderr, "error: %s: %s: %s: %s\n",
2423 progname, name, error_loc, strerror(-rc));
2424 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2425 printf("%s\n", name);
2432 * Parse a string containing an target index list into an array of integers.
2434 * The input string contains a comma delimited list of individual
2435 * indices and ranges, for example "1,2-4,7". Add the indices into the
2436 * \a tgts array and remove duplicates.
2438 * \param[out] tgts array to store indices in
2439 * \param[in] size size of \a tgts array
2440 * \param[in] offset starting index in \a tgts
2441 * \param[in] arg string containing OST index list
2442 * \param[in/out] overstriping index list may contain duplicates
2444 * \retval positive number of indices in \a tgts
2445 * \retval -EINVAL unable to parse \a arg
2447 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2448 unsigned long long *pattern)
2452 int slots = size - offset;
2454 bool overstriped = false;
2460 end_of_loop = false;
2461 while (!end_of_loop) {
2462 int start_index = 0;
2465 char *endptr = NULL;
2469 ptr = strchrnul(arg, ',');
2471 end_of_loop = *ptr == '\0';
2475 start_index = strtol(arg, &endptr, 0);
2476 if (endptr == arg) /* no data at all */
2478 if (errno != 0 || start_index < -1 ||
2479 (*endptr != '-' && *endptr != '\0'))
2482 end_index = start_index;
2483 if (*endptr == '-') {
2485 end_index = strtol(endptr + 1, &endptr, 0);
2486 if (errno != 0 || *endptr != '\0' || end_index < -1)
2488 if (end_index < start_index)
2492 for (i = start_index; i <= end_index && slots > 0; i++) {
2495 /* remove duplicate */
2496 for (j = 0; j < offset; j++) {
2497 if (tgts[j] == i && pattern &&
2498 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2500 else if (tgts[j] == i)
2506 if (j == offset) { /* check complete */
2512 if (slots == 0 && i < end_index)
2520 if (!end_of_loop && ptr)
2523 if (!overstriped && pattern)
2524 *pattern = LLAPI_LAYOUT_DEFAULT;
2526 return rc < 0 ? rc : nr;
2529 struct lfs_setstripe_args {
2530 unsigned long long lsa_comp_end;
2531 unsigned long long lsa_stripe_size;
2532 unsigned long long lsa_extension_size;
2533 long long lsa_stripe_count;
2534 long long lsa_stripe_off;
2535 __u32 lsa_comp_flags;
2536 __u32 lsa_comp_neg_flags;
2537 unsigned long long lsa_pattern;
2538 unsigned int lsa_mirror_count;
2540 bool lsa_first_comp;
2541 bool lsa_extension_comp;
2543 char *lsa_pool_name;
2546 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2548 unsigned int mirror_count = lsa->lsa_mirror_count;
2549 bool first_comp = lsa->lsa_first_comp;
2551 memset(lsa, 0, sizeof(*lsa));
2553 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2554 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2555 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2556 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2557 lsa->lsa_pool_name = NULL;
2559 lsa->lsa_mirror_count = mirror_count;
2560 lsa->lsa_first_comp = first_comp;
2564 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2565 * @lsa: Stripe options to be initialized and inherited.
2567 * This function initializes stripe options in @lsa and inherit
2568 * stripe_size, stripe_count and OST pool_name options.
2572 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2574 unsigned long long stripe_size;
2575 long long stripe_count;
2576 char *pool_name = NULL;
2578 stripe_size = lsa->lsa_stripe_size;
2579 stripe_count = lsa->lsa_stripe_count;
2580 pool_name = lsa->lsa_pool_name;
2582 setstripe_args_init(lsa);
2584 lsa->lsa_stripe_size = stripe_size;
2585 lsa->lsa_stripe_count = stripe_count;
2586 lsa->lsa_pool_name = pool_name;
2589 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2591 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2592 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2593 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2594 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2595 lsa->lsa_comp_end != 0);
2598 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2600 if (lsa->lsa_nr_tgts) {
2601 if (lsa->lsa_nr_tgts < 0 ||
2602 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2603 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2609 if (lsa->lsa_stripe_count > 0 &&
2610 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2611 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2612 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2613 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2614 lsa->lsa_stripe_count,
2626 * comp_args_to_layout() - Create or extend a composite layout.
2627 * @composite: Pointer to the composite layout.
2628 * @lsa: Stripe options for the new component.
2630 * This function creates or extends a composite layout by adding a new
2631 * component with stripe options from @lsa.
2633 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2635 * Return: 0 on success or an error code on failure.
2637 static int comp_args_to_layout(struct llapi_layout **composite,
2638 struct lfs_setstripe_args *lsa,
2641 struct llapi_layout *layout = *composite;
2642 uint64_t prev_end = 0;
2648 layout = llapi_layout_alloc();
2650 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2655 *composite = layout;
2656 lsa->lsa_first_comp = true;
2661 * Get current component extent, current component
2662 * must be the tail component.
2664 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2666 fprintf(stderr, "Get comp extent failed. %s\n",
2671 if (lsa->lsa_first_comp) {
2673 rc = llapi_layout_add_first_comp(layout);
2675 rc = llapi_layout_comp_add(layout);
2678 fprintf(stderr, "Add component failed. %s\n",
2684 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2686 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2687 lsa->lsa_comp_flags, strerror(errno));
2692 uint64_t comp_end = lsa->lsa_comp_end;
2695 * The extendable component is 0-length, so it can be removed
2696 * if there is insufficient space to extend it.
2698 if (lsa->lsa_extension_comp)
2699 comp_end = prev_end;
2701 rc = llapi_layout_comp_extent_set(layout, prev_end,
2704 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2705 prev_end, comp_end, strerror(errno));
2709 /* reset lsa_first_comp */
2710 lsa->lsa_first_comp = false;
2712 /* Data-on-MDT component setting */
2713 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2715 if (lsa->lsa_stripe_count == 0)
2716 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2717 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2718 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2719 if (lsa->lsa_stripe_off == -1 ||
2720 lsa->lsa_stripe_off == 0)
2721 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2723 * In case of Data-on-MDT patterns the only extra option
2724 * applicable is stripe size option.
2726 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2728 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2729 lsa->lsa_stripe_count);
2733 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2734 lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2736 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2737 lsa->lsa_stripe_size);
2741 if (lsa->lsa_nr_tgts != 0) {
2743 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2748 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2750 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2751 lsa->lsa_stripe_off);
2755 if (lsa->lsa_pool_name != 0) {
2757 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2758 lsa->lsa_pool_name);
2763 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2765 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2770 /* Data-on-MDT component has always single stripe up to end */
2771 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2772 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2773 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2775 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2782 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2783 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2785 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2786 rc = llapi_layout_extension_size_set(layout, size);
2788 rc = llapi_layout_stripe_size_set(layout, size);
2791 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2792 size, strerror(errno));
2796 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2798 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2799 lsa->lsa_stripe_count, strerror(errno));
2803 if (lsa->lsa_pool_name) {
2804 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2806 fprintf(stderr, "Set pool name: %s failed. %s\n",
2807 lsa->lsa_pool_name, strerror(errno));
2811 rc = llapi_layout_pool_name_set(layout, "");
2813 fprintf(stderr, "Clear pool name failed: %s\n",
2819 rc = lsa_args_stripe_count_check(lsa);
2823 if (lsa->lsa_nr_tgts > 0) {
2826 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2827 rc = llapi_layout_ost_index_set(layout, i,
2832 /* Make sure stripe offset is in OST list. */
2833 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2837 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2838 lsa->lsa_stripe_off);
2842 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2843 lsa->lsa_stripe_off != -1) {
2844 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2847 fprintf(stderr, "Set ost index %d failed. %s\n",
2848 i, strerror(errno));
2852 /* Create the second, virtual component of extension space */
2853 if (lsa->lsa_extension_comp) {
2854 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2855 lsa->lsa_extension_comp = false;
2862 static int build_component(struct llapi_layout **layout,
2863 struct lfs_setstripe_args *lsa, bool set_extent)
2867 rc = comp_args_to_layout(layout, lsa, set_extent);
2871 if (lsa->lsa_mirror_count > 0) {
2872 rc = llapi_layout_mirror_count_set(*layout,
2873 lsa->lsa_mirror_count);
2877 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2880 lsa->lsa_mirror_count = 0;
2886 static int build_prev_component(struct llapi_layout **layout,
2887 struct lfs_setstripe_args *prev,
2888 struct lfs_setstripe_args *lsa,
2891 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2894 if (prev->lsa_stripe_size) {
2896 prev->lsa_comp_end = lsa->lsa_comp_end;
2897 prev->lsa_extension_size = lsa->lsa_extension_size;
2898 prev->lsa_extension_comp = true;
2901 rc = build_component(layout, prev, true);
2907 * Copy lsa to previous lsa;
2908 * if this is an extension component, make the previous invalid;
2911 prev->lsa_stripe_size = 0;
2918 #ifndef LCME_TEMPLATE_FLAGS
2919 #define LCME_TEMPLATE_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
2923 static int build_layout_from_yaml_node(struct cYAML *node,
2924 struct llapi_layout **layout,
2925 struct lfs_setstripe_args *lsa,
2926 struct lfs_setstripe_args *prevp)
2928 struct lfs_setstripe_args prev = { 0 };
2929 __u32 *osts = lsa->lsa_tgts;
2937 string = node->cy_string;
2939 if (node->cy_type == CYAML_TYPE_OBJECT) {
2940 /* go deep to sub blocks */
2941 if (string && !strncmp(string, "component", 9) &&
2942 strncmp(string, "component0", 10) &&
2943 strncmp(string, "components", 10)) {
2944 rc = build_prev_component(layout, prevp, lsa,
2949 /* initialize lsa. */
2950 setstripe_args_init(lsa);
2951 lsa->lsa_first_comp = false;
2952 lsa->lsa_tgts = osts;
2955 rc = build_layout_from_yaml_node(node->cy_child, layout,
2960 if (!node->cy_string)
2963 /* skip leading lmm_ if present, to simplify parsing */
2964 if (strncmp(string, "lmm_", 4) == 0)
2967 if (node->cy_type == CYAML_TYPE_STRING) {
2968 if (!strcmp(string, "lcme_extent.e_end")) {
2969 if (!strcmp(node->cy_valuestring, "EOF") ||
2970 !strcmp(node->cy_valuestring, "eof"))
2971 lsa->lsa_comp_end = LUSTRE_EOF;
2972 } else if (!strcmp(string, "pool")) {
2973 lsa->lsa_pool_name = node->cy_valuestring;
2974 } else if (!strcmp(string, "pattern")) {
2975 if (!strcmp(node->cy_valuestring, "mdt"))
2976 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2977 if (!strcmp(node->cy_valuestring,
2978 "raid0,overstriped"))
2980 LLAPI_LAYOUT_OVERSTRIPING;
2981 } else if (!strcmp(string, "lcme_flags")) {
2982 rc = comp_str2flags(node->cy_valuestring,
2983 &lsa->lsa_comp_flags,
2984 &lsa->lsa_comp_neg_flags);
2988 * Only template flags have meaning in
2989 * the layout for a new file
2991 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2993 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2994 if (!strcmp(string, "lcm_mirror_count")) {
2995 lsa->lsa_mirror_count = node->cy_valueint;
2996 } else if (!strcmp(string, "lcme_extent.e_start")) {
2997 if (node->cy_valueint == 0)
2998 lsa->lsa_first_comp = true;
2999 } else if (!strcmp(string, "lcme_extent.e_end")) {
3000 if (node->cy_valueint == -1)
3001 lsa->lsa_comp_end = LUSTRE_EOF;
3003 lsa->lsa_comp_end = node->cy_valueint;
3004 } else if (!strcmp(string, "stripe_count")) {
3005 lsa->lsa_stripe_count = node->cy_valueint;
3006 } else if (!strcmp(string, "stripe_size")) {
3007 lsa->lsa_stripe_size = node->cy_valueint;
3008 } else if (!strcmp(string, "extension_size")) {
3009 lsa->lsa_extension_size = node->cy_valueint;
3010 lsa->lsa_extension_comp = true;
3011 } else if (!strcmp(string, "stripe_offset")) {
3012 lsa->lsa_stripe_off = node->cy_valueint;
3013 } else if (!strcmp(string, "l_ost_idx")) {
3014 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3019 node = node->cy_next;
3022 if (prevp == &prev) {
3023 rc = build_prev_component(layout, prevp, lsa, true);
3027 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3028 rc = build_component(layout, lsa, *layout != NULL);
3034 static int lfs_comp_create_from_yaml(char *template,
3035 struct llapi_layout **layout,
3036 struct lfs_setstripe_args *lsa,
3039 struct cYAML *tree = NULL, *err_rc = NULL;
3042 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3044 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3045 progname, template);
3046 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3047 "can't parse", &err_rc);
3048 cYAML_print_tree2file(stderr, err_rc);
3049 cYAML_free_tree(err_rc);
3054 /* initialize lsa for plain file */
3055 setstripe_args_init(lsa);
3056 lsa->lsa_tgts = osts;
3058 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3060 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3061 progname, template);
3064 /* clean clean lsa */
3065 setstripe_args_init(lsa);
3069 cYAML_free_tree(tree);
3074 * Get the extension size from the next (SEL) component and extend the
3075 * current component on it. The start of the next component is to be
3078 * \param[in] layout the current layout
3079 * \param[in] start the start of the current component
3080 * \param[in,out] end the end of the current component
3081 * \param[in] offset the offset to adjust the end position to instead of
3084 * \retval 0 - extended successfully
3085 * \retval < 0 - error
3087 static int layout_extend_comp(struct llapi_layout *layout,
3088 uint64_t start, uint64_t *end,
3091 uint64_t size, next_start, next_end;
3094 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3097 "%s setstripe: cannot move component cursor: %s\n",
3098 progname, strerror(errno));
3103 * Even if the @size will not be used below, this will fail if
3104 * this is not a SEL component - a good confirmation we are
3105 * working on right components.
3107 rc = llapi_layout_extension_size_get(layout, &size);
3110 "%s setstripe: cannot get component ext size: %s\n",
3111 progname, strerror(errno));
3115 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3117 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3118 progname, strerror(errno));
3122 next_start += offset ?: size;
3123 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3125 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3126 progname, strerror(errno));
3130 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3133 "%s setstripe: cannot move component cursor: %s\n",
3134 progname, strerror(errno));
3138 *end += offset ?: size;
3139 rc = llapi_layout_comp_extent_set(layout, start, *end);
3141 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3142 progname, strerror(errno));
3150 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3151 * end of the last component in the existing file, and adjust the
3152 * first extent start of the components to be added accordingly.
3154 * In the create mode, we need to check if the first component is an extendable
3155 * SEL component and extend its length to the extension size (first component
3156 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3158 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3161 struct llapi_layout *head;
3162 uint64_t start = 0, prev_end = 0;
3166 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3171 head = llapi_layout_get_by_path(fname, 0);
3174 "%s setstripe: cannot read layout from '%s': %s\n",
3175 progname, fname, strerror(errno));
3177 } else if (errno == ENODATA) {
3179 * file without LOVEA, this component-add will be turned
3180 * into a component-create.
3182 llapi_layout_free(head);
3186 * the new layout will be added to an empty one, it
3187 * still needs to be adjusted below
3191 } else if (!llapi_layout_is_composite(head)) {
3193 "%s setstripe: '%s' not a composite file\n",
3195 llapi_layout_free(head);
3199 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3202 "%s setstripe: cannot get prev extent: %s\n",
3203 progname, strerror(errno));
3204 llapi_layout_free(head);
3208 llapi_layout_free(head);
3212 /* Make sure we use the first component of the layout to be added. */
3213 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3216 "%s setstripe: cannot move component cursor: %s\n",
3217 progname, strerror(errno));
3221 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3223 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3224 progname, strerror(errno));
3228 if (start == 0 && end == 0) {
3229 rc = layout_extend_comp(layout, start, &end,
3230 comp_add ? prev_end : 0);
3235 if (start > prev_end || end < prev_end) {
3237 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3238 progname, start, end, prev_end);
3242 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3245 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3246 progname, prev_end, end, strerror(errno));
3253 static int mirror_adjust_first_extents(struct mirror_args *list)
3261 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3264 list = list->m_next;
3270 static inline bool arg_is_eof(char *arg)
3272 return !strncmp(arg, "-1", strlen("-1")) ||
3273 !strncmp(arg, "EOF", strlen("EOF")) ||
3274 !strncmp(arg, "eof", strlen("eof"));
3278 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3280 * Return: Valid mirror_args pointer on success and
3281 * NULL if memory allocation fails.
3283 static struct mirror_args *lfs_mirror_alloc(void)
3285 struct mirror_args *mirror = NULL;
3288 mirror = calloc(1, sizeof(*mirror));
3290 mirror->m_inherit = false;
3301 * lfs_mirror_free() - Free memory allocated for a mirror argument
3303 * @mirror: Previously allocated mirror argument structure by
3304 * lfs_mirror_alloc().
3306 * Free memory allocated for @mirror.
3310 static void lfs_mirror_free(struct mirror_args *mirror)
3312 if (mirror->m_layout)
3313 llapi_layout_free(mirror->m_layout);
3318 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3319 * @mirror_list: Previously allocated mirror list.
3321 * Free memory allocated for @mirror_list.
3325 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3327 struct mirror_args *next_mirror = NULL;
3329 while (mirror_list) {
3330 next_mirror = mirror_list->m_next;
3331 lfs_mirror_free(mirror_list);
3332 mirror_list = next_mirror;
3337 LFS_SETQUOTA_DELETE = 1,
3345 LFS_COMP_NO_VERIFY_OPT,
3347 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3349 LFS_MIRROR_STATE_OPT,
3351 LFS_MIRROR_INDEX_OPT,
3352 LFS_LAYOUT_FOREIGN_OPT,
3361 #ifndef LCME_USER_MIRROR_FLAGS
3362 /* The mirror flags can be set by users at creation time. */
3363 #define LCME_USER_MIRROR_FLAGS (LCME_FL_PREF_RW)
3367 static int lfs_setstripe_internal(int argc, char **argv,
3368 enum setstripe_origin opc)
3370 struct lfs_setstripe_args lsa = { 0 };
3371 struct llapi_stripe_param *param = NULL;
3372 struct find_param migrate_mdt_param = {
3382 unsigned long long size_units = 1;
3383 bool migrate_mode = false;
3384 bool migrate_mdt_mode = false;
3385 bool setstripe_mode = false;
3386 bool migration_block = false;
3387 __u64 migration_flags = 0;
3388 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3389 int comp_del = 0, comp_set = 0;
3392 struct llapi_layout *layout = NULL;
3393 struct llapi_layout **lpp = &layout;
3394 bool mirror_mode = false;
3395 bool has_m_file = false;
3396 __u32 mirror_count = 0;
3397 enum mirror_flags mirror_flags = 0;
3398 struct mirror_args *mirror_list = NULL;
3399 struct mirror_args *new_mirror = NULL;
3400 struct mirror_args *last_mirror = NULL;
3401 __u16 mirror_id = 0;
3403 bool from_yaml = false;
3404 bool from_copy = false;
3405 char *template = NULL;
3406 bool foreign_mode = false;
3408 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3409 char *mode_opt = NULL;
3410 mode_t previous_umask = 0;
3411 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3413 struct option long_opts[] = {
3414 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3415 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3416 /* --block is only valid in migrate mode */
3417 { .val = 'b', .name = "block", .has_arg = no_argument },
3418 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3419 { .val = LFS_COMP_ADD_OPT,
3420 .name = "comp-add", .has_arg = no_argument },
3421 { .val = LFS_COMP_ADD_OPT,
3422 .name = "component-add", .has_arg = no_argument },
3423 { .val = LFS_COMP_DEL_OPT,
3424 .name = "comp-del", .has_arg = no_argument },
3425 { .val = LFS_COMP_DEL_OPT,
3426 .name = "component-del", .has_arg = no_argument },
3427 { .val = LFS_COMP_FLAGS_OPT,
3428 .name = "comp-flags", .has_arg = required_argument },
3429 { .val = LFS_COMP_FLAGS_OPT,
3430 .name = "component-flags",
3431 .has_arg = required_argument },
3432 { .val = LFS_COMP_SET_OPT,
3433 .name = "comp-set", .has_arg = no_argument },
3434 { .val = LFS_COMP_SET_OPT,
3435 .name = "component-set",
3436 .has_arg = no_argument},
3437 { .val = LFS_COMP_NO_VERIFY_OPT,
3438 .name = "no-verify", .has_arg = no_argument},
3439 { .val = LFS_LAYOUT_FLAGS_OPT,
3440 .name = "flags", .has_arg = required_argument},
3441 { .val = LFS_LAYOUT_FOREIGN_OPT,
3442 .name = "foreign", .has_arg = optional_argument},
3443 { .val = LFS_MIRROR_ID_OPT,
3444 .name = "mirror-id", .has_arg = required_argument},
3445 { .val = LFS_MODE_OPT,
3446 .name = "mode", .has_arg = required_argument},
3447 { .val = LFS_LAYOUT_COPY,
3448 .name = "copy", .has_arg = required_argument},
3449 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3450 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3451 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3452 { .val = 'C', .name = "overstripe-count",
3453 .has_arg = required_argument},
3454 { .val = 'd', .name = "delete", .has_arg = no_argument},
3455 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3456 /* used with "lfs migrate -m" */
3457 { .val = 'd', .name = "directory", .has_arg = no_argument},
3458 /* --non-direct is only valid in migrate mode */
3459 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3460 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3461 { .val = 'E', .name = "component-end",
3462 .has_arg = required_argument},
3463 { .val = 'f', .name = "file", .has_arg = required_argument },
3464 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3465 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3466 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3467 { .val = 'h', .name = "help", .has_arg = no_argument },
3468 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3469 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3470 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3471 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3472 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3473 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3474 { .val = 'L', .name = "layout", .has_arg = required_argument },
3475 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3476 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3477 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3478 /* --non-block is only valid in migrate mode */
3479 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3480 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3481 { .val = 'o', .name = "ost", .has_arg = required_argument },
3482 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3483 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3484 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3486 { .val = 'p', .name = "pool", .has_arg = required_argument },
3487 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3488 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3489 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3490 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3491 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3492 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3493 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3494 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3495 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3496 /* --verbose is only valid in migrate mode */
3497 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3498 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3499 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3500 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3501 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3502 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3505 setstripe_args_init(&lsa);
3507 migrate_mode = (opc == SO_MIGRATE);
3508 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3509 setstripe_mode = (opc == SO_SETSTRIPE);
3510 if (opc == SO_MIRROR_DELETE) {
3512 mirror_flags = MF_DESTROY;
3515 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3517 while ((c = getopt_long(argc, argv,
3518 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:y:z:",
3519 long_opts, NULL)) >= 0) {
3525 case LFS_COMP_ADD_OPT:
3528 case LFS_COMP_DEL_OPT:
3531 case LFS_COMP_FLAGS_OPT:
3532 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3533 &lsa.lsa_comp_neg_flags);
3536 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3538 "%s: inverted flags are not supported\n",
3543 case LFS_COMP_SET_OPT:
3546 case LFS_COMP_NO_VERIFY_OPT:
3547 mirror_flags |= MF_NO_VERIFY;
3549 case LFS_MIRROR_ID_OPT: {
3550 unsigned long int id;
3553 id = strtoul(optarg, &end, 0);
3554 if (errno != 0 || *end != '\0' || id == 0 ||
3557 "%s %s: invalid mirror ID '%s'\n",
3558 progname, argv[0], optarg);
3562 mirror_id = (__u16)id;
3565 case LFS_LAYOUT_FLAGS_OPT: {
3568 /* check for numeric flags (foreign and mirror cases) */
3569 if (setstripe_mode && !mirror_mode && !last_mirror) {
3571 flags = strtoul(optarg, &end, 16);
3572 if (errno != 0 || *end != '\0' ||
3573 flags >= UINT32_MAX) {
3575 "%s %s: invalid hex flags '%s'\n",
3576 progname, argv[0], optarg);
3579 if (!foreign_mode) {
3581 "%s %s: hex flags must be specified with --foreign option\n",
3588 if (!mirror_mode || !last_mirror) {
3590 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3595 result = comp_str2flags(optarg, &last_mirror->m_flags,
3602 "%s: inverted flags are not supported\n",
3607 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3609 "%s: unsupported mirror flags: %s\n",
3616 case LFS_LAYOUT_FOREIGN_OPT:
3618 /* check pure numeric */
3619 type = strtoul(optarg, &end, 0);
3622 type = check_foreign_type_name(optarg);
3623 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3625 "%s %s: unrecognized foreign type '%s'\n",
3630 } else if (type >= UINT32_MAX) {
3632 "%s %s: invalid foreign type '%s'\n",
3633 progname, argv[0], optarg);
3637 foreign_mode = true;
3642 mode = strtoul(mode_opt, &end, 8);
3645 "%s %s: bad mode '%s'\n",
3646 progname, argv[0], mode_opt);
3649 previous_umask = umask(0);
3652 case LFS_LAYOUT_COPY:
3657 if (!migrate_mode) {
3659 "%s %s: -b|--block valid only for migrate command\n",
3663 migration_block = true;
3666 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3668 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3672 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3676 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3677 if (errno != 0 || *end != '\0'|| optarg == end ||
3678 lsa.lsa_stripe_count < -1 ||
3679 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3681 "%s %s: invalid stripe count '%s'\n",
3682 progname, argv[0], optarg);
3686 if (lsa.lsa_stripe_count == -1)
3687 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3691 migrate_mdt_param.fp_max_depth = 1;
3693 /* delete the default striping pattern */
3695 if (opc == SO_MIRROR_SPLIT) {
3698 "%s %s: -d cannot used with -f\n",
3702 mirror_flags |= MF_DESTROY;
3707 if (!migrate_mode) {
3709 "%s %s: -D|--non-direct is valid only for migrate command\n",
3713 migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3716 if (lsa.lsa_comp_end != 0) {
3717 result = comp_args_to_layout(lpp, &lsa, true);
3719 fprintf(stderr, "%s: invalid layout\n",
3724 setstripe_args_init_inherit(&lsa);
3727 if (arg_is_eof(optarg)) {
3728 lsa.lsa_comp_end = LUSTRE_EOF;
3730 result = llapi_parse_size(optarg,
3733 /* assume units of KB if too small */
3734 if (lsa.lsa_comp_end < 4096)
3735 lsa.lsa_comp_end *= 1024;
3737 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3739 "%s %s: invalid component end '%s'\n",
3740 progname, argv[0], optarg);
3746 if (!migrate_mode) {
3748 "--mdt-hash is valid only for migrate command\n");
3752 lsa.lsa_pattern = check_hashtype(optarg);
3753 if (lsa.lsa_pattern == 0) {
3755 "%s %s: bad stripe hash type '%s'\n",
3756 progname, argv[0], optarg);
3762 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3763 if (errno != 0 || *end != '\0' || optarg == end ||
3764 lsa.lsa_stripe_off < -1 ||
3765 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3767 "%s %s: invalid stripe offset '%s'\n",
3768 progname, argv[0], optarg);
3771 if (lsa.lsa_stripe_off == -1)
3772 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3775 comp_id = strtoul(optarg, &end, 0);
3776 if (*end != '\0' || comp_id == 0 ||
3777 comp_id > LCME_ID_MAX) {
3779 "%s %s: invalid component ID '%s'\n",
3780 progname, argv[0], optarg);
3785 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3787 "error: %s: invalid option: %s\n",
3788 progname, argv[optopt + 1]);
3791 if (opc == SO_MIRROR_EXTEND) {
3794 "error: %s: '-N' must exist in front of '%s'\n",
3795 progname, argv[optopt + 1]);
3798 last_mirror->m_file = optarg;
3799 last_mirror->m_count = 1;
3803 mirror_list = lfs_mirror_alloc();
3804 mirror_list->m_file = optarg;
3809 if (strcmp(argv[optind - 1], "mdt") == 0) {
3810 /* Can be only the first component */
3814 "error: 'mdt' layout can be only the first one\n");
3817 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3820 "error: 'mdt' layout size is too big\n");
3823 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3824 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
3825 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3828 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3834 if (!migrate_mode) {
3836 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3840 migrate_mdt_mode = true;
3841 lsa.lsa_nr_tgts = parse_targets(tgts,
3842 sizeof(tgts) / sizeof(__u32),
3843 lsa.lsa_nr_tgts, optarg, NULL);
3844 if (lsa.lsa_nr_tgts < 0) {
3846 "%s: invalid MDT target(s) '%s'\n",
3851 lsa.lsa_tgts = tgts;
3852 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3853 lsa.lsa_stripe_off = tgts[0];
3856 if (!migrate_mode) {
3858 "%s %s: -n|--non-block valid only for migrate command\n",
3862 migration_flags |= LLAPI_MIGRATION_NONBLOCK;
3865 if (opc == SO_SETSTRIPE) {
3866 opc = SO_MIRROR_CREATE;
3872 mirror_count = strtoul(optarg, &end, 0);
3873 if (errno != 0 || *end != '\0' ||
3874 mirror_count == 0 ||
3875 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3877 "error: %s: bad mirror count: %s\n",
3884 new_mirror = lfs_mirror_alloc();
3885 new_mirror->m_count = mirror_count;
3888 mirror_list = new_mirror;
3891 /* wrap up last mirror */
3892 if (!setstripe_args_specified(&lsa))
3893 last_mirror->m_inherit = true;
3894 if (lsa.lsa_comp_end == 0)
3895 lsa.lsa_comp_end = LUSTRE_EOF;
3897 result = comp_args_to_layout(lpp, &lsa, true);
3899 lfs_mirror_free(new_mirror);
3903 setstripe_args_init_inherit(&lsa);
3905 last_mirror->m_next = new_mirror;
3908 last_mirror = new_mirror;
3909 lpp = &last_mirror->m_layout;
3912 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3913 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3915 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3917 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3919 "%s %s: -o|--ost incompatible with DoM layout\n",
3924 * -o allows overstriping, and must note it because
3925 * parse_targets is shared with MDT striping, which
3926 * does not allow duplicates
3928 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3929 lsa.lsa_nr_tgts = parse_targets(tgts,
3930 sizeof(tgts) / sizeof(__u32),
3931 lsa.lsa_nr_tgts, optarg,
3933 if (lsa.lsa_nr_tgts < 0) {
3935 "%s %s: invalid OST target(s) '%s'\n",
3936 progname, argv[0], optarg);
3940 lsa.lsa_tgts = tgts;
3941 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3942 lsa.lsa_stripe_off = tgts[0];
3948 if (optarg[0] == '\0' || lov_pool_is_inherited(optarg))
3949 lsa.lsa_pool_name = NULL;
3951 lsa.lsa_pool_name = optarg;
3954 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3956 /* assume units of KB if too small to be valid */
3957 if (lsa.lsa_stripe_size < 4096)
3958 lsa.lsa_stripe_size *= 1024;
3960 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3962 "%s %s: invalid stripe size '%s'\n",
3963 progname, argv[0], optarg);
3968 if (!migrate_mode) {
3970 "%s %s: -v|--verbose valid only for migrate command\n",
3974 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3975 migration_flags = LLAPI_MIGRATION_VERBOSE;
3985 result = llapi_parse_size(optarg,
3986 &lsa.lsa_extension_size,
3990 "%s %s: invalid extension size '%s'\n",
3991 progname, argv[0], optarg);
3995 lsa.lsa_extension_comp = true;
3998 fprintf(stderr, "%s: unrecognized option '%s'\n",
3999 progname, argv[optind - 1]);
4005 fname = argv[optind];
4007 if (optind == argc) {
4008 fprintf(stderr, "%s %s: FILE must be specified\n",
4013 /* lfs migrate $filename should keep the file's layout by default */
4014 if (migrate_mode && !layout && !from_yaml &&
4015 !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4018 if (xattr && !foreign_mode) {
4020 * only print a warning as this is harmless and will be ignored
4023 "%s %s: xattr has been specified for non-foreign layout\n",
4025 } else if (foreign_mode && !xattr) {
4027 "%s %s: xattr must be provided in foreign mode\n",
4032 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4033 comp_set || comp_id || delete || from_copy ||
4034 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4037 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4042 if (mirror_mode && mirror_count == 0) {
4044 "error: %s: --mirror-count|-N option is required\n",
4051 if (!setstripe_args_specified(&lsa))
4052 last_mirror->m_inherit = true;
4053 if (lsa.lsa_comp_end == 0)
4054 lsa.lsa_comp_end = LUSTRE_EOF;
4057 if (lsa.lsa_comp_end != 0) {
4058 result = comp_args_to_layout(lpp, &lsa, true);
4060 fprintf(stderr, "error: %s: invalid layout\n",
4067 if (mirror_flags & MF_NO_VERIFY) {
4068 if (opc != SO_MIRROR_EXTEND) {
4070 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4074 } else if (!has_m_file) {
4076 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4083 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4085 "%s %s: --component-set doesn't have component-id set\n",
4090 if ((delete + comp_set + comp_del + comp_add) > 1) {
4092 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4097 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4098 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4100 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4105 if ((comp_set || comp_del) &&
4106 (setstripe_args_specified(&lsa) || layout != NULL)) {
4108 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4113 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4115 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4120 if (comp_add || comp_del) {
4123 result = lstat(fname, &st);
4124 if (result == 0 && S_ISDIR(st.st_mode)) {
4126 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4133 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4142 "%s %s: option -E must be specified with --component-add\n",
4148 if (from_yaml && from_copy) {
4150 "%s: can't specify --yaml and --copy together\n",
4155 if ((from_yaml || from_copy) &&
4156 (setstripe_args_specified(&lsa) || layout != NULL)) {
4158 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4163 if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4165 "%s %s: options --non-block and --block are mutually exclusive\n",
4170 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4171 opc != SO_MIRROR_DELETE && comp_id != 0) {
4173 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4178 if (migrate_mdt_mode) {
4179 struct lmv_user_md *lmu;
4181 /* initialize migrate mdt parameters */
4182 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4183 LMV_USER_MAGIC_SPECIFIC));
4186 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4187 progname, argv[0], strerror(ENOMEM));
4191 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4192 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4193 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4195 "%s %s: migrate should specify MDT index\n",
4200 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4201 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4202 lmu->lum_hash_type = lsa.lsa_pattern;
4204 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4205 if (lsa.lsa_pool_name) {
4206 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4207 sizeof(lmu->lum_pool_name) - 1);
4208 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4210 if (lsa.lsa_nr_tgts > 1) {
4213 if (lsa.lsa_stripe_count > 0 &&
4214 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4215 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4217 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4218 progname, lsa.lsa_stripe_count,
4224 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4225 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4226 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4227 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4229 lmu->lum_magic = LMV_USER_MAGIC;
4232 migrate_mdt_param.fp_lmv_md = lmu;
4233 migrate_mdt_param.fp_migrate = 1;
4234 } else if (!layout) {
4235 if (lsa_args_stripe_count_check(&lsa))
4238 /* initialize stripe parameters */
4239 param = calloc(1, offsetof(typeof(*param),
4240 lsp_osts[lsa.lsa_nr_tgts]));
4243 "%s %s: cannot allocate memory for parameters: %s\n",
4244 progname, argv[0], strerror(ENOMEM));
4249 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4250 param->lsp_stripe_size = lsa.lsa_stripe_size;
4251 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4252 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4253 param->lsp_stripe_count = -1;
4255 param->lsp_stripe_count = lsa.lsa_stripe_count;
4257 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4258 param->lsp_stripe_offset = -1;
4260 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4261 param->lsp_stripe_pattern =
4262 llapi_pattern_to_lov(lsa.lsa_pattern);
4263 if (param->lsp_stripe_pattern == EINVAL) {
4264 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4269 param->lsp_pool = lsa.lsa_pool_name;
4270 param->lsp_is_specific = false;
4272 if (lsa.lsa_nr_tgts > 0) {
4273 param->lsp_is_specific = true;
4274 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4275 memcpy(param->lsp_osts, tgts,
4276 sizeof(*tgts) * lsa.lsa_nr_tgts);
4281 /* generate a layout from a YAML template */
4282 result = lfs_comp_create_from_yaml(template, &layout,
4286 "error: %s: can't create composite layout from template file %s\n",
4292 if (layout != NULL || mirror_list != NULL) {
4294 result = mirror_adjust_first_extents(mirror_list);
4296 result = layout_adjust_first_extent(fname, layout,
4298 if (result == -ENODATA)
4300 else if (result != 0) {
4301 fprintf(stderr, "error: %s: invalid layout\n",
4307 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4309 layout = llapi_layout_get_by_path(template ?: fname, 0);
4312 "%s: can't create composite layout from file %s: %s\n",
4313 progname, template ?: fname,
4320 if (migrate_mdt_mode) {
4321 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4322 } else if (migrate_mode) {
4323 result = lfs_migrate(fname, migration_flags, param,
4325 } else if (comp_set != 0) {
4326 result = lfs_component_set(fname, comp_id,
4329 lsa.lsa_comp_neg_flags);
4330 } else if (comp_del != 0) {
4331 result = lfs_component_del(fname, comp_id,
4333 lsa.lsa_comp_neg_flags);
4334 } else if (comp_add != 0) {
4335 result = lfs_component_add(fname, layout);
4336 } else if (opc == SO_MIRROR_CREATE) {
4337 result = mirror_create(fname, mirror_list);
4338 } else if (opc == SO_MIRROR_EXTEND) {
4339 result = mirror_extend(fname, mirror_list,
4341 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4342 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4344 "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4348 if (lsa.lsa_pool_name)
4349 mirror_flags |= MF_COMP_POOL;
4350 else if (mirror_id != 0)
4351 comp_id = mirror_id;
4353 mirror_flags |= MF_COMP_ID;
4354 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4356 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4360 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4362 has_m_file ? mirror_list->m_file :
4364 } else if (layout) {
4365 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4371 } else if (foreign_mode) {
4372 result = llapi_file_create_foreign(fname, mode, type,
4379 result = llapi_file_open_param(fname,
4388 /* Save the first error encountered. */
4396 umask(previous_umask);
4399 free(migrate_mdt_param.fp_lmv_md);
4400 llapi_layout_free(layout);
4401 lfs_mirror_list_free(mirror_list);
4406 llapi_layout_free(layout);
4407 lfs_mirror_list_free(mirror_list);
4411 static int lfs_poollist(int argc, char **argv)
4416 return llapi_poollist(argv[1]);
4419 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4420 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4425 char *endptr = "AD";
4430 else if (str[0] == '-')
4436 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4437 long long val = strtoll(timebuf, &endptr, 0);
4442 unit *= 52; /* 52 weeks + 1 day below */
4446 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4447 param->fp_time_margin *= (1 + unit / 52);
4448 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4450 case '\0': /* days are default unit if none used */
4463 /* don't need to multiply by 1 for seconds */
4466 "%s find: bad time string '%s': %s\n",
4467 progname, timebuf, strerror(EINVAL));
4471 if (param->fp_time_margin == 0 ||
4472 (*endptr && unit < param->fp_time_margin))
4473 param->fp_time_margin = unit;
4480 fprintf(stderr, "%s find: bad time '%s': too large\n",
4490 static int str2quotaid(__u32 *id, const char *arg)
4492 unsigned long int projid_tmp = 0;
4493 char *endptr = NULL;
4495 projid_tmp = strtoul(arg, &endptr, 10);
4496 if (*endptr != '\0')
4498 /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4499 if (projid_tmp >= UINT32_MAX)
4506 static int name2uid(unsigned int *id, const char *name)
4508 struct passwd *passwd;
4510 passwd = getpwnam(name);
4513 *id = passwd->pw_uid;
4518 static int name2gid(unsigned int *id, const char *name)
4520 struct group *group;
4522 group = getgrnam(name);
4525 *id = group->gr_gid;
4530 static inline int name2projid(unsigned int *id, const char *name)
4535 static int uid2name(char **name, unsigned int id)
4537 struct passwd *passwd;
4539 passwd = getpwuid(id);
4542 *name = passwd->pw_name;
4547 static inline int gid2name(char **name, unsigned int id)
4549 struct group *group;
4551 group = getgrgid(id);
4554 *name = group->gr_name;
4559 static int name2layout(__u32 *layout, char *name)
4561 char *ptr, *layout_name;
4564 for (ptr = name; ; ptr = NULL) {
4565 layout_name = strtok(ptr, ",");
4568 if (strcmp(layout_name, "released") == 0)
4569 *layout |= LOV_PATTERN_F_RELEASED;
4570 else if (strcmp(layout_name, "raid0") == 0)
4571 *layout |= LOV_PATTERN_RAID0;
4572 else if (strcmp(layout_name, "mdt") == 0)
4573 *layout |= LOV_PATTERN_MDT;
4574 else if (strcmp(layout_name, "overstriping") == 0)
4575 *layout |= LOV_PATTERN_OVERSTRIPING;
4582 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4585 int user, group, other;
4590 mode_t previous_flags;
4592 user = group = other = 0;
4612 user = group = other = 1;
4623 who = user || group || other;
4626 usermask = umask(0022);
4631 if (*input == '-' || *input == '+' || *input == '=')
4634 /* operation is required */
4637 /* get the flags in *outmode */
4640 previous_flags = (*outmode & 0700);
4641 perm |= user ? previous_flags : 0;
4642 perm |= group ? (previous_flags >> 3) : 0;
4643 perm |= other ? (previous_flags >> 6) : 0;
4647 previous_flags = (*outmode & 0070);
4648 perm |= user ? (previous_flags << 3) : 0;
4649 perm |= group ? previous_flags : 0;
4650 perm |= other ? (previous_flags >> 3) : 0;
4654 previous_flags = (*outmode & 0007);
4655 perm |= user ? (previous_flags << 6) : 0;
4656 perm |= group ? (previous_flags << 3) : 0;
4657 perm |= other ? previous_flags : 0;
4664 /* this part is optional,
4665 * if empty perm = 0 and *outmode is not modified
4672 perm |= user ? 0400 : 0;
4673 perm |= group ? 0040 : 0;
4674 perm |= other ? 0004 : 0;
4675 /* set read permission for uog except for umask's
4678 perm |= who ? 0 : (0444 & ~usermask);
4681 perm |= user ? 0200 : 0;
4682 perm |= group ? 0020 : 0;
4683 perm |= other ? 0002 : 0;
4684 /* set write permission for uog except for umask'
4687 perm |= who ? 0 : (0222 & ~usermask);
4690 perm |= user ? 0100 : 0;
4691 perm |= group ? 0010 : 0;
4692 perm |= other ? 0001 : 0;
4693 /* set execute permission for uog except for umask'
4696 perm |= who ? 0 : (0111 & ~usermask);
4700 * Adds execute permission to 'u', 'g' and/or 'g' if
4701 * specified and either 'u', 'g' or 'o' already has
4702 * execute permissions.
4704 if ((*outmode & 0111) != 0) {
4705 perm |= user ? 0100 : 0;
4706 perm |= group ? 0010 : 0;
4707 perm |= other ? 0001 : 0;
4708 perm |= !who ? 0111 : 0;
4712 /* s is ignored if o is given, but it's not an error */
4713 if (other && !group && !user)
4715 perm |= user ? S_ISUID : 0;
4716 perm |= group ? S_ISGID : 0;
4719 /* 't' should be used when 'a' is given
4722 perm |= (!who || all) ? S_ISVTX : 0;
4723 /* using ugo with t is not an error */
4734 /* uog flags should be only one character long */
4735 if (previous_flags && (*input != '\0' && *input != ','))
4740 /* remove the flags from outmode */
4744 /* add the flags to outmode */
4748 /* set the flags of outmode to perm */
4758 static int str2mode_t(const char *input, mode_t *outmode)
4765 if (*input >= '0' && *input <= '7') {
4766 /* parse octal representation */
4771 /* look for invalid digits in octal representation */
4772 while (isdigit(*iter))
4777 *outmode = strtoul(input, &end, 8);
4779 if (errno != 0 || *outmode > 07777) {
4784 } else if (*input == '8' || *input == '9') {
4785 /* error: invalid octal number */
4788 /* parse coma seperated list of symbolic representation */
4797 rc = parse_symbolic(input, outmode, &end);
4802 } while (*end == ',');
4810 static int lfs_find(int argc, char **argv)
4815 struct find_param param = {
4818 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4820 struct option long_opts[] = {
4821 { .val = 'A', .name = "atime", .has_arg = required_argument },
4822 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4823 { .val = 'B', .name = "btime", .has_arg = required_argument },
4824 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4825 { .val = LFS_COMP_COUNT_OPT,
4826 .name = "comp-count", .has_arg = required_argument },
4827 { .val = LFS_COMP_COUNT_OPT,
4828 .name = "component-count",
4829 .has_arg = required_argument },
4830 { .val = LFS_COMP_FLAGS_OPT,
4831 .name = "comp-flags", .has_arg = required_argument },
4832 { .val = LFS_COMP_FLAGS_OPT,
4833 .name = "component-flags",
4834 .has_arg = required_argument },
4835 { .val = LFS_COMP_START_OPT,
4836 .name = "comp-start", .has_arg = required_argument },
4837 { .val = LFS_COMP_START_OPT,
4838 .name = "component-start",
4839 .has_arg = required_argument },
4840 { .val = LFS_MIRROR_STATE_OPT,
4841 .name = "mirror-state", .has_arg = required_argument },
4842 { .val = LFS_NEWERXY_OPT,
4843 .name = "newer", .has_arg = required_argument},
4844 { .val = LFS_NEWERXY_OPT,
4845 .name = "neweraa", .has_arg = required_argument},
4846 { .val = LFS_NEWERXY_OPT,
4847 .name = "neweram", .has_arg = required_argument},
4848 { .val = LFS_NEWERXY_OPT,
4849 .name = "newerac", .has_arg = required_argument},
4850 { .val = LFS_NEWERXY_OPT,
4851 .name = "newerab", .has_arg = required_argument},
4852 { .val = LFS_NEWERXY_OPT,
4853 .name = "newerma", .has_arg = required_argument},
4854 { .val = LFS_NEWERXY_OPT,
4855 .name = "newermm", .has_arg = required_argument},
4856 { .val = LFS_NEWERXY_OPT,
4857 .name = "newermc", .has_arg = required_argument},
4858 { .val = LFS_NEWERXY_OPT,
4859 .name = "newermb", .has_arg = required_argument},
4860 { .val = LFS_NEWERXY_OPT,
4861 .name = "newerca", .has_arg = required_argument},
4862 { .val = LFS_NEWERXY_OPT,
4863 .name = "newercm", .has_arg = required_argument},
4864 { .val = LFS_NEWERXY_OPT,
4865 .name = "newercc", .has_arg = required_argument},
4866 { .val = LFS_NEWERXY_OPT,
4867 .name = "newercb", .has_arg = required_argument},
4868 { .val = LFS_NEWERXY_OPT,
4869 .name = "newerba", .has_arg = required_argument},
4870 { .val = LFS_NEWERXY_OPT,
4871 .name = "newerbm", .has_arg = required_argument},
4872 { .val = LFS_NEWERXY_OPT,
4873 .name = "newerbc", .has_arg = required_argument},
4874 { .val = LFS_NEWERXY_OPT,
4875 .name = "newerbb", .has_arg = required_argument},
4876 { .val = LFS_NEWERXY_OPT,
4877 .name = "newerBa", .has_arg = required_argument},
4878 { .val = LFS_NEWERXY_OPT,
4879 .name = "newerBm", .has_arg = required_argument},
4880 { .val = LFS_NEWERXY_OPT,
4881 .name = "newerBc", .has_arg = required_argument},
4882 { .val = LFS_NEWERXY_OPT,
4883 .name = "newerBB", .has_arg = required_argument},
4884 { .val = LFS_NEWERXY_OPT,
4885 .name = "newerat", .has_arg = required_argument},
4886 { .val = LFS_NEWERXY_OPT,
4887 .name = "newermt", .has_arg = required_argument},
4888 { .val = LFS_NEWERXY_OPT,
4889 .name = "newerct", .has_arg = required_argument},
4890 { .val = LFS_NEWERXY_OPT,
4891 .name = "newerbt", .has_arg = required_argument},
4892 { .val = LFS_NEWERXY_OPT,
4893 .name = "newerBt", .has_arg = required_argument},
4894 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4895 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4896 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4897 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4898 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4899 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4900 { .val = 'E', .name = "component-end",
4901 .has_arg = required_argument },
4902 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4903 { .val = LFS_LAYOUT_FOREIGN_OPT,
4904 .name = "foreign", .has_arg = optional_argument},
4905 { .val = 'g', .name = "gid", .has_arg = required_argument },
4906 { .val = 'G', .name = "group", .has_arg = required_argument },
4907 { .val = 'h', .name = "help", .has_arg = no_argument },
4908 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4909 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4910 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4911 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4912 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4913 { .val = 'L', .name = "layout", .has_arg = required_argument },
4914 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4915 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4916 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4917 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4918 { .val = 'n', .name = "name", .has_arg = required_argument },
4919 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4920 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4921 { .val = 'O', .name = "obd", .has_arg = required_argument },
4922 { .val = 'O', .name = "ost", .has_arg = required_argument },
4923 { .val = LFS_FIND_PERM,
4924 .name = "perm", .has_arg = required_argument },
4925 /* no short option for pool yet, can be 'p' after 2.18 */
4926 { .val = LFS_POOL_OPT,
4927 .name = "pool", .has_arg = required_argument },
4928 { .val = '0', .name = "print0", .has_arg = no_argument },
4929 { .val = 'P', .name = "print", .has_arg = no_argument },
4930 { .val = LFS_PRINTF_OPT,
4931 .name = "printf", .has_arg = required_argument },
4932 { .val = LFS_PROJID_OPT,
4933 .name = "projid", .has_arg = required_argument },
4934 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4935 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4936 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4937 { .val = 's', .name = "size", .has_arg = required_argument },
4938 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4939 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4940 { .val = 't', .name = "type", .has_arg = required_argument },
4941 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4942 { .val = 'u', .name = "uid", .has_arg = required_argument },
4943 { .val = 'U', .name = "user", .has_arg = required_argument },
4944 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4945 { .val = 'z', .name = "extension-size",
4946 .has_arg = required_argument },
4947 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4961 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4962 while ((c = getopt_long_only(argc, argv,
4963 "-0A:b:B:c:C:D:E:g:G:hH:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:z:",
4964 long_opts, &optidx)) >= 0) {
4969 /* '!' is part of option */
4971 * when getopt_long_only() finds a string which is not
4972 * an option nor a known option argument it returns 1
4973 * in that case if we already have found pathstart and pathend
4974 * (i.e. we have the list of pathnames),
4975 * the only supported value is "!"
4977 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4978 if (!isoption && pathend != -1) {
4980 "err: %s: filename|dirname must either precede options or follow options\n",
4985 if (!isoption && pathstart == -1)
4986 pathstart = optind - 1;
4987 if (isoption && pathstart != -1 && pathend == -1)
4988 pathend = optind - 2;
4995 * unknown; opt is "!" or path component,
4996 * checking done above.
4998 if (strcmp(optarg, "!") == 0)
5002 xtime = ¶m.fp_atime;
5003 xsign = ¶m.fp_asign;
5004 param.fp_exclude_atime = !!neg_opt;
5005 /* no break, this falls through to 'B' for btime */
5009 xtime = ¶m.fp_btime;
5010 xsign = ¶m.fp_bsign;
5011 param.fp_exclude_btime = !!neg_opt;
5013 /* no break, this falls through to 'C' for ctime */
5017 xtime = ¶m.fp_ctime;
5018 xsign = ¶m.fp_csign;
5019 param.fp_exclude_ctime = !!neg_opt;
5021 /* no break, this falls through to 'M' for mtime */
5025 xtime = ¶m.fp_mtime;
5026 xsign = ¶m.fp_msign;
5027 param.fp_exclude_mtime = !!neg_opt;
5029 rc = set_time(¶m, &t, xtime, optarg);
5030 if (rc == LONG_MAX) {
5038 if (optarg[0] == '+') {
5039 param.fp_blocks_sign = -1;
5041 } else if (optarg[0] == '-') {
5042 param.fp_blocks_sign = 1;
5046 param.fp_blocks_units = 1024;
5047 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5048 ¶m.fp_blocks_units, 0);
5050 fprintf(stderr, "error: bad blocks '%s'\n",
5054 param.fp_check_blocks = 1;
5055 param.fp_exclude_blocks = !!neg_opt;
5057 case LFS_COMP_COUNT_OPT:
5058 if (optarg[0] == '+') {
5059 param.fp_comp_count_sign = -1;
5061 } else if (optarg[0] == '-') {
5062 param.fp_comp_count_sign = 1;
5067 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5068 if (errno != 0 || *endptr != '\0' ||
5069 param.fp_comp_count > UINT32_MAX) {
5071 "error: bad component count '%s'\n",
5075 param.fp_check_comp_count = 1;
5076 param.fp_exclude_comp_count = !!neg_opt;
5078 case LFS_COMP_FLAGS_OPT:
5079 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5080 ¶m.fp_comp_neg_flags);
5083 "error: bad component flags '%s'\n",
5087 param.fp_check_comp_flags = 1;
5089 __u32 flags = param.fp_comp_neg_flags;
5091 param.fp_comp_neg_flags = param.fp_comp_flags;
5092 param.fp_comp_flags = flags;
5095 case LFS_COMP_START_OPT:
5096 if (optarg[0] == '+') {
5097 param.fp_comp_start_sign = -1;
5099 } else if (optarg[0] == '-') {
5100 param.fp_comp_start_sign = 1;
5104 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5105 ¶m.fp_comp_start_units, 0);
5108 "error: bad component start '%s'\n",
5112 param.fp_check_comp_start = 1;
5113 param.fp_exclude_comp_start = !!neg_opt;
5115 case LFS_MIRROR_STATE_OPT:
5116 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5117 ¶m.fp_mirror_neg_state);
5120 "error: bad mirrored file state '%s'\n",
5124 param.fp_check_mirror_state = 1;
5126 __u16 state = param.fp_mirror_neg_state;
5128 param.fp_mirror_neg_state =
5129 param.fp_mirror_state;
5130 param.fp_mirror_state = state;
5134 if (optarg[0] == '+') {
5135 param.fp_stripe_count_sign = -1;
5137 } else if (optarg[0] == '-') {
5138 param.fp_stripe_count_sign = 1;
5143 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5144 if (errno != 0 || *endptr != '\0' ||
5145 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5147 "error: bad stripe_count '%s'\n",
5152 param.fp_check_stripe_count = 1;
5153 param.fp_exclude_stripe_count = !!neg_opt;
5157 param.fp_max_depth = strtol(optarg, 0, 0);
5158 if (errno != 0 || param.fp_max_depth < 0) {
5160 "error: bad maxdepth '%s'\n",
5167 if (optarg[0] == '+') {
5168 param.fp_comp_end_sign = -1;
5170 } else if (optarg[0] == '-') {
5171 param.fp_comp_end_sign = 1;
5175 if (arg_is_eof(optarg)) {
5176 param.fp_comp_end = LUSTRE_EOF;
5177 param.fp_comp_end_units = 1;
5180 rc = llapi_parse_size(optarg,
5182 ¶m.fp_comp_end_units, 0);
5183 /* assume units of KB if too small */
5184 if (param.fp_comp_end < 4096)
5185 param.fp_comp_end *= 1024;
5189 "error: bad component end '%s'\n",
5193 param.fp_check_comp_end = 1;
5194 param.fp_exclude_comp_end = !!neg_opt;
5196 case LFS_LAYOUT_FOREIGN_OPT: {
5197 /* all types by default */
5198 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5201 /* check pure numeric */
5202 type = strtoul(optarg, &endptr, 0);
5205 type = check_foreign_type_name(optarg);
5206 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5208 "%s %s: unknown foreign type '%s'\n",
5213 } else if (type >= UINT32_MAX) {
5215 "%s %s: invalid foreign type '%s'\n",
5216 progname, argv[0], optarg);
5220 param.fp_foreign_type = type;
5221 param.fp_check_foreign = 1;
5222 param.fp_exclude_foreign = !!neg_opt;
5225 case LFS_NEWERXY_OPT: {
5231 time_t ref = time(NULL);
5233 /* no need to check bad options, they won't get here */
5234 if (strlen(long_opts[optidx].name) == 7) {
5235 x = long_opts[optidx].name[5];
5236 y = long_opts[optidx].name[6];
5240 static const char *const fmts[] = {
5241 "%Y-%m-%d %H:%M:%S",
5244 "%H:%M:%S", /* sometime today */
5253 for (i = 0; fmts[i] != NULL; i++) {
5256 /* Init for times relative to today */
5257 if (strncmp(fmts[i], "%H", 2) == 0) {
5258 localtime_r(&ref, &tm);
5260 memset(&tm, 0, sizeof(tm));
5263 ptr = strptime(optarg, fmts[i], &tm);
5265 while (ptr && isspace(*ptr))
5267 if (ptr == optarg + strlen(optarg)) {
5275 "%s: invalid time '%s'\n",
5278 "supported formats are:\n ");
5279 for (i = 0; fmts[i] != NULL; i++)
5280 fprintf(stderr, "'%s', ",
5282 fprintf(stderr, "\n");
5288 } else if (y == 'b' || y == 'B') {
5291 rc = llapi_get_lum_file(optarg, NULL, &stx,
5293 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5294 if (!(stx.stx_mask & STATX_BTIME))
5299 "%s: get btime failed '%s': %s\n",
5305 ref = stx.stx_btime.tv_sec;
5307 struct stat statbuf;
5309 if (stat(optarg, &statbuf) < 0) {
5311 "%s: cannot stat file '%s': %s\n",
5320 ref = statbuf.st_atime;
5323 ref = statbuf.st_mtime;
5326 ref = statbuf.st_ctime;
5330 "%s: invalid Y argument: '%c'\n",
5339 xidx = NEWERXY_ATIME;
5342 xidx = NEWERXY_MTIME;
5345 xidx = NEWERXY_CTIME;
5349 xidx = NEWERXY_BTIME;
5353 "%s: invalid X argument: '%c'\n",
5360 newery = ¶m.fp_newery[xidx][negidx];
5366 *newery = *newery > ref ? ref : *newery;
5368 *newery = *newery > ref ? *newery : ref;
5370 param.fp_newerxy = 1;
5375 rc = name2gid(¶m.fp_gid, optarg);
5377 if (str2quotaid(¶m.fp_gid, optarg)) {
5379 "Group/GID: %s cannot be found.\n",
5385 param.fp_exclude_gid = !!neg_opt;
5386 param.fp_check_gid = 1;
5389 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5390 ¶m.fp_hash_exflags,
5391 ¶m.fp_hash_type);
5396 if (param.fp_hash_inflags || param.fp_hash_exflags)
5397 param.fp_check_hash_flag = 1;
5398 param.fp_exclude_hash_type = !!neg_opt;
5404 ret = name2layout(¶m.fp_layout, optarg);
5407 param.fp_exclude_layout = !!neg_opt;
5408 param.fp_check_layout = 1;
5412 rc = name2uid(¶m.fp_uid, optarg);
5414 if (str2quotaid(¶m.fp_uid, optarg)) {
5416 "User/UID: %s cannot be found.\n",
5422 param.fp_exclude_uid = !!neg_opt;
5423 param.fp_check_uid = 1;
5426 param.fp_pattern = (char *)optarg;
5427 param.fp_exclude_pattern = !!neg_opt;
5430 if (optarg[0] == '+') {
5431 param.fp_mirror_count_sign = -1;
5433 } else if (optarg[0] == '-') {
5434 param.fp_mirror_count_sign = 1;
5439 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5440 if (errno != 0 || *endptr != '\0' ||
5441 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5443 "error: bad mirror count '%s'\n",
5447 param.fp_check_mirror_count = 1;
5448 param.fp_exclude_mirror_count = !!neg_opt;
5453 char *buf, *token, *next, *p;
5457 buf = strdup(optarg);
5463 param.fp_exclude_obd = !!neg_opt;
5466 while (token && *token) {
5467 token = strchr(token, ',');
5474 param.fp_exclude_mdt = !!neg_opt;
5475 param.fp_num_alloc_mdts += len;
5476 tmp = realloc(param.fp_mdt_uuid,
5477 param.fp_num_alloc_mdts *
5478 sizeof(*param.fp_mdt_uuid));
5484 param.fp_mdt_uuid = tmp;
5486 param.fp_exclude_obd = !!neg_opt;
5487 param.fp_num_alloc_obds += len;
5488 tmp = realloc(param.fp_obd_uuid,
5489 param.fp_num_alloc_obds *
5490 sizeof(*param.fp_obd_uuid));
5496 param.fp_obd_uuid = tmp;
5498 for (token = buf; token && *token; token = next) {
5499 struct obd_uuid *puuid;
5503 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5506 ¶m.fp_obd_uuid[param.fp_num_obds++];
5508 p = strchr(token, ',');
5515 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5520 strncpy(puuid->uuid, token,
5521 sizeof(puuid->uuid));
5528 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5532 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5534 "Pool name %s is too long (max %d)\n",
5535 optarg, LOV_MAXPOOLNAME);
5540 * We do check for empty pool because empty pool
5541 * is used to find V1 LOV attributes
5543 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5544 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5545 param.fp_exclude_pool = !!neg_opt;
5546 param.fp_check_pool = 1;
5548 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5549 case 'p': /* want this for --pool, to match getstripe/find */
5551 "warning: -p deprecated, use --print0 or -0\n");
5554 param.fp_zero_end = 1;
5556 case 'P': /* we always print, this option is a no-op */
5558 case LFS_PRINTF_OPT:
5559 param.fp_format_printf_str = optarg;
5561 case LFS_PROJID_OPT:
5562 rc = name2projid(¶m.fp_projid, optarg);
5564 if (str2quotaid(¶m.fp_projid, optarg)) {
5566 "Invalid project ID: %s\n",
5572 param.fp_exclude_projid = !!neg_opt;
5573 param.fp_check_projid = 1;
5576 if (optarg[0] == '+') {
5577 param.fp_size_sign = -1;
5579 } else if (optarg[0] == '-') {
5580 param.fp_size_sign = 1;
5584 ret = llapi_parse_size(optarg, ¶m.fp_size,
5585 ¶m.fp_size_units, 0);
5587 fprintf(stderr, "error: bad file size '%s'\n",
5591 param.fp_check_size = 1;
5592 param.fp_exclude_size = !!neg_opt;
5595 if (optarg[0] == '+') {
5596 param.fp_stripe_size_sign = -1;
5598 } else if (optarg[0] == '-') {
5599 param.fp_stripe_size_sign = 1;
5603 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5604 ¶m.fp_stripe_size_units, 0);
5605 /* assume units of KB if too small to be valid */
5606 if (param.fp_stripe_size < 4096)
5607 param.fp_stripe_size *= 1024;
5609 fprintf(stderr, "error: bad stripe_size '%s'\n",
5613 param.fp_check_stripe_size = 1;
5614 param.fp_exclude_stripe_size = !!neg_opt;
5617 param.fp_exclude_type = !!neg_opt;
5618 switch (optarg[0]) {
5620 param.fp_type = S_IFBLK;
5623 param.fp_type = S_IFCHR;
5626 param.fp_type = S_IFDIR;
5629 param.fp_type = S_IFREG;
5632 param.fp_type = S_IFLNK;
5635 param.fp_type = S_IFIFO;
5638 param.fp_type = S_IFSOCK;
5641 fprintf(stderr, "%s: bad type '%s'\n",
5648 param.fp_exclude_perm = !!neg_opt;
5649 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5650 if (*optarg == '/') {
5651 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5653 } else if (*optarg == '-') {
5654 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5658 if (str2mode_t(optarg, ¶m.fp_perm)) {
5659 fprintf(stderr, "error: invalid mode '%s'\n",
5666 if (optarg[0] == '+') {
5667 param.fp_mdt_count_sign = -1;
5669 } else if (optarg[0] == '-') {
5670 param.fp_mdt_count_sign = 1;
5675 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5676 if (errno != 0 || *endptr != '\0' ||
5677 param.fp_mdt_count >= UINT32_MAX) {
5678 fprintf(stderr, "error: bad mdt_count '%s'\n",
5683 param.fp_check_mdt_count = 1;
5684 param.fp_exclude_mdt_count = !!neg_opt;
5687 if (optarg[0] == '+') {
5688 param.fp_ext_size_sign = -1;
5690 } else if (optarg[0] == '-') {
5691 param.fp_ext_size_sign = 1;
5695 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5696 ¶m.fp_ext_size_units, 0);
5698 fprintf(stderr, "error: bad ext-size '%s'\n",
5702 param.fp_ext_size /= SEL_UNIT_SIZE;
5703 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5704 param.fp_check_ext_size = 1;
5705 param.fp_exclude_ext_size = !!neg_opt;
5708 fprintf(stderr, "%s: unrecognized option '%s'\n",
5709 progname, argv[optind - 1]);
5715 if (!param.fp_verbose)
5716 param.fp_verbose = VERBOSE_DEFAULT;
5718 if (pathstart == -1) {
5719 fprintf(stderr, "error: %s: no filename|pathname\n",
5723 } else if (pathend == -1) {
5729 rc = llapi_find(argv[pathstart], ¶m);
5732 pathbad = pathstart;
5734 } while (++pathstart < pathend);
5737 fprintf(stderr, "%s: failed for '%s': %s\n",
5738 progname, argv[pathbad], strerror(-rc));
5741 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5742 free(param.fp_obd_uuid);
5744 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5745 free(param.fp_mdt_uuid);
5750 static int lfs_getstripe_internal(int argc, char **argv,
5751 struct find_param *param)
5753 struct option long_opts[] = {
5754 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5755 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5756 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5757 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5758 { .val = LFS_COMP_COUNT_OPT,
5759 .name = "comp-count", .has_arg = no_argument },
5760 { .val = LFS_COMP_COUNT_OPT,
5761 .name = "component-count", .has_arg = no_argument },
5762 { .val = LFS_COMP_FLAGS_OPT,
5763 .name = "comp-flags", .has_arg = optional_argument },
5764 { .val = LFS_COMP_FLAGS_OPT,
5765 .name = "component-flags", .has_arg = optional_argument },
5766 { .val = LFS_COMP_START_OPT,
5767 .name = "comp-start", .has_arg = optional_argument },
5768 { .val = LFS_COMP_START_OPT,
5769 .name = "component-start", .has_arg = optional_argument },
5770 { .val = LFS_MIRROR_INDEX_OPT,
5771 .name = "mirror-index", .has_arg = required_argument },
5772 { .val = LFS_MIRROR_ID_OPT,
5773 .name = "mirror-id", .has_arg = required_argument },
5774 { .val = LFS_NO_FOLLOW_OPT,
5775 .name = "no-follow", .has_arg = no_argument },
5776 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5777 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5778 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5779 { .val = 'd', .name = "directory", .has_arg = no_argument },
5780 { .val = 'D', .name = "default", .has_arg = no_argument },
5781 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5782 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5783 { .val = 'F', .name = "fid", .has_arg = no_argument },
5784 { .val = 'g', .name = "generation", .has_arg = no_argument },
5785 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5786 { .val = 'h', .name = "help", .has_arg = no_argument },
5787 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5788 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5789 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5790 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5791 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5792 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5793 { .val = 'L', .name = "layout", .has_arg = no_argument },
5794 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5795 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5796 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5797 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5798 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5799 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5800 { .val = 'O', .name = "obd", .has_arg = required_argument },
5801 { .val = 'O', .name = "ost", .has_arg = required_argument },
5802 { .val = 'p', .name = "pool", .has_arg = no_argument },
5803 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5804 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5805 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5806 { .val = 'R', .name = "raw", .has_arg = no_argument },
5807 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5808 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5809 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5810 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5811 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5812 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5813 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5814 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5815 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5816 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5817 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5821 int pathstart = -1, pathend = -1;
5825 while ((c = getopt_long(argc, argv,
5826 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5827 long_opts, NULL)) != -1) {
5831 /* '!' is part of option */
5832 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5833 if (!isoption && pathend != -1) {
5835 "error: %s: filename|dirname must either precede options or follow options\n",
5839 if (!isoption && pathstart == -1)
5840 pathstart = optind - 1;
5841 if (isoption && pathstart != -1 && pathend == -1)
5842 pathend = optind - 2;
5846 /* unknown: opt is "!" */
5847 if (strcmp(optarg, "!") == 0)
5851 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5852 param->fp_verbose |= VERBOSE_COUNT;
5853 param->fp_max_depth = 0;
5856 case LFS_COMP_COUNT_OPT:
5857 param->fp_verbose |= VERBOSE_COMP_COUNT;
5858 param->fp_max_depth = 0;
5860 case LFS_COMP_FLAGS_OPT:
5862 rc = comp_str2flags(optarg,
5863 ¶m->fp_comp_flags,
5864 ¶m->fp_comp_neg_flags);
5867 "error: %s bad component flags '%s'.\n",
5871 param->fp_check_comp_flags = 1;
5873 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5874 param->fp_max_depth = 0;
5877 case LFS_COMP_START_OPT:
5880 if (tmp[0] == '+') {
5881 param->fp_comp_start_sign = -1;
5883 } else if (tmp[0] == '-') {
5884 param->fp_comp_start_sign = 1;
5887 rc = llapi_parse_size(tmp,
5888 ¶m->fp_comp_start,
5889 ¶m->fp_comp_start_units, 0);
5892 "error: %s bad component start '%s'.\n",
5896 param->fp_check_comp_start = 1;
5898 param->fp_verbose |= VERBOSE_COMP_START;
5899 param->fp_max_depth = 0;
5902 case LFS_MIRROR_INDEX_OPT: {
5903 unsigned long int mirror_index;
5905 if (optarg[0] == '+') {
5906 param->fp_mirror_index_sign = -1;
5908 } else if (optarg[0] == '-') {
5909 param->fp_mirror_index_sign = 1;
5914 mirror_index = strtoul(optarg, &end, 0);
5915 if (errno != 0 || *end != '\0' ||
5916 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5917 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5919 "%s %s: invalid mirror index '%s'\n",
5920 progname, argv[0], optarg);
5924 param->fp_mirror_index = (__u16)mirror_index;
5926 if (param->fp_mirror_id != 0) {
5928 "%s %s: can't specify both mirror index and mirror ID\n",
5932 param->fp_check_mirror_index = 1;
5933 param->fp_exclude_mirror_index = !!neg_opt;
5936 case LFS_MIRROR_ID_OPT: {
5937 unsigned long int mirror_id;
5939 if (optarg[0] == '+') {
5940 param->fp_mirror_id_sign = -1;
5942 } else if (optarg[0] == '-') {
5943 param->fp_mirror_id_sign = 1;
5948 mirror_id = strtoul(optarg, &end, 0);
5949 if (errno != 0 || *end != '\0' ||
5950 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5951 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5953 "%s %s: invalid mirror ID '%s'\n",
5954 progname, argv[0], optarg);
5958 param->fp_mirror_id = (__u16)mirror_id;
5960 if (param->fp_mirror_index != 0) {
5962 "%s %s: can't specify both mirror index and mirror ID\n",
5966 param->fp_check_mirror_id = 1;
5967 param->fp_exclude_mirror_id = !!neg_opt;
5970 case LFS_NO_FOLLOW_OPT:
5971 param->fp_no_follow = true;
5974 param->fp_max_depth = 0;
5977 param->fp_get_default_lmv = 1;
5982 if (tmp[0] == '+') {
5983 param->fp_comp_end_sign = -1;
5985 } else if (tmp[0] == '-') {
5986 param->fp_comp_end_sign = 1;
5990 if (arg_is_eof(tmp)) {
5991 param->fp_comp_end = LUSTRE_EOF;
5992 param->fp_comp_end_units = 1;
5995 rc = llapi_parse_size(tmp,
5996 ¶m->fp_comp_end,
5997 ¶m->fp_comp_end_units, 0);
5998 /* assume units of KB if too small */
5999 if (param->fp_comp_end < 4096)
6000 param->fp_comp_end *= 1024;
6004 "error: %s bad component end '%s'.\n",
6008 param->fp_check_comp_end = 1;
6010 param->fp_verbose |= VERBOSE_COMP_END;
6011 param->fp_max_depth = 0;
6015 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6016 param->fp_verbose |= VERBOSE_DFID;
6017 param->fp_max_depth = 0;
6021 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6022 param->fp_verbose |= VERBOSE_GENERATION;
6023 param->fp_max_depth = 0;
6027 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6028 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6029 param->fp_max_depth = 0;
6034 param->fp_comp_id = strtoul(optarg, &end, 0);
6035 if (*end != '\0' || param->fp_comp_id == 0 ||
6036 param->fp_comp_id > LCME_ID_MAX) {
6038 "error: %s bad component id '%s'\n",
6042 param->fp_check_comp_id = 1;
6044 param->fp_max_depth = 0;
6045 param->fp_verbose |= VERBOSE_COMP_ID;
6049 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6050 param->fp_verbose |= VERBOSE_PATTERN;
6051 param->fp_max_depth = 0;
6054 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6057 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6060 if (!(param->fp_verbose & VERBOSE_DETAIL))
6061 param->fp_max_depth = 0;
6062 param->fp_verbose |= VERBOSE_MDTINDEX;
6065 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6066 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6067 param->fp_max_depth = 0;
6071 if (param->fp_obd_uuid) {
6073 "error: %s: only one obduuid allowed",
6077 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6080 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6081 param->fp_verbose |= VERBOSE_POOL;
6082 param->fp_max_depth = 0;
6089 param->fp_recursive = 1;
6095 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6096 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6097 param->fp_max_depth = 0;
6101 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6107 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6108 param->fp_verbose |= VERBOSE_EXT_SIZE;
6109 param->fp_max_depth = 0;
6113 fprintf(stderr, "%s: unrecognized option '%s'\n",
6114 progname, argv[optind - 1]);
6120 if (pathstart == -1) {
6121 fprintf(stderr, "error: %s: no filename|pathname\n",
6124 } else if (pathend == -1) {
6132 if (param->fp_recursive)
6133 param->fp_max_depth = -1;
6134 else if (param->fp_verbose & VERBOSE_DETAIL)
6135 param->fp_max_depth = 1;
6137 if (!param->fp_verbose)
6138 param->fp_verbose = VERBOSE_DEFAULT;
6139 if (param->fp_quiet)
6140 param->fp_verbose = VERBOSE_OBJID;
6143 rc = llapi_getstripe(argv[pathstart], param);
6144 } while (++pathstart < pathend && !rc);
6147 fprintf(stderr, "error: %s failed for %s.\n",
6148 argv[0], argv[optind - 1]);
6152 static int lfs_tgts(int argc, char **argv)
6154 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6155 struct find_param param;
6156 int index = 0, rc = 0;
6161 if (argc == 2 && !realpath(argv[1], path)) {
6163 fprintf(stderr, "error: invalid path '%s': %s\n",
6164 argv[1], strerror(-rc));
6168 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6169 /* Check if we have a mount point */
6170 if (mntdir[0] == '\0')
6173 memset(¶m, 0, sizeof(param));
6174 if (!strcmp(argv[0], "mdts"))
6175 param.fp_get_lmv = 1;
6177 rc = llapi_ostlist(mntdir, ¶m);
6179 fprintf(stderr, "error: %s: failed on %s\n",
6182 if (path[0] != '\0')
6184 memset(mntdir, 0, PATH_MAX);
6190 static int lfs_getstripe(int argc, char **argv)
6192 struct find_param param = { 0 };
6194 param.fp_max_depth = 1;
6195 return lfs_getstripe_internal(argc, argv, ¶m);
6199 static int lfs_getdirstripe(int argc, char **argv)
6201 struct find_param param = { 0 };
6202 struct option long_opts[] = {
6203 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6204 { .val = 'D', .name = "default", .has_arg = no_argument },
6205 { .val = 'h', .name = "help", .has_arg = no_argument },
6206 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6207 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6208 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6209 { .val = 'O', .name = "obd", .has_arg = required_argument },
6210 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6211 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6212 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6213 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6214 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6215 { .val = LFS_INHERIT_RR_OPT,
6216 .name = "max-inherit-rr", .has_arg = no_argument },
6220 param.fp_get_lmv = 1;
6222 while ((c = getopt_long(argc, argv,
6223 "cDhHimO:rtTvXy", long_opts, NULL)) != -1) {
6227 param.fp_verbose |= VERBOSE_COUNT;
6230 param.fp_get_default_lmv = 1;
6232 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6235 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6239 param.fp_verbose |= VERBOSE_HASH_TYPE;
6244 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6247 if (param.fp_obd_uuid) {
6249 "%s: only one obduuid allowed",
6253 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6256 param.fp_recursive = 1;
6259 param.fp_verbose |= VERBOSE_DETAIL;
6262 param.fp_verbose |= VERBOSE_INHERIT;
6264 case LFS_INHERIT_RR_OPT:
6265 param.fp_verbose |= VERBOSE_INHERIT_RR;
6271 fprintf(stderr, "%s: unrecognized option '%s'\n",
6272 progname, argv[optind - 1]);
6282 if (param.fp_recursive)
6283 param.fp_max_depth = -1;
6285 if (!param.fp_verbose)
6286 param.fp_verbose = VERBOSE_DEFAULT;
6289 rc = llapi_getstripe(argv[optind], ¶m);
6290 } while (++optind < argc && !rc);
6293 fprintf(stderr, "error: %s failed for %s.\n",
6294 argv[0], argv[optind - 1]);
6299 MNTDF_INODES = 0x0001,
6300 MNTDF_COOKED = 0x0002,
6301 MNTDF_LAZY = 0x0004,
6302 MNTDF_VERBOSE = 0x0008,
6303 MNTDF_SHOW = 0x0010,
6304 MNTDF_DECIMAL = 0x0020,
6307 #define COOK(value, base) \
6310 while (value > base) { \
6318 #define CDF "%11llu"
6319 #define HDF "%8.1f%c"
6323 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6325 double avail, used, ratio = 0;
6328 avail = st->os_ffree;
6329 used = st->os_files - st->os_ffree;
6331 avail = st->os_bavail;
6332 used = st->os_blocks - st->os_bfree;
6334 if (avail + used > 0)
6335 ratio = used / (used + avail) * 100;
6337 /* Round up to match df(1) usage percentage */
6338 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6342 * This is to identify various problem states for "lfs df" if .osn_err = true,
6343 * so only show flags reflecting those states by default. Informational states
6344 * are only shown with "-v" and use lower-case names to distinguish them.
6345 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6347 static struct obd_statfs_state_names {
6348 enum obd_statfs_state osn_state;
6349 const char osn_name;
6352 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6353 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6354 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6355 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6356 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6357 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6358 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6359 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6360 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6363 static int showdf(char *mntdir, struct obd_statfs *stat,
6364 char *uuid, enum mntdf_flags flags,
6365 char *type, int index, int rc)
6367 long long avail, used, total;
6369 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6370 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6371 char tbuf[3 * sizeof(__u64)];
6372 char ubuf[3 * sizeof(__u64)];
6373 char abuf[3 * sizeof(__u64)];
6374 char rbuf[3 * sizeof(__u64)];
6381 if (flags & MNTDF_INODES) {
6382 avail = stat->os_ffree;
6383 used = stat->os_files - stat->os_ffree;
6384 total = stat->os_files;
6386 int shift = flags & MNTDF_COOKED ? 0 : 10;
6388 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6389 used = ((stat->os_blocks - stat->os_bfree) *
6390 stat->os_bsize) >> shift;
6391 total = (stat->os_blocks * stat->os_bsize) >> shift;
6394 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6396 if (flags & MNTDF_COOKED) {
6397 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6401 cook_val = (double)total;
6402 i = COOK(cook_val, base);
6404 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6407 snprintf(tbuf, sizeof(tbuf), CDF, total);
6409 cook_val = (double)used;
6410 i = COOK(cook_val, base);
6412 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6415 snprintf(ubuf, sizeof(ubuf), CDF, used);
6417 cook_val = (double)avail;
6418 i = COOK(cook_val, base);
6420 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6423 snprintf(abuf, sizeof(abuf), CDF, avail);
6425 snprintf(tbuf, sizeof(tbuf), CDF, total);
6426 snprintf(ubuf, sizeof(tbuf), CDF, used);
6427 snprintf(abuf, sizeof(tbuf), CDF, avail);
6430 sprintf(rbuf, RDF, ratio);
6431 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6432 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6434 printf("[%s:%d]", type, index);
6436 if (stat->os_state) {
6440 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6441 if (oss_names[i].osn_state & stat->os_state &&
6442 (oss_names[i].osn_err ||
6443 flags & MNTDF_VERBOSE))
6444 printf("%c", oss_names[i].osn_name);
6451 printf(UUF": inactive device\n", uuid);
6454 printf(UUF": %s\n", uuid, strerror(-rc));
6461 struct ll_stat_type {
6466 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6468 struct ll_statfs_data {
6470 struct obd_statfs sd_st;
6473 struct ll_statfs_buf {
6475 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6478 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6479 int ops, struct ll_statfs_buf *lsb)
6481 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6482 struct obd_uuid uuid_buf;
6483 char *poolname = NULL;
6484 struct ll_stat_type types[] = {
6485 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6486 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6487 { .st_name = NULL } };
6488 struct ll_stat_type *tp;
6489 __u64 ost_files = 0;
6490 __u64 ost_ffree = 0;
6498 poolname = strchr(pool, '.');
6500 if (strncmp(fsname, pool, strlen(fsname))) {
6501 fprintf(stderr, "filesystem name incorrect\n");
6509 fd = open(mntdir, O_RDONLY);
6512 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6517 if (flags & MNTDF_SHOW) {
6518 if (flags & MNTDF_INODES)
6519 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6520 "UUID", "Inodes", "IUsed", "IFree",
6521 "IUse%", "Mounted on");
6523 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6525 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6526 "Used", "Available", "Use%", "Mounted on");
6529 for (tp = types; tp->st_name != NULL; tp++) {
6530 bool have_ost = false;
6532 if (!(tp->st_op & ops))
6535 for (index = 0; index < LOV_ALL_STRIPES &&
6536 (!lsb || lsb->sb_count < LL_STATFS_MAX); index++) {
6537 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6538 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6539 type = flags & MNTDF_LAZY ?
6540 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6541 rc2 = llapi_obd_fstatfs(fd, type, index,
6542 &stat_buf, &uuid_buf);
6547 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6548 if (!(flags & MNTDF_VERBOSE))
6550 } else if (rc2 < 0 && rc == 0) {
6555 * If we have OSTs then don't report MDT block counts.
6556 * For MDT-only filesystems the expectation is that all
6557 * layouts have a DoM component. For filesystems with
6558 * OSTs, files are not necessarily going to store data
6559 * on MDTs, and MDT space is limited to a fraction of
6560 * OST space, so don't include it in the summary.
6562 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6569 if (poolname && tp->st_op == LL_STATFS_LOV &&
6570 llapi_search_ost(fsname, poolname,
6571 obd_uuid2str(&uuid_buf)) != 1)
6575 * the llapi_obd_fstatfs() call may have returned with
6576 * an error, but if it filled in uuid_buf we will at
6577 * lease use that to print out a message for that OBD.
6578 * If we didn't get anything in the uuid_buf, then fill
6579 * it in so that we can print an error message.
6581 if (uuid_buf.uuid[0] == '\0')
6582 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6583 "%s%04x", tp->st_name, index);
6585 lsb->sb_buf[lsb->sb_count].sd_index = index;
6586 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6589 if (flags & MNTDF_SHOW)
6590 showdf(mntdir, &stat_buf,
6591 obd_uuid2str(&uuid_buf), flags,
6592 tp->st_name, index, rc2);
6597 if (tp->st_op == LL_STATFS_LMV) {
6598 sum.os_ffree += stat_buf.os_ffree;
6599 sum.os_files += stat_buf.os_files;
6600 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6601 ost_files += stat_buf.os_files;
6602 ost_ffree += stat_buf.os_ffree;
6604 sum.os_blocks += stat_buf.os_blocks *
6606 sum.os_bfree += stat_buf.os_bfree *
6608 sum.os_bavail += stat_buf.os_bavail *
6616 * If we have _some_ OSTs, but don't have as many free objects on the
6617 * OST as inodes on the MDTs, reduce the reported number of inodes
6618 * to compensate, so that the "inodes in use" number is correct.
6619 * This should be kept in sync with ll_statfs_internal().
6621 if (ost_files && ost_ffree < sum.os_ffree) {
6622 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6623 sum.os_ffree = ost_ffree;
6625 if (flags & MNTDF_SHOW) {
6627 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6635 LAYOUT_INHERIT_UNSET = -2,
6639 static int lfs_setdirstripe(int argc, char **argv)
6642 struct lfs_setstripe_args lsa = { 0 };
6643 struct llapi_stripe_param *param = NULL;
6644 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6647 char *mode_opt = NULL;
6648 bool default_stripe = false;
6649 bool delete = false;
6650 bool foreign_mode = false;
6651 bool mdt_count_set = false;
6652 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6653 mode_t previous_mode = 0;
6655 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6656 int max_inherit = LAYOUT_INHERIT_UNSET;
6657 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6658 struct option long_opts[] = {
6659 { .val = 'c', .name = "count", .has_arg = required_argument },
6660 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6661 { .val = 'd', .name = "delete", .has_arg = no_argument },
6662 { .val = 'D', .name = "default", .has_arg = no_argument },
6663 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6664 { .val = LFS_LAYOUT_FLAGS_OPT,
6665 .name = "flags", .has_arg = required_argument },
6666 { .val = LFS_LAYOUT_FOREIGN_OPT,
6667 .name = "foreign", .has_arg = optional_argument},
6668 { .val = 'h', .name = "help", .has_arg = no_argument },
6669 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6670 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6671 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6672 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6674 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6675 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6676 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6678 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6679 { .val = 'i', .name = "index", .has_arg = required_argument },
6681 { .val = 'o', .name = "mode", .has_arg = required_argument },
6682 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6683 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6685 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6686 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6687 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
6688 { .val = LFS_INHERIT_RR_OPT,
6689 .name = "max-inherit-rr", .has_arg = required_argument},
6690 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6694 setstripe_args_init(&lsa);
6696 while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
6697 long_opts, NULL)) >= 0) {
6705 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6706 if (errno != 0 || *end != '\0' ||
6707 lsa.lsa_stripe_count < -1 ||
6708 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6710 "%s: invalid stripe count '%s'\n",
6714 mdt_count_set = true;
6718 default_stripe = true;
6721 default_stripe = true;
6723 case LFS_LAYOUT_FOREIGN_OPT:
6725 /* check pure numeric */
6726 type = strtoul(optarg, &end, 0);
6729 type = check_foreign_type_name(optarg);
6730 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6732 "%s %s: unknown foreign type '%s'\n",
6737 } else if (type >= UINT32_MAX) {
6739 "%s %s: invalid foreign type '%s'\n",
6740 progname, argv[0], optarg);
6744 foreign_mode = true;
6746 case LFS_LAYOUT_FLAGS_OPT:
6748 flags = strtoul(optarg, &end, 16);
6749 if (errno != 0 || *end != '\0' ||
6750 flags >= UINT32_MAX) {
6752 "%s %s: invalid hex flags '%s'\n",
6753 progname, argv[0], optarg);
6756 if (!foreign_mode) {
6758 "%s %s: hex flags must be specified with --foreign option\n",
6763 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6766 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6770 lsa.lsa_pattern = check_hashtype(optarg);
6771 if (lsa.lsa_pattern == 0) {
6773 "%s %s: bad directory hash type '%s'\n",
6774 progname, argv[0], optarg);
6779 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6782 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6783 if (strcmp(argv[optind - 1], "--index") == 0)
6785 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6788 lsa.lsa_nr_tgts = parse_targets(mdts,
6789 sizeof(mdts) / sizeof(__u32),
6790 lsa.lsa_nr_tgts, optarg, NULL);
6791 if (lsa.lsa_nr_tgts < 0) {
6793 "%s %s: invalid MDT target(s) '%s'\n",
6794 progname, argv[0], optarg);
6798 lsa.lsa_tgts = mdts;
6799 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6800 lsa.lsa_stripe_off = mdts[0];
6802 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6805 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6815 max_inherit = strtol(optarg, &end, 10);
6816 if (errno != 0 || *end != '\0' || max_inherit < -2) {
6818 "%s %s: invalid max-inherit '%s'\n",
6819 progname, argv[0], optarg);
6822 if (max_inherit == 0) {
6823 max_inherit = LMV_INHERIT_NONE;
6824 } else if (max_inherit == -1) {
6825 max_inherit = LMV_INHERIT_UNLIMITED;
6826 } else if (max_inherit > LMV_INHERIT_MAX) {
6828 "%s %s: max-inherit %d exceeds maximum %u\n",
6829 progname, argv[0], max_inherit,
6834 case LFS_INHERIT_RR_OPT:
6835 if (!default_stripe) {
6837 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6842 max_inherit_rr = strtol(optarg, &end, 10);
6843 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6845 "%s %s: invalid max-inherit-rr '%s'\n",
6846 progname, argv[0], optarg);
6849 if (max_inherit_rr == 0) {
6850 max_inherit_rr = LMV_INHERIT_RR_NONE;
6851 } else if (max_inherit_rr == -1) {
6852 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6853 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6855 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6856 progname, argv[0], max_inherit_rr,
6857 LMV_INHERIT_RR_MAX);
6862 fprintf(stderr, "%s: unrecognized option '%s'\n",
6863 progname, argv[optind - 1]);
6870 if (optind == argc) {
6871 fprintf(stderr, "%s %s: DIR must be specified\n",
6876 if (xattr && !foreign_mode) {
6878 * only print a warning as this is armless and will be
6882 "%s %s: xattr has been specified for non-foreign layout\n",
6884 } else if (foreign_mode && !xattr) {
6886 "%s %s: xattr must be provided in foreign mode\n",
6891 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6892 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6894 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6899 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6900 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6901 /* if no parameters set, create directory on least-used MDTs */
6902 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
6903 lsa.lsa_stripe_count = 1;
6907 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6908 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6910 "%s %s: cannot specify -d with -c or -i options\n",
6916 mode = strtoul(mode_opt, &end, 8);
6919 "%s %s: bad MODE '%s'\n",
6920 progname, argv[0], mode_opt);
6923 previous_mode = umask(0);
6926 /* check max-inherit and warn user in some cases */
6927 if (default_stripe &&
6928 (lsa.lsa_stripe_count < 0 || lsa.lsa_stripe_count > 1)) {
6929 if (max_inherit == LMV_INHERIT_UNLIMITED)
6931 "%s %s: unrecommended max-inherit=-1 when default stripe-count=%lld\n",
6932 progname, argv[0], lsa.lsa_stripe_count);
6933 else if (max_inherit > LMV_INHERIT_DEFAULT_STRIPED + 2 &&
6934 max_inherit != LMV_INHERIT_NONE)
6936 "%s %s: unrecommended max-inherit=%d when default stripe-count=%lld\n",
6937 progname, argv[0], max_inherit,
6938 lsa.lsa_stripe_count);
6941 if (default_stripe && lsa.lsa_nr_tgts > 1 && !mdt_count_set) {
6943 "%s %s: trying to create unrecommended default striped directory layout,\n"
6944 " '-D -i x,y,z' will stripe every new directory across all MDTs,\n"
6945 " add -c with the number of MDTs to do this anyway\n",
6950 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6951 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6952 lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
6954 "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
6955 progname, argv[0], lsa.lsa_stripe_off);
6959 /* foreign LMV/dir case */
6961 if (argc > optind + 1) {
6963 "%s %s: cannot specify multiple foreign dirs\n",
6968 dname = argv[optind];
6969 result = llapi_dir_create_foreign(dname, mode, type, flags,
6973 "%s mkdir: can't create foreign dir '%s': %s\n",
6974 progname, dname, strerror(-result));
6979 * initialize stripe parameters, in case param is converted to specific,
6980 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6982 param = calloc(1, offsetof(typeof(*param),
6983 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6984 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6987 "%s %s: cannot allocate memory for parameters: %s\n",
6988 progname, argv[0], strerror(ENOMEM));
6992 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6993 if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
6994 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6995 lsa.lsa_stripe_count == 0))
6996 lsa.lsa_stripe_count = 1;
6997 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6998 param->lsp_stripe_count = lsa.lsa_stripe_count;
6999 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7000 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
7002 param->lsp_stripe_offset = lsa.lsa_stripe_off;
7003 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
7004 param->lsp_stripe_pattern = lsa.lsa_pattern;
7006 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
7007 param->lsp_pool = lsa.lsa_pool_name;
7008 param->lsp_is_specific = false;
7009 if (max_inherit == LAYOUT_INHERIT_UNSET) {
7010 if (lsa.lsa_stripe_count == 0 || lsa.lsa_stripe_count == 1)
7011 max_inherit = LMV_INHERIT_DEFAULT_PLAIN;
7013 max_inherit = LMV_INHERIT_DEFAULT_STRIPED;
7015 param->lsp_max_inherit = max_inherit;
7016 if (default_stripe) {
7018 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
7019 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
7020 param->lsp_max_inherit_rr = max_inherit_rr;
7022 if (strcmp(argv[0], "mkdir") == 0)
7023 param->lsp_is_create = true;
7024 if (lsa.lsa_nr_tgts > 1) {
7025 if (lsa.lsa_stripe_count > 0 &&
7026 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
7027 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
7029 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
7030 argv[0], lsa.lsa_stripe_count,
7036 param->lsp_is_specific = true;
7037 param->lsp_stripe_count = lsa.lsa_nr_tgts;
7038 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
7041 dname = argv[optind];
7043 if (default_stripe) {
7044 result = llapi_dir_set_default_lmv(dname, param);
7047 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7048 progname, dname, strerror(-result));
7052 result = llapi_dir_create(dname, mode, param);
7055 "%s setdirstripe: cannot create dir '%s': %s\n",
7056 progname, dname, strerror(-result));
7057 } while (!result && (dname = argv[++optind]));
7060 umask(previous_mode);
7067 static int lfs_rmentry(int argc, char **argv)
7074 fprintf(stderr, "error: %s: missing dirname\n",
7080 dname = argv[index];
7082 result = llapi_direntry_remove(dname);
7085 "error: %s: remove dir entry '%s' failed\n",
7089 dname = argv[++index];
7094 static int lfs_unlink_foreign(int argc, char **argv)
7101 fprintf(stderr, "error: %s: missing pathname\n",
7108 while (name != NULL) {
7109 result = llapi_unlink_foreign(name);
7112 "error: %s: unlink foreign entry '%s' failed\n",
7116 name = argv[++index];
7121 static int lfs_mv(int argc, char **argv)
7123 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7124 struct find_param param = {
7131 struct option long_opts[] = {
7132 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7133 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7134 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7137 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7139 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7142 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7146 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7147 if (errno != 0 || *end != '\0' ||
7148 lmu.lum_stripe_offset >= UINT32_MAX) {
7149 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7155 param.fp_verbose = VERBOSE_DETAIL;
7158 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7159 progname, argv[optind - 1]);
7164 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7165 fprintf(stderr, "%s mv: MDT index must be specified\n",
7170 if (optind >= argc) {
7171 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7175 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7177 /* initialize migrate mdt parameters */
7178 param.fp_lmv_md = &lmu;
7179 param.fp_migrate = 1;
7180 rc = llapi_migrate_mdt(argv[optind], ¶m);
7182 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7183 progname, argv[optind], lmu.lum_stripe_offset,
7188 static int lfs_osts(int argc, char **argv)
7190 return lfs_tgts(argc, argv);
7193 static int lfs_mdts(int argc, char **argv)
7195 return lfs_tgts(argc, argv);
7198 static int lfs_df(int argc, char **argv)
7200 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7201 enum mntdf_flags flags = MNTDF_SHOW;
7202 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7203 int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7204 char fsname[PATH_MAX] = "", *pool_name = NULL;
7205 struct option long_opts[] = {
7206 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7207 { .val = 'H', .name = "si", .has_arg = no_argument },
7208 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7209 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7210 { .val = 'p', .name = "pool", .has_arg = required_argument },
7211 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7214 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7217 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7220 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7223 flags |= MNTDF_INODES;
7226 flags |= MNTDF_LAZY;
7232 flags |= MNTDF_VERBOSE;
7235 fprintf(stderr, "%s: unrecognized option '%s'\n",
7236 progname, argv[optind - 1]);
7241 /* Handle case where path is not specified */
7242 if (optind == argc) {
7243 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7244 /* Check if we have a mount point */
7245 if (mntdir[0] == '\0')
7248 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7249 if (rc || path[0] != '\0')
7252 fsname[0] = '\0'; /* avoid matching in next loop */
7253 mntdir[0] = '\0'; /* avoid matching in next loop */
7254 path[0] = '\0'; /* clean for next loop */
7259 /* Loop through all the remaining arguments. These are Lustre FS
7262 for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7265 fsname[0] = '\0'; /* start clean */
7266 mntdir[0] = '\0'; /* start clean */
7267 path[0] = '\0'; /* start clean */
7269 /* path does not exists at all */
7270 if (!realpath(argv[arg_idx], path)) {
7272 fprintf(stderr, "error: invalid path '%s': %s\n",
7273 argv[arg_idx], strerror(-rc));
7274 /* save first seen error */
7281 /* path exists but may not be a Lustre filesystem */
7282 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7283 /* Check if we have a mount point */
7284 if (mntdir[0] == '\0')
7287 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7288 if (rc || path[0] != '\0') {
7291 /* save first seen error */
7299 llapi_printf(LLAPI_MSG_ERROR,
7300 "%s:%s Not a Lustre filesystem\n",
7301 argv[0], argv[arg_idx]);
7302 /* save first seen error */
7311 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7312 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7316 if (opt_fsname == opt_instance) { /* both true or both false */
7317 rc = llapi_getname(mntdir, buf, buflen);
7318 } else if (opt_fsname) {
7320 * llapi_search_mounts() fills @buf with fsname, but that is not
7321 * called if explicit paths are specified on the command-line
7324 rc = llapi_get_fsname(mntdir, buf, buflen);
7325 } else /* if (opt_instance) */ {
7326 rc = llapi_get_instance(mntdir, buf, buflen);
7330 fprintf(stderr, "cannot get instance for '%s': %s\n",
7331 mntdir, strerror(-rc));
7336 printf("%s %s\n", buf, mntdir);
7338 printf("%s\n", buf);
7343 static int lfs_getname(int argc, char **argv)
7345 struct option long_opts[] = {
7346 { .val = 'h', .name = "help", .has_arg = no_argument },
7347 { .val = 'i', .name = "instance", .has_arg = no_argument },
7348 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7350 bool opt_instance = false, opt_fsname = false;
7351 char fsname[PATH_MAX] = "";
7354 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7357 opt_instance = true;
7363 fprintf(stderr, "%s: unrecognized option '%s'\n",
7364 progname, argv[optind - 1]);
7371 if (optind == argc) { /* no paths specified, get all paths. */
7372 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7375 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7376 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7377 opt_instance, opt_fsname, true);
7380 path[0] = fsname[0] = mntdir[0] = '\0';
7382 } else { /* paths specified, only attempt to search these. */
7385 /* if only one path is given, print only requested info */
7386 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7388 for (; optind < argc; optind++) {
7389 rc2 = print_instance(argv[optind], fsname,
7390 sizeof(fsname), opt_instance,
7391 opt_fsname, opt_mntdir);
7401 static int lfs_check(int argc, char **argv)
7403 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7411 if (argc < 2 || argc > 3) {
7412 fprintf(stderr, "%s check: server type must be specified\n",
7417 obd_types[0] = obd_type1;
7418 obd_types[1] = obd_type2;
7419 obd_types[2] = obd_type3;
7421 if (strcmp(argv[1], "osts") == 0) {
7422 strcpy(obd_types[0], "osc");
7423 } else if (strcmp(argv[1], "mdts") == 0 ||
7424 strcmp(argv[1], "mds") == 0) {
7425 strcpy(obd_types[0], "mdc");
7426 } else if (strcmp(argv[1], "mgts") == 0) {
7427 strcpy(obd_types[0], "mgc");
7428 } else if (strcmp(argv[1], "all") == 0 ||
7429 strcmp(argv[1], "servers") == 0) {
7431 strcpy(obd_types[0], "osc");
7432 strcpy(obd_types[1], "mdc");
7433 strcpy(obd_types[2], "mgc");
7435 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7440 if (argc >= 3 && !realpath(argv[2], path)) {
7442 fprintf(stderr, "error: invalid path '%s': %s\n",
7443 argv[2], strerror(-rc));
7447 rc = llapi_search_mounts(path, 0, mntdir, NULL);
7448 if (rc < 0 || mntdir[0] == '\0') {
7450 "%s check: cannot find mounted Lustre filesystem: %s\n",
7451 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7455 rc = llapi_target_check(num_types, obd_types, path);
7457 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7458 progname, argv[1], strerror(-rc));
7463 #ifdef HAVE_SYS_QUOTA_H
7464 #define ADD_OVERFLOW(a, b) \
7465 ((((a) + (b)) < (a)) ? \
7466 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7468 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7469 * returns the value or ULONG_MAX on integer overflow or incorrect format
7471 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7472 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7473 * 3. empty integer value is interpreted as 0
7475 static unsigned long str2sec(const char *timestr)
7477 const char spec[] = "smhdw";
7478 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7479 unsigned long val = 0;
7482 if (strpbrk(timestr, spec) == NULL) {
7484 * no specifiers inside the time string,
7485 * should treat it as an integer value
7487 val = strtoul(timestr, &tail, 10);
7488 return *tail ? ULONG_MAX : val;
7491 /* format string is XXwXXdXXhXXmXXs */
7497 v = strtoul(timestr, &tail, 10);
7498 if (v == ULONG_MAX || *tail == '\0')
7500 * value too large (ULONG_MAX or more)
7501 * or missing specifier
7505 ptr = strchr(spec, *tail);
7507 /* unknown specifier */
7512 /* check if product will overflow the type */
7513 if (!(v < ULONG_MAX / mult[ind]))
7516 ADD_OVERFLOW(val, mult[ind] * v);
7517 if (val == ULONG_MAX)
7529 #define ARG2ULL(nr, str, def_units) \
7531 unsigned long long limit, units = def_units; \
7534 rc = llapi_parse_size(str, &limit, &units, 1); \
7536 fprintf(stderr, "%s: invalid limit '%s'\n", \
7543 static inline int has_times_option(int argc, char **argv)
7547 for (i = 1; i < argc; i++)
7548 if (!strcmp(argv[i], "-t"))
7554 static inline int lfs_verify_poolarg(char *pool)
7556 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7558 "Pool name '%.*s' is longer than %d\n",
7559 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7565 /* special grace time, only notify the user when its quota is over soft limit
7566 * but doesn't block new writes until the hard limit is reached.
7568 #define NOTIFY_GRACE "notify"
7569 #define NOTIFY_GRACE_TIME LQUOTA_GRACE_MASK
7572 static inline __u64 lustre_stoqb(size_t space)
7574 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7577 #define lustre_stoqb toqb
7580 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7583 char *mnt, *obd_type = (char *)qctl->obd_type;
7584 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7585 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7586 struct option long_opts[] = {
7587 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7588 { .val = 'g', .name = "group", .has_arg = no_argument },
7589 { .val = 'h', .name = "help", .has_arg = no_argument },
7590 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7591 { .val = 'p', .name = "projid", .has_arg = no_argument },
7592 { .val = 't', .name = "times", .has_arg = no_argument },
7593 { .val = 'u', .name = "user", .has_arg = no_argument },
7594 { .val = LFS_POOL_OPT,
7595 .name = "pool", .has_arg = required_argument },
7599 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7600 qctl->qc_type = ALLQUOTA;
7602 while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7603 long_opts, NULL)) != -1) {
7614 if (qctl->qc_type != ALLQUOTA) {
7616 "%s: -u/g/p cannot be used more than once\n",
7620 qctl->qc_type = qtype;
7623 if (strncmp(optarg, NOTIFY_GRACE,
7624 strlen(NOTIFY_GRACE)) == 0) {
7625 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7627 dqi->dqi_bgrace = str2sec(optarg);
7628 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7630 "%s: bad block-grace: %s\n",
7635 dqb->dqb_valid |= QIF_BTIME;
7638 if (strncmp(optarg, NOTIFY_GRACE,
7639 strlen(NOTIFY_GRACE)) == 0) {
7640 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7642 dqi->dqi_igrace = str2sec(optarg);
7643 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7645 "%s: bad inode-grace: %s\n",
7650 dqb->dqb_valid |= QIF_ITIME;
7652 case 't': /* Yes, of course! */
7655 if (lfs_verify_poolarg(optarg))
7657 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7658 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7660 /* getopt prints error message for us when opterr != 0 */
7662 fprintf(stderr, "%s: unrecognized option '%s'\n",
7663 progname, argv[optind - 1]);
7670 if (qctl->qc_type == ALLQUOTA) {
7671 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7676 if (optind != argc - 1) {
7677 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7678 progname, argv[optind + 1]);
7683 rc = llapi_quotactl(mnt, qctl);
7686 fprintf(stderr, "%s %s ", obd_type,
7687 obd_uuid2str(&qctl->obd_uuid));
7688 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7695 #define BSLIMIT (1 << 0)
7696 #define BHLIMIT (1 << 1)
7697 #define ISLIMIT (1 << 2)
7698 #define IHLIMIT (1 << 3)
7700 int lfs_setquota(int argc, char **argv)
7703 struct if_quotactl *qctl;
7704 char *mnt, *obd_type;
7705 struct obd_dqblk *dqb;
7706 struct option long_opts[] = {
7707 { .val = 'b', .name = "block-softlimit",
7708 .has_arg = required_argument },
7709 { .val = 'B', .name = "block-hardlimit",
7710 .has_arg = required_argument },
7711 { .val = 'd', .name = "default", .has_arg = no_argument },
7712 { .val = LFS_SETQUOTA_DELETE,
7713 .name = "delete", .has_arg = no_argument },
7714 { .val = 'g', .name = "group", .has_arg = required_argument },
7715 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7716 { .val = 'h', .name = "help", .has_arg = no_argument },
7717 { .val = 'i', .name = "inode-softlimit",
7718 .has_arg = required_argument },
7719 { .val = 'I', .name = "inode-hardlimit",
7720 .has_arg = required_argument },
7721 { .val = 'p', .name = "projid", .has_arg = required_argument },
7722 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7723 { .val = 'u', .name = "user", .has_arg = required_argument },
7724 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7725 { .val = LFS_POOL_OPT,
7726 .name = "pool", .has_arg = required_argument },
7728 unsigned int limit_mask = 0;
7729 bool use_default = false;
7730 int qtype, qctl_len;
7732 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7733 qctl = malloc(qctl_len);
7737 memset(qctl, 0, qctl_len);
7738 obd_type = (char *)qctl->obd_type;
7739 dqb = &qctl->qc_dqblk;
7741 if (has_times_option(argc, argv)) {
7742 rc = lfs_setquota_times(argc, argv, qctl);
7746 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7747 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7748 * so it can be used as a marker that qc_type
7749 * isn't reinitialized from command line
7751 while ((c = getopt_long(argc, argv, "b:B:dDg:Ghi:I:p:Pu:U",
7752 long_opts, NULL)) != -1) {
7755 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7758 goto quota_type_def;
7761 rc = name2uid(&qctl->qc_id, optarg);
7764 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7767 goto quota_type_def;
7770 rc = name2gid(&qctl->qc_id, optarg);
7773 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7776 goto quota_type_def;
7779 rc = name2projid(&qctl->qc_id, optarg);
7782 if (str2quotaid(&qctl->qc_id, optarg)) {
7784 "%s setquota: invalid id '%s'\n",
7791 if (qctl->qc_id == 0) {
7793 "%s setquota: can't set quota for root usr/group/project.\n",
7800 if (qctl->qc_type != ALLQUOTA) {
7802 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7807 qctl->qc_type = qtype;
7809 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7811 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 15, 53, 0)
7812 fprintf(stderr, "'-d' deprecatd, use '-D' or '--default'\n");
7818 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7820 case LFS_SETQUOTA_DELETE:
7821 qctl->qc_cmd = LUSTRE_Q_DELETEQID;
7824 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7825 dqb->dqb_bsoftlimit >>= 10;
7826 limit_mask |= BSLIMIT;
7827 if (dqb->dqb_bsoftlimit &&
7828 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7830 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7832 (unsigned long long)dqb->dqb_bsoftlimit,
7836 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7837 dqb->dqb_bhardlimit >>= 10;
7838 limit_mask |= BHLIMIT;
7839 if (dqb->dqb_bhardlimit &&
7840 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7842 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7843 "See '%s help setquota' or Lustre manual for details\n",
7845 (unsigned long long)dqb->dqb_bhardlimit,
7849 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7850 limit_mask |= ISLIMIT;
7851 if (dqb->dqb_isoftlimit &&
7852 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7854 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7856 (unsigned long long)dqb->dqb_isoftlimit,
7860 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7861 limit_mask |= IHLIMIT;
7862 if (dqb->dqb_ihardlimit &&
7863 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7865 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7867 (unsigned long long)dqb->dqb_ihardlimit,
7871 if (lfs_verify_poolarg(optarg)) {
7875 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7876 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
7877 LUSTRE_Q_SETDEFAULT_POOL :
7878 LUSTRE_Q_SETQUOTAPOOL;
7882 "%s setquota: unrecognized option '%s'\n",
7883 progname, argv[optind - 1]);
7891 if (qctl->qc_type == ALLQUOTA) {
7893 "%s setquota: either -u or -g must be specified\n",
7899 if (!use_default && qctl->qc_cmd != LUSTRE_Q_DELETEQID &&
7902 "%s setquota: at least one limit must be specified\n",
7908 if ((use_default || qctl->qc_cmd == LUSTRE_Q_DELETEQID) &&
7911 "%s setquota: limits should not be specified when using default quota or deleting quota ID\n",
7917 if (use_default && qctl->qc_id == 0) {
7919 "%s setquota: can not set default quota for root user/group/project\n",
7925 if (qctl->qc_cmd == LUSTRE_Q_DELETEQID && qctl->qc_id == 0) {
7927 "%s setquota: can not delete root user/group/project\n",
7933 if (optind != argc - 1) {
7935 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7936 progname, argv[optind]);
7944 dqb->dqb_bhardlimit = 0;
7945 dqb->dqb_bsoftlimit = 0;
7946 dqb->dqb_ihardlimit = 0;
7947 dqb->dqb_isoftlimit = 0;
7950 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7951 /* do not set inode limits for Pool Quotas */
7952 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
7953 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
7954 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7955 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7956 /* sigh, we can't just set blimits/ilimits */
7957 struct if_quotactl *tmp_qctl;
7959 tmp_qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7963 if (qctl->qc_cmd == LUSTRE_Q_SETQUOTAPOOL) {
7964 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
7965 strncpy(tmp_qctl->qc_poolname, qctl->qc_poolname,
7968 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7970 tmp_qctl->qc_type = qctl->qc_type;
7971 tmp_qctl->qc_id = qctl->qc_id;
7973 rc = llapi_quotactl(mnt, tmp_qctl);
7979 if (!(limit_mask & BHLIMIT))
7980 dqb->dqb_bhardlimit = tmp_qctl->qc_dqblk.dqb_bhardlimit;
7981 if (!(limit_mask & BSLIMIT))
7982 dqb->dqb_bsoftlimit = tmp_qctl->qc_dqblk.dqb_bsoftlimit;
7983 if (!(limit_mask & IHLIMIT))
7984 dqb->dqb_ihardlimit = tmp_qctl->qc_dqblk.dqb_ihardlimit;
7985 if (!(limit_mask & ISLIMIT))
7986 dqb->dqb_isoftlimit = tmp_qctl->qc_dqblk.dqb_isoftlimit;
7988 /* Keep grace times if we have got no softlimit arguments */
7989 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7990 dqb->dqb_valid |= QIF_BTIME;
7991 dqb->dqb_btime = tmp_qctl->qc_dqblk.dqb_btime;
7994 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7995 dqb->dqb_valid |= QIF_ITIME;
7996 dqb->dqb_itime = tmp_qctl->qc_dqblk.dqb_itime;
8001 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
8002 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
8004 rc = llapi_quotactl(mnt, qctl);
8008 "%s setquota: cannot quotactl '%s' '%s': %s\n",
8010 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
8013 "%s setquota: quotactl failed: %s\n",
8014 progname, strerror(-rc));
8018 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8024 /* Converts seconds value into format string
8025 * result is returned in buf
8027 * 1. result is in descenting order: 1w2d3h4m5s
8028 * 2. zero fields are not filled (except for p. 3): 5d1s
8029 * 3. zero seconds value is presented as "0s"
8031 static char *__sec2str(time_t seconds, char *buf)
8033 const char spec[] = "smhdw";
8034 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
8039 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
8040 c = seconds / mult[i];
8042 if (c > 0 || (i == 0 && buf == tail))
8043 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
8052 static void sec2str(time_t seconds, char *buf, int rc)
8059 tail = __sec2str(seconds, tail);
8061 if (rc && tail - buf < 39) {
8067 static void diff2str(time_t seconds, char *buf, time_t now)
8072 if (seconds <= now) {
8073 strcpy(buf, "expired");
8076 __sec2str(seconds - now, buf);
8079 static void print_quota_title(char *name, struct if_quotactl *qctl,
8080 bool human_readable, bool show_default)
8083 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
8084 printf("%15s %8s%8s%8s %8s%8s%8s\n",
8085 "Filesystem", "bquota", "blimit", "bgrace",
8086 "iquota", "ilimit", "igrace");
8088 printf("Disk quotas for %s %s (%cid %u):\n",
8089 qtype_name(qctl->qc_type), name,
8090 *qtype_name(qctl->qc_type), qctl->qc_id);
8091 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
8092 "Filesystem", human_readable ? "used" : "kbytes",
8093 "quota", "limit", "grace",
8094 "files", "quota", "limit", "grace");
8098 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8101 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8104 snprintf(buf, buflen, "%5.4gP",
8105 (double)num / ((__u64)1 << 40));
8107 snprintf(buf, buflen, "%5.4gT",
8108 (double)num / (1 << 30));
8110 snprintf(buf, buflen, "%5.4gG",
8111 (double)num / (1 << 20));
8113 snprintf(buf, buflen, "%5.4gM",
8114 (double)num / (1 << 10));
8116 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8120 #ifdef HAVE_NATIVE_CLIENT
8121 /* In the current Lustre implementation, the grace time is either the time
8122 * or the timestamp to be used after some quota ID exceeds the soft limt,
8123 * 48 bits should be enough, its high 16 bits can be used as quota flags.
8125 #define LQUOTA_GRACE_BITS 48
8126 #define LQUOTA_GRACE_MASK ((1ULL << LQUOTA_GRACE_BITS) - 1)
8127 #define LQUOTA_GRACE_MAX LQUOTA_GRACE_MASK
8128 #define LQUOTA_GRACE(t) (t & LQUOTA_GRACE_MASK)
8129 #define LQUOTA_FLAG(t) (t >> LQUOTA_GRACE_BITS)
8130 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8133 #define STRBUF_LEN 24
8134 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8135 int rc, bool h, bool show_default)
8141 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8142 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8143 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8144 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8145 int bover = 0, iover = 0;
8146 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8147 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8149 char strbuf[STRBUF_LEN];
8151 dqb->dqb_btime &= LQUOTA_GRACE_MASK;
8152 dqb->dqb_itime &= LQUOTA_GRACE_MASK;
8154 if (dqb->dqb_bhardlimit &&
8155 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8157 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8158 if (dqb->dqb_btime > now)
8164 if (dqb->dqb_ihardlimit &&
8165 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8167 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8168 if (dqb->dqb_itime > now)
8174 if (strlen(mnt) > 15)
8175 printf("%s\n%15s", mnt, "");
8177 printf("%15s", mnt);
8180 snprintf(timebuf, sizeof(timebuf), "%llu",
8181 (unsigned long long)dqb->dqb_btime);
8183 diff2str(dqb->dqb_btime, timebuf, now);
8185 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8186 strbuf, sizeof(strbuf), h);
8187 if (rc == -EREMOTEIO)
8188 sprintf(numbuf[0], "%s*", strbuf);
8190 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8191 "%s" : "[%s]", strbuf);
8193 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8194 if (type == QC_GENERAL)
8195 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8196 "%s" : "[%s]", strbuf);
8198 sprintf(numbuf[1], "%s", "-");
8200 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8201 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8202 "%s" : "[%s]", strbuf);
8205 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8207 printf(" %7s%c %6s %7s %7s",
8208 numbuf[0], bover ? '*' : ' ', numbuf[1],
8209 numbuf[2], bover > 1 ? timebuf : "-");
8212 snprintf(timebuf, sizeof(timebuf), "%llu",
8213 (unsigned long long)dqb->dqb_itime);
8215 diff2str(dqb->dqb_itime, timebuf, now);
8217 snprintf(numbuf[0], sizeof(numbuf),
8218 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8219 (uintmax_t)dqb->dqb_curinodes);
8221 if (type == QC_GENERAL)
8222 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8224 (uintmax_t)dqb->dqb_isoftlimit);
8226 sprintf(numbuf[1], "%s", "-");
8228 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8229 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8232 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8233 else if (type != QC_OSTIDX)
8234 printf(" %7s%c %6s %7s %7s",
8235 numbuf[0], iover ? '*' : ' ', numbuf[1],
8236 numbuf[2], iover > 1 ? timebuf : "-");
8238 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8240 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8241 qctl->qc_cmd == Q_GETOINFO) {
8245 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8246 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8248 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8249 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8250 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8252 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8254 printf("Block grace time: %s; Inode grace time: %s\n",
8255 bgtimebuf, igtimebuf);
8259 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8263 /* format is "lustre-OST0001" */
8264 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8266 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8271 *idx = strtoul(dash, &endp, 16);
8272 if (*idx > 0xffff) {
8273 fprintf(stderr, "wrong index %s\n", tgtname);
8280 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8281 bool h, __u64 *total)
8283 int rc = 0, rc1 = 0, count = 0, i = 0;
8284 char **list = NULL, *buffer = NULL;
8285 __u32 valid = qctl->qc_valid;
8287 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8290 /* Is it correct for the case OST0000, OST0002, OST0003 -
8291 * we will ask OST0001 that is absent and won't ask OST0003? */
8292 rc = llapi_get_obd_count(mnt, &count, is_mdt);
8294 fprintf(stderr, "can not get %s count: %s\n",
8295 is_mdt ? "mdt" : "ost", strerror(-rc));
8299 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8300 char fname[PATH_MAX];
8301 char fsname[LUSTRE_MAXFSNAME + 1];
8302 int bufsize = sizeof(struct obd_uuid) * count;
8304 rc = llapi_search_fsname(mnt, fsname);
8306 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8310 buffer = malloc(bufsize + sizeof(*list) * count);
8313 list = (char **)(buffer + bufsize);
8314 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8315 count = llapi_get_poolmembers(fname, list, count,
8321 for (i = 0; i < count; i++) {
8322 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8325 if (tgt_name2index(list[i], &index))
8327 qctl->qc_idx = index;
8332 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8333 rc = llapi_quotactl(mnt, qctl);
8335 /* It is remote client case. */
8336 if (rc == -EOPNOTSUPP) {
8341 /* no target for this index yet */
8342 if (rc == -ENODEV) {
8347 /* inactive target */
8348 if (rc == -ENODATA) {
8349 char name[UUID_MAX+8];
8351 snprintf(name, sizeof(name), "%s[inact]",
8352 obd_uuid2str(&qctl->obd_uuid));
8353 memset(&qctl->qc_dqinfo, 0,
8354 sizeof(qctl->qc_dqinfo));
8355 memset(&qctl->qc_dqblk, 0,
8356 sizeof(qctl->qc_dqblk));
8357 print_quota(name, qctl, qctl->qc_valid, 0, h,
8365 fprintf(stderr, "quotactl %s%d failed.\n",
8366 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8370 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8371 qctl->qc_valid, 0, h, false);
8372 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8373 qctl->qc_dqblk.dqb_bhardlimit;
8378 qctl->qc_valid = valid;
8382 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8383 int verbose, int quiet, bool human_readable,
8386 int rc1 = 0, rc2 = 0, rc3 = 0;
8387 char *obd_type = (char *)qctl->obd_type;
8388 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8389 __u64 total_ialloc = 0, total_balloc = 0;
8390 bool use_default_for_blk = false;
8391 bool use_default_for_file = false;
8394 rc1 = llapi_quotactl(mnt, qctl);
8398 fprintf(stderr, "%s quotas are not enabled.\n",
8399 qtype_name(qctl->qc_type));
8402 fprintf(stderr, "Permission denied.\n");
8405 /* We already got error message. */
8408 fprintf(stderr, "Unexpected quotactl error: %s\n",
8413 if (!show_default && qctl->qc_id == 0) {
8414 qctl->qc_dqblk.dqb_bhardlimit = 0;
8415 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8416 qctl->qc_dqblk.dqb_ihardlimit = 0;
8417 qctl->qc_dqblk.dqb_isoftlimit = 0;
8418 qctl->qc_dqblk.dqb_btime = 0;
8419 qctl->qc_dqblk.dqb_itime = 0;
8420 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8423 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8424 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8425 use_default_for_blk = true;
8426 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8429 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8430 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8431 use_default_for_file = true;
8432 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8435 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8436 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8437 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8438 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8439 print_quota_title(name, qctl, human_readable, show_default);
8441 if (rc1 && *obd_type)
8442 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8444 if (qctl->qc_valid != QC_GENERAL)
8447 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8448 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8449 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8450 (QIF_LIMITS|QIF_USAGE));
8452 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8454 if (!show_default && verbose &&
8455 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8456 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8457 char strbuf[STRBUF_LEN];
8459 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8461 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8463 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8465 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8466 (uintmax_t)total_ialloc, strbuf);
8469 if (use_default_for_blk)
8470 printf("%cid %u is using default block quota setting\n",
8471 *qtype_name(qctl->qc_type), qctl->qc_id);
8473 if (use_default_for_file)
8474 printf("%cid %u is using default file quota setting\n",
8475 *qtype_name(qctl->qc_type), qctl->qc_id);
8477 if (rc1 || rc2 || rc3 || inacc)
8478 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8492 static int lfs_project(int argc, char **argv)
8494 int ret = 0, err = 0, c, i;
8495 struct project_handle_control phc = { 0 };
8496 enum lfs_project_ops_t op;
8499 phc.assign_projid = false;
8500 /* default action */
8501 op = LFS_PROJECT_LIST;
8503 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8506 if (op != LFS_PROJECT_LIST) {
8508 "%s: cannot specify '-c' '-C' '-s' together\n",
8513 op = LFS_PROJECT_CHECK;
8516 if (op != LFS_PROJECT_LIST) {
8518 "%s: cannot specify '-c' '-C' '-s' together\n",
8523 op = LFS_PROJECT_CLEAR;
8526 if (op != LFS_PROJECT_LIST) {
8528 "%s: cannot specify '-c' '-C' '-s' together\n",
8533 phc.set_inherit = true;
8534 op = LFS_PROJECT_SET;
8540 phc.keep_projid = true;
8543 phc.recursive = true;
8546 if (str2quotaid(&phc.projid, optarg)) {
8548 "Invalid project ID: %s\n",
8553 phc.assign_projid = true;
8557 phc.newline = false;
8560 fprintf(stderr, "%s: invalid option '%c'\n",
8566 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8567 op = LFS_PROJECT_SET;
8568 phc.set_projid = true;
8569 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8570 phc.set_projid = true;
8574 case LFS_PROJECT_CHECK:
8575 if (phc.keep_projid) {
8577 "%s: '-k' is useless together with '-c'\n",
8582 case LFS_PROJECT_CLEAR:
8585 "%s: '-0' is useless together with '-C'\n",
8589 if (phc.assign_projid) {
8591 "%s: '-p' is useless together with '-C'\n",
8596 case LFS_PROJECT_SET:
8599 "%s: '-0' is useless together with '-s'\n",
8603 if (phc.keep_projid) {
8605 "%s: '-k' is useless together with '-s'\n",
8613 "%s: '-0' is useless for list operations\n",
8623 fprintf(stderr, "%s: missing file or directory target(s)\n",
8628 for (i = 0; i < argc; i++) {
8630 case LFS_PROJECT_CHECK:
8631 err = lfs_project_check(argv[i], &phc);
8633 case LFS_PROJECT_LIST:
8634 err = lfs_project_list(argv[i], &phc);
8636 case LFS_PROJECT_CLEAR:
8637 err = lfs_project_clear(argv[i], &phc);
8639 case LFS_PROJECT_SET:
8640 err = lfs_project_set(argv[i], &phc);
8652 static int lfs_quota(int argc, char **argv)
8655 char *mnt, *name = NULL;
8656 struct if_quotactl *qctl;
8658 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8659 __u32 valid = QC_GENERAL, idx = 0;
8660 bool human_readable = false;
8661 bool show_default = false;
8663 bool show_pools = false;
8664 struct option long_opts[] = {
8665 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
8667 char **poollist = NULL;
8671 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8675 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8676 qctl->qc_type = ALLQUOTA;
8677 obd_uuid = (char *)qctl->obd_uuid.uuid;
8679 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8680 long_opts, NULL)) != -1) {
8683 show_default = true;
8688 show_default = true;
8693 show_default = true;
8697 if (qctl->qc_type != ALLQUOTA) {
8699 "%s quota: only one of -u, -g, or -p may be specified\n",
8704 qctl->qc_type = qtype;
8707 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8710 valid = qctl->qc_valid = QC_UUID;
8711 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8714 valid = qctl->qc_valid = QC_MDTIDX;
8715 idx = qctl->qc_idx = atoi(optarg);
8716 if (idx == 0 && *optarg != '0') {
8718 "%s quota: invalid MDT index '%s'\n",
8725 valid = qctl->qc_valid = QC_OSTIDX;
8726 idx = qctl->qc_idx = atoi(optarg);
8727 if (idx == 0 && *optarg != '0') {
8729 "%s quota: invalid OST index '%s'\n",
8742 human_readable = true;
8745 if ((!optarg) && (argv[optind] != NULL) &&
8746 (argv[optind][0] != '-') &&
8747 (argv[optind][0] != '/')) {
8748 optarg = argv[optind++];
8749 if (lfs_verify_poolarg(optarg)) {
8753 strncpy(qctl->qc_poolname, optarg,
8755 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
8756 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
8758 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8762 /* optarg is NULL */
8764 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8767 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8768 progname, argv[optind - 1]);
8774 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8775 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8776 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8777 qctl->qc_type == ALLQUOTA &&
8778 optind == argc - 1 && !show_default) {
8781 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8782 qctl->qc_type = qtype;
8783 qctl->qc_valid = valid;
8784 if (qtype == USRQUOTA) {
8785 qctl->qc_id = geteuid();
8786 rc = uid2name(&name, qctl->qc_id);
8788 qctl->qc_id = getegid();
8789 rc = gid2name(&name, qctl->qc_id);
8790 memset(&qctl->qc_dqblk, 0,
8791 sizeof(qctl->qc_dqblk));
8796 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8797 human_readable, show_default);
8802 /* lfs quota -u username /path/to/lustre/mount */
8803 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8804 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8805 /* options should be followed by u/g-name and mntpoint */
8806 if ((!show_default && optind + 2 != argc) ||
8807 (show_default && optind + 1 != argc) ||
8808 qctl->qc_type == ALLQUOTA) {
8810 "%s quota: name and mount point must be specified\n",
8816 if (!show_default) {
8817 name = argv[optind++];
8818 switch (qctl->qc_type) {
8820 rc = name2uid(&qctl->qc_id, name);
8823 rc = name2gid(&qctl->qc_id, name);
8826 rc = name2projid(&qctl->qc_id, name);
8833 qctl->qc_valid = QC_GENERAL;
8834 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8835 LUSTRE_Q_GETDEFAULT_POOL :
8836 LUSTRE_Q_GETDEFAULT;
8841 if (str2quotaid(&qctl->qc_id, name)) {
8842 fprintf(stderr, "%s quota: invalid id '%s'\n",
8848 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8849 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8860 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
8864 for (i = 0; i < poolcount; i++) {
8865 p = memchr(poollist[i], '.', MAXNAMLEN);
8867 fprintf(stderr, "bad string format %.*s\n",
8868 MAXNAMLEN, poollist[i]);
8873 printf("Quotas for pool: %s\n", p);
8874 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
8875 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8876 human_readable, show_default);
8883 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8884 human_readable, show_default);
8890 #endif /* HAVE_SYS_QUOTA_H! */
8892 static int flushctx_ioctl(char *mp)
8896 fd = open(mp, O_RDONLY);
8898 fprintf(stderr, "flushctx: error open %s: %s\n",
8899 mp, strerror(errno));
8903 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8905 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8906 mp, strerror(errno));
8912 static int lfs_flushctx(int argc, char **argv)
8914 int kdestroy = 0, reap = 0, c;
8915 char mntdir[PATH_MAX] = {'\0'};
8919 while ((c = getopt(argc, argv, "kr")) != -1) {
8929 "error: %s: option '-%c' unrecognized\n",
8936 rc = system("kdestroy > /dev/null");
8938 rc = WEXITSTATUS(rc);
8940 "error destroying tickets: %d, continuing\n",
8945 if (optind >= argc) {
8946 /* flush for all mounted lustre fs. */
8947 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8948 /* Check if we have a mount point */
8949 if (mntdir[0] == '\0')
8952 if (flushctx_ioctl(mntdir))
8955 mntdir[0] = '\0'; /* avoid matching in next loop */
8958 /* flush fs as specified */
8959 while (optind < argc) {
8960 if (flushctx_ioctl(argv[optind++]))
8966 rc = system("keyctl reap > /dev/null");
8968 rc = WEXITSTATUS(rc);
8969 fprintf(stderr, "error reaping keyring: %d\n", rc);
8976 static int lfs_changelog(int argc, char **argv)
8978 void *changelog_priv;
8979 struct changelog_rec *rec;
8980 long long startrec = 0, endrec = 0;
8982 struct option long_opts[] = {
8983 { .val = 'f', .name = "follow", .has_arg = no_argument },
8985 char short_opts[] = "f";
8988 while ((rc = getopt_long(argc, argv, short_opts,
8989 long_opts, NULL)) != -1) {
8996 "%s changelog: unrecognized option '%s'\n",
8997 progname, argv[optind - 1]);
9001 if (optind >= argc) {
9002 fprintf(stderr, "%s changelog: mdtname must be specified\n",
9007 mdd = argv[optind++];
9008 if (argc > optind) {
9010 startrec = strtoll(argv[optind++], NULL, 10);
9011 if (errno != 0 || startrec < 0) {
9013 "%s changelog: bad startrec\n",
9019 if (argc > optind) {
9021 endrec = strtoll(argv[optind++], NULL, 10);
9022 if (errno != 0 || endrec < 0) {
9024 "%s changelog: bad endrec\n",
9030 rc = llapi_changelog_start(&changelog_priv,
9031 CHANGELOG_FLAG_BLOCK |
9032 CHANGELOG_FLAG_JOBID |
9033 CHANGELOG_FLAG_EXTRA_FLAGS |
9034 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
9037 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
9038 progname, strerror(errno = -rc));
9042 rc = llapi_changelog_set_xflags(changelog_priv,
9043 CHANGELOG_EXTRA_FLAG_UIDGID |
9044 CHANGELOG_EXTRA_FLAG_NID |
9045 CHANGELOG_EXTRA_FLAG_OMODE |
9046 CHANGELOG_EXTRA_FLAG_XATTR);
9049 "%s changelog: cannot set xflags for changelog: %s\n",
9050 progname, strerror(errno = -rc));
9054 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
9058 if (endrec && rec->cr_index > endrec) {
9059 llapi_changelog_free(&rec);
9062 if (rec->cr_index < startrec) {
9063 llapi_changelog_free(&rec);
9067 secs = rec->cr_time >> 30;
9068 gmtime_r(&secs, &ts);
9069 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
9070 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
9071 changelog_type2str(rec->cr_type),
9072 ts.tm_hour, ts.tm_min, ts.tm_sec,
9073 (int)(rec->cr_time & ((1 << 30) - 1)),
9074 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
9075 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
9077 if (rec->cr_flags & CLF_JOBID) {
9078 struct changelog_ext_jobid *jid =
9079 changelog_rec_jobid(rec);
9081 if (jid->cr_jobid[0] != '\0')
9082 printf(" j=%s", jid->cr_jobid);
9085 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
9086 struct changelog_ext_extra_flags *ef =
9087 changelog_rec_extra_flags(rec);
9089 printf(" ef=0x%llx",
9090 (unsigned long long)ef->cr_extra_flags);
9092 if (ef->cr_extra_flags & CLFE_UIDGID) {
9093 struct changelog_ext_uidgid *uidgid =
9094 changelog_rec_uidgid(rec);
9096 printf(" u=%llu:%llu",
9097 (unsigned long long)uidgid->cr_uid,
9098 (unsigned long long)uidgid->cr_gid);
9100 if (ef->cr_extra_flags & CLFE_NID) {
9101 struct changelog_ext_nid *nid =
9102 changelog_rec_nid(rec);
9105 libcfs_nid2str(nid->cr_nid));
9108 if (ef->cr_extra_flags & CLFE_OPEN) {
9109 struct changelog_ext_openmode *omd =
9110 changelog_rec_openmode(rec);
9111 char mode[] = "---";
9113 /* exec mode must be exclusive */
9114 if (omd->cr_openflags & MDS_FMODE_EXEC) {
9117 if (omd->cr_openflags & MDS_FMODE_READ)
9119 if (omd->cr_openflags &
9126 if (strcmp(mode, "---") != 0)
9127 printf(" m=%s", mode);
9130 if (ef->cr_extra_flags & CLFE_XATTR) {
9131 struct changelog_ext_xattr *xattr =
9132 changelog_rec_xattr(rec);
9134 if (xattr->cr_xattr[0] != '\0')
9135 printf(" x=%s", xattr->cr_xattr);
9139 if (!fid_is_zero(&rec->cr_pfid))
9140 printf(" p="DFID, PFID(&rec->cr_pfid));
9141 if (rec->cr_namelen)
9142 printf(" %.*s", rec->cr_namelen,
9143 changelog_rec_name(rec));
9145 if (rec->cr_flags & CLF_RENAME) {
9146 struct changelog_ext_rename *rnm =
9147 changelog_rec_rename(rec);
9149 if (!fid_is_zero(&rnm->cr_sfid))
9150 printf(" s="DFID" sp="DFID" %.*s",
9151 PFID(&rnm->cr_sfid),
9152 PFID(&rnm->cr_spfid),
9153 (int)changelog_rec_snamelen(rec),
9154 changelog_rec_sname(rec));
9158 llapi_changelog_free(&rec);
9161 llapi_changelog_fini(&changelog_priv);
9164 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9165 progname, strerror(errno = -rc));
9167 return (rc == 1 ? 0 : rc);
9170 static int lfs_changelog_clear(int argc, char **argv)
9179 endrec = strtoll(argv[3], NULL, 10);
9180 if (errno != 0 || endrec < 0) {
9182 "%s: bad endrec '%s'\n",
9187 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9190 fprintf(stderr, "%s: record out of range: %llu\n",
9192 else if (rc == -ENOENT)
9193 fprintf(stderr, "%s: no changelog user: %s\n",
9196 fprintf(stderr, "%s error: %s\n", argv[0],
9205 static void rstripc(char *str, int c)
9207 char *end = str + strlen(str);
9209 for (; str < end && end[-1] == c; --end)
9213 static int lfs_fid2path(int argc, char **argv)
9215 struct option long_opts[] = {
9216 { .val = 'c', .name = "cur", .has_arg = no_argument },
9217 { .val = 'c', .name = "current", .has_arg = no_argument },
9218 { .val = 'c', .name = "print-link", .has_arg = no_argument },
9219 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
9220 { .val = 'l', .name = "link", .has_arg = required_argument },
9222 char short_opts[] = "cfl:pr:";
9223 bool print_link = false;
9224 bool print_fid = false;
9226 char mnt_dir[PATH_MAX] = "";
9228 char *path_or_fsname;
9229 long long recno = -1;
9231 char *endptr = NULL;
9236 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9246 linkno = strtol(optarg, &endptr, 10);
9247 if (errno != 0 || *endptr != '\0' || linkno < 0) {
9249 "%s fid2path: invalid linkno '%s'\n",
9255 /* recno is something to do with changelogs
9256 * that was never implemented. We just pass it
9257 * through for the MDT to ignore.
9260 recno = strtoll(optarg, &endptr, 10);
9261 if (errno != 0 || *endptr != '\0' || recno < 0) {
9263 "%s fid2path: invalid recno '%s'\n",
9270 "%s fid2path: unrecognized option '%s'\n",
9271 progname, argv[optind - 1]);
9276 if (argc - optind < 2) {
9278 "Usage: %s fid2path FSNAME|ROOT FID...\n",
9283 path_or_fsname = argv[optind];
9285 if (*path_or_fsname == '/') {
9286 print_mnt_dir = true;
9287 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9289 print_mnt_dir = false;
9290 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9295 "%s fid2path: cannot resolve mount point for '%s': %s\n",
9296 progname, path_or_fsname, strerror(-rc));
9300 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9303 "%s fid2path: cannot open mount point for '%s': %s\n",
9304 progname, path_or_fsname, strerror(-rc));
9308 /* Strip trailing slashes from mnt_dir. */
9309 rstripc(mnt_dir + 1, '/');
9311 for (i = optind + 1; i < argc; i++) {
9312 const char *fid_str = argv[i];
9316 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9319 "%s fid2path: invalid FID '%s'\n",
9327 int linktmp = (linkno >= 0) ? linkno : 0;
9329 int oldtmp = linktmp;
9330 long long rectmp = recno;
9331 char path_buf[PATH_MAX];
9333 rc2 = llapi_fid2path_at(mnt_fd, &fid,
9334 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9337 "%s fid2path: cannot find %s %s: %s\n",
9338 progname, path_or_fsname, fid_str,
9346 printf("%s ", fid_str);
9349 printf("%d ", linktmp);
9351 /* You may think this looks wrong or weird (and it is!)
9352 * but we are actually trying to preserve the old quirky
9353 * behaviors (enforced by our old quirky tests!) that
9354 * make lfs so much fun to work on:
9356 * lustre 0x200000007:0x1:0x0 => "/"
9357 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9359 * Note that llapi_fid2path() returns "" for the root
9363 print_mnt_dir ? mnt_dir : "",
9364 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9368 /* specified linkno */
9371 if (oldtmp == linktmp)
9383 static int lfs_path2fid(int argc, char **argv)
9385 struct option long_opts[] = {
9386 { .val = 'p', .name = "parents", .has_arg = no_argument },
9389 const char short_opts[] = "p";
9390 const char *sep = "";
9393 bool show_parents = false;
9395 while ((rc = getopt_long(argc, argv, short_opts,
9396 long_opts, NULL)) != -1) {
9399 show_parents = true;
9403 "%s path2fid: unrecognized option '%s'\n",
9404 progname, argv[optind - 1]);
9409 if (optind > argc - 1) {
9410 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9413 } else if (optind < argc - 1) {
9418 for (path = argv + optind; *path != NULL; path++) {
9421 if (!show_parents) {
9422 err = llapi_path2fid(*path, &fid);
9424 printf("%s%s"DFID"\n",
9425 *sep != '\0' ? *path : "", sep,
9428 char name[NAME_MAX + 1];
9429 unsigned int linkno = 0;
9431 while ((err = llapi_path2parent(*path, linkno, &fid,
9432 name, sizeof(name))) == 0) {
9433 if (*sep != '\0' && linkno == 0)
9434 printf("%s%s", *path, sep);
9436 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9441 /* err == -ENODATA is end-of-loop */
9442 if (linkno > 0 && err == -ENODATA) {
9450 "%s path2fid: cannot get %sfid for '%s': %s\n",
9451 progname, show_parents ? "parent " : "", *path,
9463 #define MAX_ERRNO 4095
9464 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9466 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9470 rc = llapi_rmfid(device, fa);
9472 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9473 progname, strerror(-rc));
9477 for (k = 0; k < fa->fa_nr; k++) {
9478 rc2 = (__s32)fa->fa_fids[k].f_ver;
9479 if (!IS_ERR_VALUE(rc2))
9485 fa->fa_fids[k].f_ver = 0;
9486 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9487 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9493 static int lfs_rmfid(int argc, char **argv)
9495 char *fidstr, *device;
9496 int rc = 0, rc2, nr;
9497 struct fid_array *fa;
9499 if (optind > argc - 1) {
9500 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9504 device = argv[optind++];
9507 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9513 while (optind < argc) {
9516 fidstr = argv[optind++];
9517 while (*fidstr == '[')
9519 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9521 fprintf(stderr, "unrecognized FID: %s\n",
9526 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9527 /* start another batch */
9528 rc2 = lfs_rmfid_and_show_errors(device, fa);
9535 rc2 = lfs_rmfid_and_show_errors(device, fa);
9543 static int lfs_data_version(int argc, char **argv)
9545 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9553 fprintf(stderr, "%s: FILE must be specified\n",
9558 while ((c = getopt(argc, argv, "hnrw")) != -1) {
9561 data_version_flags = 0;
9564 data_version_flags |= LL_DV_RD_FLUSH;
9567 data_version_flags |= LL_DV_WR_FLUSH;
9571 "%s data_version: unrecognized option '%s'\n",
9572 progname, argv[optind - 1]);
9578 if (optind == argc) {
9579 fprintf(stderr, "%s data_version: FILE must be specified\n",
9584 path = argv[optind];
9585 fd = open(path, O_RDONLY);
9588 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9589 progname, path, strerror(-rc));
9593 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9596 "%s data_version: cannot get version for '%s': %s\n",
9597 progname, path, strerror(-rc));
9599 printf("%ju" "\n", (uintmax_t)data_version);
9605 static int lfs_hsm_state(int argc, char **argv)
9610 struct hsm_user_state hus;
9618 rc = llapi_hsm_state_get(path, &hus);
9620 fprintf(stderr, "can't get hsm state for %s: %s\n",
9621 path, strerror(errno = -rc));
9625 /* Display path name and status flags */
9626 printf("%s: (0x%08x)", path, hus.hus_states);
9628 if (hus.hus_states & HS_RELEASED)
9629 printf(" released");
9630 if (hus.hus_states & HS_EXISTS)
9632 if (hus.hus_states & HS_DIRTY)
9634 if (hus.hus_states & HS_ARCHIVED)
9635 printf(" archived");
9636 /* Display user-settable flags */
9637 if (hus.hus_states & HS_NORELEASE)
9638 printf(" never_release");
9639 if (hus.hus_states & HS_NOARCHIVE)
9640 printf(" never_archive");
9641 if (hus.hus_states & HS_LOST)
9642 printf(" lost_from_hsm");
9644 if (hus.hus_archive_id != 0)
9645 printf(", archive_id:%d", hus.hus_archive_id);
9648 } while (++i < argc);
9653 #define LFS_HSM_SET 0
9654 #define LFS_HSM_CLEAR 1
9657 * Generic function to set or clear HSM flags.
9658 * Used by hsm_set and hsm_clear.
9660 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9662 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9664 struct option long_opts[] = {
9665 { .val = 'A', .name = "archived", .has_arg = no_argument },
9666 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
9667 { .val = 'd', .name = "dirty", .has_arg = no_argument },
9668 { .val = 'e', .name = "exists", .has_arg = no_argument },
9669 { .val = 'h', .name = "help", .has_arg = no_argument },
9670 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
9671 { .val = 'l', .name = "lost", .has_arg = no_argument },
9672 { .val = 'r', .name = "norelease", .has_arg = no_argument },
9677 __u32 archive_id = 0;
9683 while ((c = getopt_long(argc, argv, "aAdehi:lr",
9684 long_opts, NULL)) != -1) {
9690 mask |= HS_NOARCHIVE;
9693 mask |= HS_ARCHIVED;
9696 mask |= HS_NORELEASE;
9706 archive_id = strtol(optarg, &end, 10);
9707 if (errno != 0 || *end != '\0' || archive_id < 0) {
9709 "%s: invalid archive_id: '%s'\n",
9715 fprintf(stderr, "%s: unrecognized option '%s'\n",
9716 progname, argv[optind - 1]);
9723 /* User should have specified a flag */
9727 while (optind < argc) {
9728 path = argv[optind];
9730 /* If mode == 0, this means we apply the mask. */
9731 if (mode == LFS_HSM_SET)
9732 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9734 rc = llapi_hsm_state_set(path, 0, mask, 0);
9737 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9738 path, strerror(errno = -rc));
9747 static int lfs_hsm_action(int argc, char **argv)
9752 struct hsm_current_action hca;
9753 struct hsm_extent he;
9754 enum hsm_user_action hua;
9755 enum hsm_progress_states hps;
9763 rc = llapi_hsm_current_action(path, &hca);
9765 fprintf(stderr, "can't get hsm action for %s: %s\n",
9766 path, strerror(errno = -rc));
9769 he = hca.hca_location;
9770 hua = hca.hca_action;
9771 hps = hca.hca_state;
9773 printf("%s: %s", path, hsm_user_action2name(hua));
9775 /* Skip file without action */
9776 if (hca.hca_action == HUA_NONE) {
9781 printf(" %s ", hsm_progress_state2name(hps));
9783 if ((hps == HPS_RUNNING) &&
9784 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9785 printf("(%llu bytes moved)\n",
9786 (unsigned long long)he.length);
9787 else if ((he.offset + he.length) == LUSTRE_EOF)
9788 printf("(from %llu to EOF)\n",
9789 (unsigned long long)he.offset);
9791 printf("(from %llu to %llu)\n",
9792 (unsigned long long)he.offset,
9793 (unsigned long long)(he.offset + he.length));
9795 } while (++i < argc);
9800 static int lfs_hsm_set(int argc, char **argv)
9802 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9805 static int lfs_hsm_clear(int argc, char **argv)
9807 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9811 * Check file state and return its fid, to be used by lfs_hsm_request().
9813 * \param[in] file Path to file to check
9814 * \param[in,out] fid Pointer to allocated lu_fid struct.
9815 * \param[in,out] last_dev Pointer to last device id used.
9817 * \return 0 on success.
9819 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9825 rc = lstat(file, &st);
9827 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9831 * Checking for regular file as archiving as posix copytool
9832 * rejects archiving files other than regular files
9834 if (!S_ISREG(st.st_mode)) {
9835 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9838 /* A request should be ... */
9839 if (*last_dev != st.st_dev && *last_dev != 0) {
9841 "All files should be on the same filesystem: %s\n",
9845 *last_dev = st.st_dev;
9847 rc = llapi_path2fid(file, fid);
9849 fprintf(stderr, "Cannot read FID of %s: %s\n",
9850 file, strerror(-rc));
9856 /* Fill an HSM HUR item with a given file name.
9858 * If mntpath is set, then the filename is actually a FID, and no
9859 * lookup on the filesystem will be performed.
9861 * \param[in] hur the user request to fill
9862 * \param[in] idx index of the item inside the HUR to fill
9863 * \param[in] mntpath mountpoint of Lustre
9864 * \param[in] fname filename (if mtnpath is NULL)
9865 * or FID (if mntpath is set)
9866 * \param[in] last_dev pointer to last device id used
9868 * \retval 0 on success
9869 * \retval CMD_HELP or a negative errno on error
9871 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9872 const char *mntpath, const char *fname,
9875 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9878 hui->hui_extent.length = -1;
9881 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9883 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9886 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9890 hur->hur_request.hr_itemcount++;
9895 static int lfs_hsm_request(int argc, char **argv, int action)
9897 struct option long_opts[] = {
9898 { .val = 'a', .name = "archive", .has_arg = required_argument },
9899 { .val = 'D', .name = "data", .has_arg = required_argument },
9900 { .val = 'h', .name = "help", .has_arg = no_argument },
9901 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9902 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9905 struct hsm_user_request *hur, *oldhur;
9910 char *filelist = NULL;
9911 char fullpath[PATH_MAX];
9912 char *opaque = NULL;
9916 int nbfile_alloc = 0;
9917 char *some_file = NULL;
9918 char *mntpath = NULL;
9924 while ((c = getopt_long(argc, argv, "a:D:hl:m:",
9925 long_opts, NULL)) != -1) {
9934 if (action != HUA_ARCHIVE &&
9935 action != HUA_REMOVE) {
9937 "error: -a is supported only when archiving or removing\n");
9940 archive_id = atoi(optarg);
9945 some_file = strdup(optarg);
9949 fprintf(stderr, "%s: unrecognized option '%s'\n",
9950 progname, argv[optind - 1]);
9957 /* All remaining args are files, so we have at least nbfile */
9958 nbfile = argc - optind;
9960 if ((nbfile == 0) && (!filelist))
9964 opaque_len = strlen(opaque);
9967 * Alloc the request structure with enough place to store all files
9968 * from command line.
9970 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9972 fprintf(stderr, "Cannot create the request: %s\n",
9976 nbfile_alloc = nbfile;
9978 hur->hur_request.hr_action = action;
9979 hur->hur_request.hr_archive_id = archive_id;
9980 hur->hur_request.hr_flags = 0;
9982 /* All remaining args are files, add them */
9983 if (nbfile != 0 && some_file == NULL)
9984 some_file = strdup(argv[optind]);
9986 for (i = 0; i < nbfile; i++) {
9987 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9993 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9995 /* If a filelist was specified, read the filelist from it. */
9997 fp = fopen(filelist, "r");
9999 fprintf(stderr, "Cannot read the file list %s: %s\n",
10000 filelist, strerror(errno));
10005 while ((rc = getline(&line, &len, fp)) != -1) {
10007 * If allocated buffer was too small, get something
10010 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
10013 nbfile_alloc = nbfile_alloc * 2 + 1;
10015 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
10019 "hsm: cannot allocate the request: %s\n",
10026 size = hur_len(oldhur);
10029 "hsm: cannot allocate %u files + %u bytes data\n",
10030 oldhur->hur_request.hr_itemcount,
10031 oldhur->hur_request.hr_data_len);
10038 memcpy(hur, oldhur, size);
10043 if (line[strlen(line) - 1] == '\n')
10044 line[strlen(line) - 1] = '\0';
10046 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
10047 mntpath, line, &last_dev);
10063 /* If a --data was used, add it to the request */
10064 hur->hur_request.hr_data_len = opaque_len;
10066 memcpy(hur_data(hur), opaque, opaque_len);
10068 /* Send the HSM request */
10069 if (realpath(some_file, fullpath) == NULL) {
10070 fprintf(stderr, "Could not find path '%s': %s\n",
10071 some_file, strerror(errno));
10073 rc = llapi_hsm_request(fullpath, hur);
10075 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
10076 some_file, strerror(-rc));
10086 static int lfs_hsm_archive(int argc, char **argv)
10088 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
10091 static int lfs_hsm_restore(int argc, char **argv)
10093 return lfs_hsm_request(argc, argv, HUA_RESTORE);
10096 static int lfs_hsm_release(int argc, char **argv)
10098 return lfs_hsm_request(argc, argv, HUA_RELEASE);
10101 static int lfs_hsm_remove(int argc, char **argv)
10103 return lfs_hsm_request(argc, argv, HUA_REMOVE);
10106 static int lfs_hsm_cancel(int argc, char **argv)
10108 return lfs_hsm_request(argc, argv, HUA_CANCEL);
10111 static int lfs_swap_layouts(int argc, char **argv)
10116 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
10117 SWAP_LAYOUTS_KEEP_MTIME |
10118 SWAP_LAYOUTS_KEEP_ATIME);
10121 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
10123 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
10125 int lfs_get_mode(const char *string)
10127 enum lock_mode_user mode;
10129 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10130 if (lock_mode_names[mode] == NULL)
10132 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10139 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10141 enum lu_ladvise_type advice;
10144 advice < ARRAY_SIZE(ladvise_names); advice++) {
10145 if (ladvise_names[advice] == NULL)
10147 if (strcmp(string, ladvise_names[advice]) == 0)
10151 return LU_LADVISE_INVALID;
10154 static int lfs_ladvise(int argc, char **argv)
10156 struct option long_opts[] = {
10157 { .val = 'a', .name = "advice", .has_arg = required_argument },
10158 { .val = 'b', .name = "background", .has_arg = no_argument },
10159 { .val = 'e', .name = "end", .has_arg = required_argument },
10160 { .val = 'h', .name = "help", .has_arg = no_argument },
10161 { .val = 'l', .name = "length", .has_arg = required_argument },
10162 { .val = 'm', .name = "mode", .has_arg = required_argument },
10163 { .val = 's', .name = "start", .has_arg = required_argument },
10164 { .val = 'u', .name = "unset", .has_arg = no_argument },
10165 { .name = NULL } };
10166 struct llapi_lu_ladvise advice;
10167 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10168 unsigned long long start = 0;
10169 unsigned long long end = LUSTRE_EOF;
10170 unsigned long long length = 0;
10171 unsigned long long size_units;
10172 unsigned long long flags = 0;
10178 while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10179 long_opts, NULL)) != -1) {
10182 advice_type = lfs_get_ladvice(optarg);
10183 if (advice_type == LU_LADVISE_INVALID) {
10185 "%s: invalid advice type '%s'\n",
10187 fprintf(stderr, "Valid types:");
10189 for (advice_type = 0;
10190 advice_type < ARRAY_SIZE(ladvise_names);
10192 if (ladvise_names[advice_type] == NULL)
10194 fprintf(stderr, " %s",
10195 ladvise_names[advice_type]);
10197 fprintf(stderr, "\n");
10210 rc = llapi_parse_size(optarg, &end,
10213 fprintf(stderr, "%s: bad end offset '%s'\n",
10220 rc = llapi_parse_size(optarg, &start,
10224 "%s: bad start offset '%s'\n",
10231 rc = llapi_parse_size(optarg, &length,
10234 fprintf(stderr, "%s: bad length '%s'\n",
10240 mode = lfs_get_mode(optarg);
10243 "%s: bad mode '%s', valid modes are READ or WRITE\n",
10249 fprintf(stderr, "%s: unrecognized option '%s'\n",
10250 progname, argv[optind - 1]);
10257 if (advice_type == LU_LADVISE_INVALID) {
10258 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10259 fprintf(stderr, "Valid types:");
10260 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10262 if (ladvise_names[advice_type] == NULL)
10264 fprintf(stderr, " %s", ladvise_names[advice_type]);
10266 fprintf(stderr, "\n");
10270 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10272 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10277 if (argc <= optind) {
10278 fprintf(stderr, "%s: please give one or more file names\n",
10283 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10284 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10289 if (end == LUSTRE_EOF && length != 0)
10290 end = start + length;
10292 if (end <= start) {
10293 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10294 argv[0], start, end);
10298 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10299 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10304 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10305 fprintf(stderr, "%s: mode is required with lockahead\n",
10310 while (optind < argc) {
10313 path = argv[optind++];
10315 fd = open(path, O_RDONLY);
10317 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10318 argv[0], path, strerror(errno));
10323 advice.lla_start = start;
10324 advice.lla_end = end;
10325 advice.lla_advice = advice_type;
10326 advice.lla_value1 = 0;
10327 advice.lla_value2 = 0;
10328 advice.lla_value3 = 0;
10329 advice.lla_value4 = 0;
10330 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10331 advice.lla_lockahead_mode = mode;
10332 advice.lla_peradvice_flags = flags;
10335 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10339 "%s: cannot give advice '%s' to file '%s': %s\n",
10340 argv[0], ladvise_names[advice_type],
10341 path, strerror(errno));
10347 if (rc == 0 && rc2 < 0)
10353 static const char *const heat_names[] = LU_HEAT_NAMES;
10355 static int lfs_heat_get(int argc, char **argv)
10357 struct lu_heat *heat;
10366 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10368 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10373 while (optind < argc) {
10374 path = argv[optind++];
10376 fd = open(path, O_RDONLY);
10378 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10379 argv[0], path, strerror(errno));
10384 heat->lh_count = OBD_HEAT_COUNT;
10385 rc2 = llapi_heat_get(fd, heat);
10389 "%s: cannot get heat of file '%s': %s\n",
10390 argv[0], path, strerror(errno));
10394 printf("flags: %x\n", heat->lh_flags);
10395 for (i = 0; i < heat->lh_count; i++)
10396 printf("%s: %llu\n", heat_names[i],
10397 (unsigned long long)heat->lh_heat[i]);
10399 if (rc == 0 && rc2 < 0)
10407 static int lfs_heat_set(int argc, char **argv)
10409 struct option long_opts[] = {
10410 { .val = 'c', .name = "clear", .has_arg = no_argument },
10411 { .val = 'h', .name = "help", .has_arg = no_argument },
10412 { .val = 'o', .name = "off", .has_arg = no_argument },
10413 { .val = 'O', .name = "on", .has_arg = no_argument },
10414 { .name = NULL } };
10415 enum lu_heat_flag flags = 0;
10425 while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
10428 flags |= LU_HEAT_FLAG_CLEAR;
10431 flags |= LU_HEAT_FLAG_CLEAR;
10432 flags |= LU_HEAT_FLAG_OFF;
10435 flags &= ~LU_HEAT_FLAG_OFF;
10438 fprintf(stderr, "%s: unrecognized option '%s'\n",
10439 progname, argv[optind - 1]);
10446 if (argc <= optind) {
10447 fprintf(stderr, "%s: please give one or more file names\n",
10452 while (optind < argc) {
10453 path = argv[optind++];
10455 fd = open(path, O_RDONLY);
10457 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10458 argv[0], path, strerror(errno));
10463 rc2 = llapi_heat_set(fd, flags);
10467 "%s: cannot setflags heat of file '%s': %s\n",
10468 argv[0], path, strerror(errno));
10472 if (rc == 0 && rc2 < 0)
10479 * The input string contains a comma delimited list of component ids and
10480 * ranges, for example "1,2-4,7".
10482 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10484 bool end_of_loop = false;
10492 while (!end_of_loop) {
10496 char *endptr = NULL;
10499 ptr = strchrnul(arg, ',');
10500 end_of_loop = *ptr == '\0';
10503 start_index = strtol(arg, &endptr, 0);
10504 if (endptr == arg) /* no data at all */
10506 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10508 if (start_index < 0)
10511 end_index = start_index;
10512 if (*endptr == '-') {
10513 end_index = strtol(endptr + 1, &endptr, 0);
10514 if (*endptr != '\0')
10516 if (end_index < start_index)
10520 for (i = start_index; i <= end_index && size > 0; i++) {
10523 /* remove duplicate */
10524 for (j = 0; j < nr; j++) {
10528 if (j == nr) { /* no duplicate */
10534 if (size == 0 && i < end_index)
10541 if (!end_of_loop && ptr)
10544 return rc < 0 ? rc : nr;
10548 * struct verify_mirror_id - Mirror id to be verified.
10549 * @mirror_id: A specified mirror id.
10550 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10552 struct verify_mirror_id {
10558 * compare_mirror_ids() - Compare mirror ids.
10559 * @layout: Mirror component list.
10560 * @cbdata: Callback data in verify_mirror_id structure.
10562 * This is a callback function called by llapi_layout_comp_iterate()
10563 * to compare the specified mirror id with the one in the current
10564 * component of @layout. If they are the same, then the specified
10565 * mirror id is valid.
10567 * Return: a negative error code on failure or
10568 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10569 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
10572 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10574 struct verify_mirror_id *mirror_id_cbdata =
10575 (struct verify_mirror_id *)cbdata;
10576 uint32_t mirror_id;
10579 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10583 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10584 progname, strerror(errno));
10588 if (mirror_id_cbdata->mirror_id == mirror_id) {
10589 mirror_id_cbdata->is_valid_id = true;
10590 return LLAPI_LAYOUT_ITER_STOP;
10593 return LLAPI_LAYOUT_ITER_CONT;
10597 * verify_mirror_ids() - Verify specified mirror ids.
10598 * @fname: Mirrored file name.
10599 * @mirror_ids: Specified mirror ids to be verified.
10600 * @ids_nr: Number of specified mirror ids.
10602 * This function verifies that specified @mirror_ids are valid
10603 * in the mirrored file @fname.
10605 * Return: 0 on success or a negative error code on failure.
10608 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10610 struct llapi_layout *layout = NULL;
10611 struct verify_mirror_id mirror_id_cbdata = { 0 };
10613 uint32_t flr_state;
10622 if (stat(fname, &stbuf) < 0) {
10623 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10624 progname, fname, strerror(errno));
10629 if (!S_ISREG(stbuf.st_mode)) {
10630 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10636 fd = open(fname, O_DIRECT | O_RDONLY);
10638 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10639 progname, fname, strerror(errno));
10644 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10646 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10647 progname, fname, strerror(errno));
10651 layout = llapi_layout_get_by_fd(fd, 0);
10653 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10654 progname, fname, strerror(errno));
10656 llapi_lease_release(fd);
10660 rc = llapi_layout_flags_get(layout, &flr_state);
10662 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10663 progname, fname, strerror(errno));
10668 flr_state &= LCM_FL_FLR_MASK;
10669 switch (flr_state) {
10672 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10673 progname, fname, llapi_layout_flags_string(flr_state));
10680 for (i = 0; i < ids_nr; i++) {
10681 mirror_id_cbdata.mirror_id = mirror_ids[i];
10682 mirror_id_cbdata.is_valid_id = false;
10684 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10685 &mirror_id_cbdata);
10689 "%s: '%s' failed to verify mirror id: %u.\n",
10690 progname, fname, mirror_ids[i]);
10694 if (!mirror_id_cbdata.is_valid_id) {
10697 "%s: '%s' invalid specified mirror id: %u.\n",
10698 progname, fname, mirror_ids[i]);
10704 llapi_layout_free(layout);
10705 llapi_lease_release(fd);
10713 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10714 __u16 *mirror_ids, int ids_nr)
10716 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10717 struct llapi_layout *layout;
10719 uint32_t flr_state;
10728 if (stat(fname, &stbuf) < 0) {
10729 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10730 progname, fname, strerror(errno));
10734 if (!S_ISREG(stbuf.st_mode)) {
10735 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10741 /* Allow mirror resync even without the key on encrypted files */
10742 fd = open(fname, O_DIRECT | O_RDWR | O_FILE_ENC);
10744 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10745 progname, fname, strerror(errno));
10750 layout = llapi_layout_get_by_fd(fd, 0);
10752 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10753 progname, fname, strerror(errno));
10758 rc = llapi_layout_flags_get(layout, &flr_state);
10760 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10761 progname, fname, strerror(errno));
10766 flr_state &= LCM_FL_FLR_MASK;
10767 if (flr_state == LCM_FL_NONE) {
10769 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10774 /* get stale component info */
10775 comp_size = llapi_mirror_find_stale(layout, comp_array,
10776 ARRAY_SIZE(comp_array),
10777 mirror_ids, ids_nr);
10778 if (comp_size <= 0) {
10783 ioc->lil_mode = LL_LEASE_WRLCK;
10784 ioc->lil_flags = LL_LEASE_RESYNC;
10785 rc = llapi_lease_set(fd, ioc);
10787 if (rc == -EALREADY)
10791 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10792 progname, fname, strerror(-rc));
10796 /* get the read range [start, end) */
10797 start = comp_array[0].lrc_start;
10798 end = comp_array[0].lrc_end;
10799 for (idx = 1; idx < comp_size; idx++) {
10800 if (comp_array[idx].lrc_start < start)
10801 start = comp_array[idx].lrc_start;
10802 if (end < comp_array[idx].lrc_end)
10803 end = comp_array[idx].lrc_end;
10806 rc = llapi_lease_check(fd);
10807 if (rc != LL_LEASE_WRLCK) {
10808 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10813 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10816 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10817 progname, fname, strerror(-rc));
10819 rc = migrate_set_timestamps(fd, &stbuf);
10821 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
10822 progname, fname, strerror(-rc));
10826 /* need to do the lease unlock even resync fails */
10827 ioc->lil_mode = LL_LEASE_UNLCK;
10828 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10829 ioc->lil_count = 0;
10830 for (idx = 0; idx < comp_size; idx++) {
10831 if (comp_array[idx].lrc_synced) {
10832 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10837 rc2 = llapi_lease_set(fd, ioc);
10839 * llapi_lease_set returns lease mode when it request to unlock
10843 /* rc2 == 0 means lost lease lock */
10844 if (rc2 == 0 && rc == 0)
10848 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10850 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10852 llapi_lease_release(fd);
10857 llapi_layout_free(layout);
10864 static inline int lfs_mirror_resync(int argc, char **argv)
10866 struct option long_opts[] = {
10867 { .val = 'h', .name = "help", .has_arg = no_argument },
10868 { .val = 'o', .name = "only", .has_arg = required_argument },
10869 { .name = NULL } };
10870 struct ll_ioc_lease *ioc = NULL;
10871 __u16 mirror_ids[128] = { 0 };
10876 while ((c = getopt_long(argc, argv, "ho:", long_opts, NULL)) >= 0) {
10879 rc = parse_mirror_ids(mirror_ids,
10880 sizeof(mirror_ids) / sizeof(__u16),
10884 "%s: bad mirror ids '%s'.\n",
10891 fprintf(stderr, "%s: unrecognized option '%s'\n",
10892 progname, argv[optind - 1]);
10900 if (argc == optind) {
10901 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10906 if (ids_nr > 0 && argc > optind + 1) {
10908 "%s: option '--only' cannot be used upon multiple files.\n",
10915 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10920 /* set the lease on the file */
10921 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10923 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10924 argv[0], strerror(errno));
10929 for (; optind < argc; optind++) {
10930 rc = lfs_mirror_resync_file(argv[optind], ioc,
10931 mirror_ids, ids_nr);
10932 /* ignore previous file's error, continue with next file */
10935 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10943 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10945 struct llapi_layout *layout;
10948 layout = llapi_layout_get_by_fd(fd, 0);
10950 fprintf(stderr, "could not get layout.\n");
10954 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10956 fprintf(stderr, "failed to iterate layout\n");
10957 llapi_layout_free(layout);
10960 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10961 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10962 llapi_layout_free(layout);
10966 llapi_layout_free(layout);
10972 * Check whether two files are the same file
10973 * \retval 0 same file
10974 * \retval 1 not the same file
10975 * \retval <0 error code
10977 static inline int check_same_file(int fd, const char *f2)
10979 struct stat stbuf1;
10980 struct stat stbuf2;
10982 if (fstat(fd, &stbuf1) < 0)
10985 if (stat(f2, &stbuf2) < 0)
10988 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10989 stbuf1.st_ino == stbuf2.st_ino)
10995 static inline int lfs_mirror_read(int argc, char **argv)
10998 __u16 mirror_id = 0;
10999 const char *outfile = NULL;
11005 const size_t buflen = 4 << 20;
11007 struct option long_opts[] = {
11008 { .val = 'h', .name = "help", .has_arg = no_argument },
11009 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11010 { .val = 'o', .name = "outfile", .has_arg = required_argument },
11011 { .name = NULL } };
11013 while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
11018 unsigned long int id;
11021 id = strtoul(optarg, &end, 0);
11022 if (errno != 0 || *end != '\0' || id == 0 ||
11025 "%s %s: invalid mirror ID '%s'\n",
11026 progname, argv[0], optarg);
11030 mirror_id = (__u16)id;
11037 fprintf(stderr, "%s: unrecognized option '%s'\n",
11038 progname, argv[optind - 1]);
11045 if (argc == optind) {
11046 fprintf(stderr, "%s %s: no mirrored file provided\n",
11047 progname, argv[0]);
11049 } else if (argc > optind + 1) {
11050 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11054 if (mirror_id == 0) {
11055 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11056 progname, argv[0]);
11060 /* open mirror file */
11061 fname = argv[optind];
11062 fd = open(fname, O_DIRECT | O_RDONLY);
11064 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11065 progname, argv[0], fname, strerror(errno));
11069 /* verify mirror id */
11070 rc = verify_mirror_id_by_fd(fd, mirror_id);
11073 "%s %s: cannot find mirror with ID %u in '%s'\n",
11074 progname, argv[0], mirror_id, fname);
11078 /* open output file - O_EXCL ensures output is not the same as input */
11080 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
11082 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
11083 progname, argv[0], outfile, strerror(errno));
11088 outfd = STDOUT_FILENO;
11091 /* allocate buffer */
11092 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
11094 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11095 progname, argv[0], rc);
11101 ssize_t bytes_read;
11102 ssize_t written = 0;
11104 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
11105 if (bytes_read < 0) {
11108 "%s %s: fail to read data from mirror %u: %s\n",
11109 progname, argv[0], mirror_id, strerror(-rc));
11114 if (bytes_read == 0)
11117 while (written < bytes_read) {
11120 written2 = write(outfd, buf + written,
11121 bytes_read - written);
11122 if (written2 < 0) {
11124 "%s %s: fail to write %s: %s\n",
11125 progname, argv[0], outfile ? : "STDOUT",
11130 written += written2;
11133 if (written != bytes_read) {
11135 "%s %s: written %ld bytes does not match with %ld read.\n",
11136 progname, argv[0], written, bytes_read);
11158 static inline int lfs_mirror_write(int argc, char **argv)
11161 __u16 mirror_id = 0;
11162 const char *inputfile = NULL;
11168 const size_t buflen = 4 << 20;
11170 size_t page_size = sysconf(_SC_PAGESIZE);
11171 struct ll_ioc_lease_id ioc;
11172 struct option long_opts[] = {
11173 { .val = 'h', .name = "help", .has_arg = no_argument },
11174 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
11175 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11176 { .name = NULL } };
11178 while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11183 unsigned long int id;
11186 id = strtoul(optarg, &end, 0);
11187 if (errno != 0 || *end != '\0' || id == 0 ||
11190 "%s %s: invalid mirror ID '%s'\n",
11191 progname, argv[0], optarg);
11195 mirror_id = (__u16)id;
11199 inputfile = optarg;
11202 fprintf(stderr, "%s: unrecognized option '%s'\n",
11203 progname, argv[optind - 1]);
11210 if (argc == optind) {
11211 fprintf(stderr, "%s %s: no mirrored file provided\n",
11212 progname, argv[0]);
11214 } else if (argc > optind + 1) {
11215 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11219 if (mirror_id == 0) {
11220 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11221 progname, argv[0]);
11225 /* open mirror file */
11226 fname = argv[optind];
11227 fd = open(fname, O_DIRECT | O_WRONLY);
11229 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11230 progname, argv[0], fname, strerror(errno));
11234 /* verify mirror id */
11235 rc = verify_mirror_id_by_fd(fd, mirror_id);
11238 "%s %s: cannot find mirror with ID %u in '%s'\n",
11239 progname, argv[0], mirror_id, fname);
11243 /* open input file */
11245 rc = check_same_file(fd, inputfile);
11248 "%s %s: input file cannot be the mirrored file\n",
11249 progname, argv[0]);
11255 inputfd = open(inputfile, O_RDONLY, 0644);
11257 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11258 progname, argv[0], inputfile, strerror(errno));
11263 inputfd = STDIN_FILENO;
11266 /* allocate buffer */
11267 rc = posix_memalign(&buf, page_size, buflen);
11269 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11270 progname, argv[0], rc);
11271 goto close_inputfd;
11274 /* prepare target mirror components instantiation */
11275 ioc.lil_mode = LL_LEASE_WRLCK;
11276 ioc.lil_flags = LL_LEASE_RESYNC;
11277 ioc.lil_mirror_id = mirror_id;
11278 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11281 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11282 progname, argv[0], fname, strerror(errno));
11288 ssize_t bytes_read;
11292 rc = llapi_lease_check(fd);
11293 if (rc != LL_LEASE_WRLCK) {
11294 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11295 progname, argv[0], fname);
11299 bytes_read = read(inputfd, buf, buflen);
11300 if (bytes_read < 0) {
11303 "%s %s: fail to read data from '%s': %s\n",
11304 progname, argv[0], inputfile ? : "STDIN",
11311 if (bytes_read == 0)
11314 /* round up to page align to make direct IO happy. */
11315 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11317 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11322 "%s %s: fail to write to mirror %u: %s\n",
11323 progname, argv[0], mirror_id,
11331 if (pos & (page_size - 1)) {
11332 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11337 ioc.lil_mode = LL_LEASE_UNLCK;
11338 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11340 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11345 "%s %s: release lease lock of '%s' failed: %s\n",
11346 progname, argv[0], fname, strerror(errno));
11363 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11365 struct llapi_layout *layout;
11366 struct collect_ids_data cid = { .cid_ids = ids,
11368 .cid_exclude = exclude_id, };
11371 layout = llapi_layout_get_by_fd(fd, 0);
11373 fprintf(stderr, "could not get layout\n");
11377 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11379 fprintf(stderr, "failed to iterate layout\n");
11380 llapi_layout_free(layout);
11384 llapi_layout_free(layout);
11386 return cid.cid_count;
11389 #ifndef MIRROR_ID_NEG
11390 #define MIRROR_ID_NEG 0x8000
11393 static inline int lfs_mirror_copy(int argc, char **argv)
11396 __u16 read_mirror_id = 0;
11397 __u16 ids[128] = { 0 };
11399 struct llapi_layout *layout = NULL;
11400 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11407 struct ll_ioc_lease *ioc = NULL;
11408 struct ll_ioc_lease_id *resync_ioc;
11409 struct option long_opts[] = {
11410 { .val = 'h', .name = "help", .has_arg = no_argument },
11411 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
11412 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
11413 { .name = NULL } };
11414 char cmd[PATH_MAX];
11416 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11418 while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
11423 unsigned long int id;
11426 id = strtoul(optarg, &end, 0);
11427 if (errno != 0 || *end != '\0' || id == 0 ||
11430 "%s: invalid read mirror ID '%s'\n",
11435 read_mirror_id = (__u16)id;
11439 if (!strcmp(optarg, "-1")) {
11440 /* specify all other mirrors */
11441 ids[0] = (__u16)-1;
11444 count = parse_mirror_ids((__u16 *)ids,
11452 fprintf(stderr, "%s: unrecognized option '%s'\n",
11453 progname, argv[optind - 1]);
11460 if (argc == optind) {
11461 fprintf(stderr, "%s %s: no mirrored file provided\n",
11462 progname, argv[0]);
11464 } else if (argc > optind + 1) {
11465 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11469 if (read_mirror_id == 0) {
11471 "%s %s: no valid read mirror ID %d is provided\n",
11472 progname, argv[0], read_mirror_id);
11478 "%s %s: no write mirror ID is provided\n",
11479 progname, argv[0]);
11483 for (i = 0; i < count; i++) {
11484 if (read_mirror_id == ids[i]) {
11486 "%s %s: read and write mirror ID cannot be the same\n",
11487 progname, argv[0]);
11492 /* open mirror file */
11493 fname = argv[optind];
11495 fd = open(fname, O_DIRECT | O_RDWR);
11497 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11498 progname, argv[0], fname, strerror(errno));
11502 /* write to all other mirrors */
11503 if (ids[0] == (__u16)-1) {
11504 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11508 "%s %s: failed to get other mirror ids in '%s': %d\n",
11509 progname, argv[0], fname, rc);
11514 /* verify mirror id */
11515 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11518 "%s %s: cannot find mirror with ID %u in '%s'\n",
11519 progname, argv[0], read_mirror_id, fname);
11523 for (i = 0; i < count; i++) {
11524 rc = verify_mirror_id_by_fd(fd, ids[i]);
11527 "%s %s: cannot find mirror with ID %u in '%s'\n",
11528 progname, argv[0], ids[i], fname);
11533 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11536 "%s %s: cannot alloc comp id array for ioc: %s\n",
11537 progname, argv[0], strerror(errno));
11542 /* get stale component info */
11543 layout = llapi_layout_get_by_fd(fd, 0);
11545 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11546 progname, argv[0], fname, strerror(errno));
11550 comp_size = llapi_mirror_find_stale(layout, comp_array,
11551 ARRAY_SIZE(comp_array),
11553 llapi_layout_free(layout);
11554 if (comp_size < 0) {
11559 /* prepare target mirror components instantiation */
11560 resync_ioc = (struct ll_ioc_lease_id *)ioc;
11561 resync_ioc->lil_mode = LL_LEASE_WRLCK;
11562 resync_ioc->lil_flags = LL_LEASE_RESYNC;
11564 resync_ioc->lil_mirror_id = ids[0];
11566 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11567 rc = llapi_lease_set(fd, ioc);
11570 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11571 progname, argv[0], fname, strerror(errno));
11575 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11578 fprintf(stderr, "%s %s: copy error: %d\n",
11579 progname, argv[0], rc);
11583 fprintf(stdout, "mirror copied successfully: ");
11584 for (i = 0; i < copied; i++)
11585 fprintf(stdout, "%d ", ids[i]);
11586 fprintf(stdout, "\n");
11588 ioc->lil_mode = LL_LEASE_UNLCK;
11589 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11590 ioc->lil_count = 0;
11591 for (i = 0; i < comp_size; i++) {
11594 for (j = 0; j < copied; j++) {
11595 if (comp_array[i].lrc_mirror_id != ids[j])
11598 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11602 rc = llapi_lease_set(fd, ioc);
11607 "%s %s: release lease lock of '%s' failed: %s\n",
11608 progname, argv[0], fname, strerror(errno));
11623 * struct verify_chunk - Mirror chunk to be verified.
11624 * @chunk: [start, end) of the chunk.
11625 * @mirror_count: Number of mirror ids in @mirror_id array.
11626 * @mirror_id: Array of valid mirror ids that cover the chunk.
11628 struct verify_chunk {
11629 struct lu_extent chunk;
11630 unsigned int mirror_count;
11631 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11635 * print_chunks() - Print chunk information.
11636 * @fname: Mirrored file name.
11637 * @chunks: Array of chunks.
11638 * @chunk_count: Number of chunks in @chunks array.
11640 * This function prints [start, end) of each chunk in @chunks
11641 * for mirrored file @fname, and also prints the valid mirror ids
11642 * that cover the chunk.
11647 void print_chunks(const char *fname, struct verify_chunk *chunks,
11653 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11654 for (i = 0; i < chunk_count; i++) {
11655 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11657 if (chunks[i].mirror_count == 0)
11658 fprintf(stdout, "\t[");
11660 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11661 for (j = 1; j < chunks[i].mirror_count; j++)
11662 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11664 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11666 fprintf(stdout, "\n");
11670 * print_checksums() - Print CRC-32 checksum values.
11671 * @chunk: A chunk and its corresponding valid mirror ids.
11672 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
11674 * This function prints CRC-32 checksum values on @chunk for
11675 * each valid mirror that covers it.
11680 void print_checksums(struct verify_chunk *chunk, unsigned long *crc,
11681 unsigned long long pos, unsigned long long len)
11686 "CRC-32 checksum value for chunk "DEXT":\n", pos, pos + len);
11687 for (i = 0; i < chunk->mirror_count; i++)
11688 fprintf(stdout, "Mirror %u:\t%#lx\n",
11689 chunk->mirror_id[i], crc[i]);
11690 fprintf(stdout, "\n");
11694 * filter_mirror_id() - Filter specified mirror ids.
11695 * @chunks: Array of chunks.
11696 * @chunk_count: Number of chunks in @chunks array.
11697 * @mirror_ids: Specified mirror ids to be verified.
11698 * @ids_nr: Number of specified mirror ids.
11700 * This function scans valid mirror ids that cover each chunk in @chunks
11701 * and filters specified mirror ids.
11706 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11707 __u16 *mirror_ids, int ids_nr)
11712 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11713 unsigned int valid_count = 0;
11715 for (i = 0; i < chunk_count; i++) {
11716 if (chunks[i].mirror_count == 0)
11720 for (j = 0; j < ids_nr; j++) {
11721 for (k = 0; k < chunks[i].mirror_count; k++) {
11722 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11723 valid_id[valid_count] = mirror_ids[j];
11730 memcpy(chunks[i].mirror_id, valid_id,
11731 sizeof(__u16) * valid_count);
11732 chunks[i].mirror_count = valid_count;
11737 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11738 * @layout: Mirror component list.
11739 * @chunks: Array of chunks.
11740 * @chunks_size: Array size of @chunks.
11742 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11743 * to find out chunk segments and store them in @chunks array.
11745 * The @mirror_id array in each element of @chunks will store the valid
11746 * mirror ids that cover the chunk. If a mirror component covering the
11747 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11748 * will not be stored into the @mirror_id array, and the chunk for that
11749 * mirror will not be verified.
11751 * The @mirror_count in each element of @chunks will store the number of
11752 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11753 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11754 * indicates the chunk is valid in only one mirror. In both cases, the
11755 * chunk will not be verified.
11757 * Here is an example:
11759 * 0 1M 2M 3M 4M EOF
11760 * +------+-------------+--------------------+
11761 * | | | S | mirror1
11762 * +------+------+------+------+-------------+
11763 * | | S | S | | mirror2
11764 * +-------------+------+------+-------------+
11766 * prepared @chunks array will contain 5 elements:
11767 * (([0, 1M), [1, 2], 2),
11768 * ([1M, 2M), [1, 2], 2),
11769 * ([2M, 3M), [1], 1),
11770 * ([3M, 4M], [], 0),
11771 * ([4M, EOF), [2], 1))
11773 * Return: the actual array size of @chunks on success
11774 * or a negative error code on failure.
11777 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11778 struct verify_chunk *chunks,
11779 size_t chunks_size)
11783 uint32_t mirror_id;
11789 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11792 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11795 "%s: move to the first layout component: %s.\n",
11796 progname, strerror(errno));
11802 chunks[idx].chunk.e_end = LUSTRE_EOF;
11804 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11807 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11808 progname, strerror(errno));
11812 if (start > chunks[idx].chunk.e_start ||
11813 end <= chunks[idx].chunk.e_start)
11816 if (end < chunks[idx].chunk.e_end)
11817 chunks[idx].chunk.e_end = end;
11819 rc = llapi_layout_comp_flags_get(layout, &flags);
11822 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11823 progname, strerror(errno));
11827 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11830 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11833 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11834 progname, strerror(errno));
11838 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11840 "%s: mirror_id array is too small.\n",
11845 chunks[idx].mirror_id[i] = mirror_id;
11849 rc = llapi_layout_comp_use(layout,
11850 LLAPI_LAYOUT_COMP_USE_NEXT);
11853 "%s: move to the next layout component: %s.\n",
11854 progname, strerror(errno));
11857 } /* loop through all components */
11859 chunks[idx].mirror_count = i;
11861 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11865 if (idx >= chunks_size) {
11866 fprintf(stderr, "%s: chunks array is too small.\n",
11872 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11876 return rc < 0 ? rc : idx + 1;
11880 * lfs_mirror_verify_chunk() - Verify a chunk.
11881 * @fd: File descriptor of the mirrored file.
11882 * @file_size: Size of the mirrored file.
11883 * @chunk: A chunk and its corresponding valid mirror ids.
11884 * @verbose: Verbose mode.
11886 * This function verifies a @chunk contains exactly the same data
11887 * ammong the mirrors that cover it.
11889 * If @verbose is specified, then the function will print where the
11890 * differences are if the data do not match. Otherwise, it will
11891 * just return an error in that case.
11893 * Return: 0 on success or a negative error code on failure.
11896 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11897 struct verify_chunk *chunk, int verbose)
11899 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11901 size_t page_size = sysconf(_SC_PAGESIZE);
11902 ssize_t bytes_read;
11903 ssize_t bytes_done;
11907 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11911 if (file_size == 0)
11914 rc = posix_memalign(&buf, page_size, buflen);
11915 if (rc) /* error code is returned directly */
11919 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11920 PEXT(&chunk->chunk));
11921 for (i = 0; i < chunk->mirror_count; i++)
11922 fprintf(stdout, " %u", chunk->mirror_id[i]);
11923 fprintf(stdout, "\n");
11927 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11928 pos = chunk->chunk.e_start;
11929 while (bytes_done < count) {
11930 /* compute initial CRC-32 checksum */
11931 crc = crc32(0L, Z_NULL, 0);
11932 memset(crc_array, 0, sizeof(crc_array));
11935 for (i = 0; i < chunk->mirror_count; i++) {
11936 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11938 if (bytes_read < 0) {
11941 "%s: failed to read data from mirror %u: %s.\n",
11942 progname, chunk->mirror_id[i],
11947 /* compute new CRC-32 checksum */
11948 crc_array[i] = crc32(crc, buf, bytes_read);
11952 print_checksums(chunk, crc_array, pos, buflen);
11954 /* compare CRC-32 checksum values */
11955 for (i = 1; i < chunk->mirror_count; i++) {
11956 if (crc_array[i] != crc_array[0]) {
11960 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11961 progname, PEXT(&chunk->chunk),
11962 chunk->mirror_id[0],
11963 chunk->mirror_id[i]);
11968 bytes_done += bytes_read;
11971 if (verbose > 1 && rc == 0) {
11972 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11973 PEXT(&chunk->chunk));
11974 for (i = 0; i < chunk->mirror_count; i++)
11975 fprintf(stdout, " %u", chunk->mirror_id[i]);
11976 fprintf(stdout, " PASS\n\n");
11985 * lfs_mirror_verify_file() - Verify a mirrored file.
11986 * @fname: Mirrored file name.
11987 * @mirror_ids: Specified mirror ids to be verified.
11988 * @ids_nr: Number of specified mirror ids.
11989 * @verbose: Verbose mode.
11991 * This function verifies that each SYNC mirror of a mirrored file
11992 * specified by @fname contains exactly the same data.
11994 * If @mirror_ids is specified, then the function will verify the
11995 * mirrors specified by @mirror_ids contain exactly the same data.
11997 * If @verbose is specified, then the function will print where the
11998 * differences are if the data do not match. Otherwise, it will
11999 * just return an error in that case.
12001 * Return: 0 on success or a negative error code on failure.
12004 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
12007 struct verify_chunk chunks_array[1024] = { };
12008 struct llapi_layout *layout = NULL;
12010 uint32_t flr_state;
12012 int chunk_count = 0;
12018 if (stat(fname, &stbuf) < 0) {
12019 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
12020 progname, fname, strerror(errno));
12025 if (!S_ISREG(stbuf.st_mode)) {
12026 fprintf(stderr, "%s: '%s' is not a regular file.\n",
12032 if (stbuf.st_size == 0) {
12034 fprintf(stdout, "%s: '%s' file size is 0.\n",
12040 /* Allow mirror verify even without the key on encrypted files */
12041 fd = open(fname, O_DIRECT | O_RDONLY | O_FILE_ENC);
12043 fprintf(stderr, "%s: cannot open '%s': %s.\n",
12044 progname, fname, strerror(errno));
12049 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
12051 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
12052 progname, fname, strerror(errno));
12056 layout = llapi_layout_get_by_fd(fd, 0);
12058 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
12059 progname, fname, strerror(errno));
12061 llapi_lease_release(fd);
12065 rc = llapi_layout_flags_get(layout, &flr_state);
12067 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
12068 progname, fname, strerror(errno));
12073 flr_state &= LCM_FL_FLR_MASK;
12074 switch (flr_state) {
12077 fprintf(stderr, "%s: '%s' file state error: %s.\n",
12078 progname, fname, llapi_layout_flags_string(flr_state));
12084 /* find out mirror chunks to be verified */
12085 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
12086 ARRAY_SIZE(chunks_array));
12087 if (chunk_count < 0) {
12093 /* filter specified mirror ids */
12094 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
12097 print_chunks(fname, chunks_array, chunk_count);
12099 for (idx = 0; idx < chunk_count; idx++) {
12100 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
12103 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
12105 PEXT(&chunks_array[idx].chunk),
12106 (unsigned long long)stbuf.st_size);
12110 if (chunks_array[idx].mirror_count == 0) {
12112 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
12114 PEXT(&chunks_array[idx].chunk));
12116 fprintf(stderr, "skipped\n");
12120 fprintf(stderr, "failed\n");
12124 if (chunks_array[idx].mirror_count == 1) {
12127 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12129 PEXT(&chunks_array[idx].chunk),
12130 chunks_array[idx].mirror_id[0]);
12134 rc = llapi_lease_check(fd);
12135 if (rc != LL_LEASE_RDLCK) {
12136 fprintf(stderr, "%s: '%s' lost lease lock.\n",
12141 /* verify one chunk */
12142 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12143 &chunks_array[idx], verbose);
12157 llapi_layout_free(layout);
12158 llapi_lease_release(fd);
12166 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12167 * @argc: The count of lfs mirror verify command line arguments.
12168 * @argv: Array of strings for lfs mirror verify command line arguments.
12170 * This function parses lfs mirror verify command and verifies the
12171 * specified mirrored file(s).
12173 * Return: 0 on success or a negative error code on failure.
12175 static inline int lfs_mirror_verify(int argc, char **argv)
12177 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12183 char cmd[PATH_MAX];
12185 struct option long_opts[] = {
12186 { .val = 'h', .name = "help", .has_arg = no_argument },
12187 { .val = 'o', .name = "only", .has_arg = required_argument },
12188 { .val = 'v', .name = "verbose", .has_arg = no_argument },
12189 { .name = NULL } };
12191 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12193 while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12196 rc = parse_mirror_ids(mirror_ids,
12197 ARRAY_SIZE(mirror_ids),
12201 "%s: bad mirror ids '%s'.\n",
12208 "%s: at least 2 mirror ids needed with '--only' option.\n",
12218 fprintf(stderr, "%s: unrecognized option '%s'\n",
12219 progname, argv[optind - 1]);
12227 if (argc == optind) {
12228 fprintf(stderr, "%s: no file name given.\n", progname);
12233 if (ids_nr > 0 && argc > optind + 1) {
12235 "%s: '--only' cannot be used upon multiple files.\n",
12242 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12248 for (; optind < argc; optind++) {
12249 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12259 * lfs_mirror() - Parse and execute lfs mirror commands.
12260 * @argc: The count of lfs mirror command line arguments.
12261 * @argv: Array of strings for lfs mirror command line arguments.
12263 * This function parses lfs mirror commands and performs the
12264 * corresponding functions specified in mirror_cmdlist[].
12266 * Return: 0 on success or an error code on failure.
12268 static int lfs_mirror(int argc, char **argv)
12270 char cmd[PATH_MAX];
12273 setlinebuf(stdout);
12275 Parser_init("lfs-mirror > ", mirror_cmdlist);
12277 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12279 program_invocation_short_name = cmd;
12281 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12283 rc = Parser_commands();
12285 return rc < 0 ? -rc : rc;
12288 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12290 #if __BYTE_ORDER == __BIG_ENDIAN
12291 __swab16s(&attrs->lsa_valid);
12292 __swab64s(&attrs->lsa_size);
12293 __swab64s(&attrs->lsa_blocks);
12297 enum lfs_som_type {
12298 LFS_SOM_SIZE = 0x1,
12299 LFS_SOM_BLOCKS = 0x2,
12300 LFS_SOM_FLAGS = 0x4,
12301 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12305 static int lfs_getsom(int argc, char **argv)
12308 struct lustre_som_attrs *attrs;
12309 char buf[sizeof(*attrs) + 64];
12310 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12313 while ((c = getopt(argc, argv, "bfhs")) != -1) {
12316 type = LFS_SOM_BLOCKS;
12319 type = LFS_SOM_FLAGS;
12322 type = LFS_SOM_SIZE;
12325 fprintf(stderr, "%s: unrecognized option '%s'\n",
12326 progname, argv[optind - 1]);
12337 fprintf(stderr, "%s: %s\n",
12338 progname, argc == 0 ? "miss file target" :
12339 "input more than 2 files");
12344 attrs = (void *)buf;
12345 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12348 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12349 argv[0], strerror(errno), errno);
12353 lustre_som_swab(attrs);
12356 case LFS_SOM_ATTR_ALL:
12357 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12358 path, (unsigned long long)attrs->lsa_size,
12359 (unsigned long long)attrs->lsa_blocks,
12363 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12365 case LFS_SOM_BLOCKS:
12366 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12368 case LFS_SOM_FLAGS:
12369 printf("%x\n", attrs->lsa_valid);
12372 fprintf(stderr, "%s: unknown option\n", progname);
12380 * lfs_mirror_list_commands() - List lfs mirror commands.
12381 * @argc: The count of command line arguments.
12382 * @argv: Array of strings for command line arguments.
12384 * This function lists lfs mirror commands defined in mirror_cmdlist[].
12386 * Return: 0 on success.
12388 static int lfs_mirror_list_commands(int argc, char **argv)
12390 char buffer[81] = "";
12392 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12398 static int lfs_pcc_attach(int argc, char **argv)
12400 struct option long_opts[] = {
12401 { .val = 'h', .name = "help", .has_arg = no_argument },
12402 { .val = 'i', .name = "id", .has_arg = required_argument },
12403 { .name = NULL } };
12406 __u32 archive_id = 0;
12409 char fullpath[PATH_MAX];
12410 enum lu_pcc_type type = LU_PCC_READWRITE;
12413 while ((c = getopt_long(argc, argv, "hi:",
12414 long_opts, NULL)) != -1) {
12418 archive_id = strtoul(optarg, &end, 0);
12419 if (errno != 0 || *end != '\0' ||
12420 archive_id == 0 || archive_id > UINT32_MAX) {
12422 "error: %s: bad archive ID '%s'\n",
12428 fprintf(stderr, "%s: unrecognized option '%s'\n",
12429 progname, argv[optind - 1]);
12436 if (archive_id == 0) {
12437 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12441 if (argc <= optind) {
12442 fprintf(stderr, "%s: must specify one or more file names\n",
12447 while (optind < argc) {
12450 path = argv[optind++];
12451 if (!realpath(path, fullpath)) {
12452 fprintf(stderr, "%s: could not find path '%s': %s\n",
12453 argv[0], path, strerror(errno));
12459 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12462 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12463 argv[0], path, archive_id, strerror(-rc2));
12471 static int lfs_pcc_attach_fid(int argc, char **argv)
12473 struct option long_opts[] = {
12474 { .val = 'h', .name = "help", .has_arg = no_argument },
12475 { .val = 'i', .name = "id", .has_arg = required_argument },
12476 { .val = 'm', .name = "mnt", .has_arg = required_argument },
12477 { .name = NULL } };
12480 __u32 archive_id = 0;
12482 const char *mntpath = NULL;
12483 const char *fidstr;
12484 enum lu_pcc_type type = LU_PCC_READWRITE;
12487 while ((c = getopt_long(argc, argv, "hi:m:",
12488 long_opts, NULL)) != -1) {
12492 archive_id = strtoul(optarg, &end, 0);
12493 if (errno != 0 || *end != '\0' ||
12494 archive_id > UINT32_MAX) {
12496 "error: %s: bad archive ID '%s'\n",
12505 fprintf(stderr, "%s: unrecognized option '%s'\n",
12506 progname, argv[optind - 1]);
12513 if (archive_id == 0) {
12514 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12519 fprintf(stderr, "%s: must specify Lustre mount point\n",
12524 if (argc <= optind) {
12525 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12529 while (optind < argc) {
12532 fidstr = argv[optind++];
12534 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12538 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12539 argv[0], fidstr, mntpath, archive_id,
12542 if (rc == 0 && rc2 < 0)
12548 static int lfs_pcc_detach(int argc, char **argv)
12550 struct option long_opts[] = {
12551 { .val = 'h', .name = "help", .has_arg = no_argument },
12552 { .val = 'k', .name = "keep", .has_arg = no_argument },
12553 { .name = NULL } };
12557 char fullpath[PATH_MAX];
12558 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12561 while ((c = getopt_long(argc, argv, "hk",
12562 long_opts, NULL)) != -1) {
12565 detach_opt = PCC_DETACH_OPT_NONE;
12568 fprintf(stderr, "%s: unrecognized option '%s'\n",
12569 progname, argv[optind - 1]);
12576 while (optind < argc) {
12579 path = argv[optind++];
12580 if (!realpath(path, fullpath)) {
12581 fprintf(stderr, "%s: could not find path '%s': %s\n",
12582 argv[0], path, strerror(errno));
12588 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12592 "%s: cannot detach '%s' from PCC: %s\n",
12593 argv[0], path, strerror(errno));
12601 static int lfs_pcc_detach_fid(int argc, char **argv)
12603 struct option long_opts[] = {
12604 { .val = 'h', .name = "help", .has_arg = no_argument },
12605 { .val = 'k', .name = "keep", .has_arg = no_argument },
12606 { .name = NULL } };
12610 const char *mntpath;
12611 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12614 while ((c = getopt_long(argc, argv, "hk",
12615 long_opts, NULL)) != -1) {
12618 detach_opt = PCC_DETACH_OPT_NONE;
12621 fprintf(stderr, "%s: unrecognized option '%s'\n",
12622 progname, argv[optind - 1]);
12629 mntpath = argv[optind++];
12631 while (optind < argc) {
12634 fid = argv[optind++];
12636 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12639 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12640 argv[0], fid, mntpath, strerror(-rc2));
12648 static int lfs_pcc_state(int argc, char **argv)
12652 char fullpath[PATH_MAX];
12653 struct lu_pcc_state state;
12658 fprintf(stderr, "%s: must specify one or more file names\n",
12663 while (optind < argc) {
12666 path = argv[optind++];
12667 if (!realpath(path, fullpath)) {
12668 fprintf(stderr, "%s: could not find path '%s': %s\n",
12669 argv[0], path, strerror(errno));
12675 rc2 = llapi_pcc_state_get(fullpath, &state);
12680 "%s: cannot get PCC state of '%s': %s\n",
12681 argv[0], path, strerror(-rc2));
12685 printf("file: %s", path);
12686 printf(", type: %s", pcc_type2string(state.pccs_type));
12687 if (state.pccs_type == LU_PCC_NONE &&
12688 state.pccs_open_count == 0) {
12693 printf(", PCC file: %s", state.pccs_path);
12694 printf(", user number: %u", state.pccs_open_count);
12695 printf(", flags: %x", state.pccs_flags);
12702 * lfs_pcc_list_commands() - List lfs pcc commands.
12703 * @argc: The count of command line arguments.
12704 * @argv: Array of strings for command line arguments.
12706 * This function lists lfs pcc commands defined in pcc_cmdlist[].
12708 * Return: 0 on success.
12710 static int lfs_pcc_list_commands(int argc, char **argv)
12712 char buffer[81] = "";
12714 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12721 * lfs_pcc() - Parse and execute lfs pcc commands.
12722 * @argc: The count of lfs pcc command line arguments.
12723 * @argv: Array of strings for lfs pcc command line arguments.
12725 * This function parses lfs pcc commands and performs the
12726 * corresponding functions specified in pcc_cmdlist[].
12728 * Return: 0 on success or an error code on failure.
12730 static int lfs_pcc(int argc, char **argv)
12732 char cmd[PATH_MAX];
12735 setlinebuf(stdout);
12737 Parser_init("lfs-pcc > ", pcc_cmdlist);
12739 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12741 program_invocation_short_name = cmd;
12743 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12745 rc = Parser_commands();
12747 return rc < 0 ? -rc : rc;
12750 static int lfs_list_commands(int argc, char **argv)
12752 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12754 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12759 int main(int argc, char **argv)
12763 /* Ensure that liblustreapi constructor has run */
12764 if (!llapi_liblustreapi_initialized())
12765 fprintf(stderr, "liblustreapi was not properly initialized\n");
12767 setlinebuf(stdout);
12770 Parser_init("lfs > ", cmdlist);
12772 progname = program_invocation_short_name; /* Used in error messages */
12774 llapi_set_command_name(argv[1]);
12775 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12776 llapi_clear_command_name();
12778 rc = Parser_commands();
12781 return rc < 0 ? -rc : rc;
12784 #ifdef _LUSTRE_IDL_H_
12785 /* Everything we need here should be included by lustreapi.h. */
12786 # error "lfs should not depend on lustre_idl.h"
12787 #endif /* _LUSTRE_IDL_H_ */