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"
366 " <directory|filename> ..."},
367 {"setdirstripe", lfs_setdirstripe, 0,
368 "Create striped directory on specified MDT, same as mkdir.\n"
369 "May be restricted to root or group users, depending on settings.\n"
370 "usage: setdirstripe [OPTION] <directory>\n"
372 {"getdirstripe", lfs_getdirstripe, 0,
373 "To list the layout pattern info for a given directory\n"
374 "or recursively for all directories in a directory tree.\n"
375 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
376 " [--help|-h] [--mdt-hash|-H] [--obd|-O UUID]\n"
377 " [--recursive|-r] [--yaml|-y]\n"
378 " [--verbose|-v] [--default|-D]\n"
379 " [--max-inherit|-X]\n"
380 " [--max-inherit-rr] <dir> ..."},
381 {"mkdir", lfs_setdirstripe, 0,
382 "Create striped directory on specified MDT, same as setdirstripe.\n"
383 "usage: mkdir [OPTION] <directory>\n"
385 {"rm_entry", lfs_rmentry, 0,
386 "To remove the name entry of the remote directory. Note: This\n"
387 "command will only delete the name entry, i.e. the remote directory\n"
388 "will become inaccessable after this command. This can only be done\n"
389 "by the administrator\n"
390 "usage: rm_entry <dir>\n"},
391 {"unlink_foreign", lfs_unlink_foreign, 0,
392 "To remove the foreign file/dir.\n"
393 "Note: This is for files/dirs prevented to be removed using\n"
394 "unlink/rmdir, but works also for regular ones\n"
395 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
396 {"pool_list", lfs_poollist, 0,
397 "List pools or pool OSTs\n"
398 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
399 {"find", lfs_find, 0,
400 "find files matching given attributes recursively in directory tree.\n"
401 "usage: find <directory|filename> ...\n"
402 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --btime|-B [+-]N[smhdwy]]\n"
403 " [[!] --ctime|-C [+-]N[smhdwy]] [[!] --mtime|-M [+-]N[smhdwy]]\n"
404 " [[!] --blocks|-b N] [[!] --component-count [+-]<comp_cnt>]\n"
405 " [[!] --component-start [+-]N[kMGTPE]]\n"
406 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
407 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
408 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
409 " [[!] --foreign[=<foreign_type>]]\n"
410 " [[!] --gid|-g|--group|-G <gid>|<gname>] [--help|-h]\n"
411 " [[!] --layout|-L released,raid0,mdt] [--lazy]\n"
412 " [--maxdepth|-D N] [[!] --mdt-count|-T [+-]<stripes>]\n"
413 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
414 " [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
415 " [[!] --mirror-count|-N [+-]<n>]\n"
416 " [[!] --mirror-state <[^]state>]\n"
417 " [[!] --name|-n <pattern>] [[!] --newer[XY] <reference>]\n"
418 " [[!] --ost|-O <uuid|index,...>] [[!] --perm [/-]mode]\n"
419 " [[!] --pool <pool>] [--print|-P] [--print0|-0]\n"
420 " [[!] --projid <projid>] [[!] --size|-s [+-]N[bkMGTPE]]\n"
421 " [[!] --stripe-count|-c [+-]<stripes>]\n"
422 " [[!] --stripe-index|-i <index,...>]\n"
423 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
424 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
425 "\t !: used before an option indicates 'NOT' requested attribute\n"
426 "\t -: used before a value indicates less than requested value\n"
427 "\t +: used before a value indicates more than requested value\n"
428 "\t ^: used before a flag indicates to exclude it\n"},
429 {"check", lfs_check, 0,
430 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
431 "or all the servers (MGTs, MDTs and OSTs).\n"
432 "usage: check {mgts|osts|mdts|all}"},
433 {"osts", lfs_osts, 0, "list OSTs connected to client "
434 "[for specified path only]\n" "usage: osts [path]"},
435 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
436 "[for specified path only]\n" "usage: mdts [path]"},
438 "report filesystem disk space usage or inodes usage "
439 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
440 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
441 " [--pool|-p <fsname>[.<pool>]] [path]"},
442 {"getname", lfs_getname, 0,
443 "list instances and specified mount points [for specified path only]\n"
444 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
445 #ifdef HAVE_SYS_QUOTA_H
446 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
447 "usage: setquota [-t][-d] {-u|-U|-g|-G|-p|-P} {-b|-B|-i|-I LIMIT} [--pool POOL] FILESYSTEM"},
448 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
449 "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
450 " [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID]\n"
451 " [--pool <OST pool name>] <filesystem>\n"
452 " quota -t <-u|-g|-p> [--pool <OST pool name>] <filesystem>\n"
453 " quota [-q] [-v] [h] {-U|-G|-P} [--pool <OST pool name>] <filesystem>"},
454 {"project", lfs_project, 0,
455 "Change or list project attribute for specified file or directory.\n"
456 "usage: project [-d|-r] <file|directory...>\n"
457 " list project ID and flags on file(s) or directories\n"
458 " project [-p id] [-s] [-r] <file|directory...>\n"
459 " set project ID and/or inherit flag for specified file(s) or directories\n"
460 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
461 " check project ID and flags on file(s) or directories, print outliers\n"
462 " project -C [-r] [-k] <file|directory...>\n"
463 " clear the project inherit flag and ID on the file or directory\n"
466 {"flushctx", lfs_flushctx, 0,
467 "Flush security context for current user.\n"
468 "usage: flushctx [-k] [-r] [mountpoint...]"},
469 {"changelog", lfs_changelog, 0,
470 "Show the metadata changes on an MDT."
471 "\nusage: changelog <mdtname> [startrec [endrec]]"},
472 {"changelog_clear", lfs_changelog_clear, 0,
473 "Indicate that old changelog records up to <endrec> are no longer of "
474 "interest to consumer <id>, allowing the system to free up space.\n"
475 "An <endrec> of 0 means all records.\n"
476 "usage: changelog_clear <mdtname> <id> <endrec>"},
477 {"fid2path", lfs_fid2path, 0,
478 "Resolve the full path(s) for given FID(s). For a specific hardlink "
479 "specify link number <linkno>.\n"
480 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
481 "<fsname|root> <fid>..."},
482 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
483 "usage: path2fid [--parents] <path> ..."},
484 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
485 "usage: rmfid <fsname|rootpath> <fid> ..."},
486 {"data_version", lfs_data_version, 0, "Display file data version for "
487 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
488 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
489 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
490 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
491 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
492 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
493 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
495 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
496 "[--archived] [--lost] <file> ..."},
497 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
498 "given files.\n" "usage: hsm_action <file> ..."},
499 {"hsm_archive", lfs_hsm_archive, 0,
500 "Archive file to external storage.\n"
501 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
503 {"hsm_restore", lfs_hsm_restore, 0,
504 "Restore file from external storage.\n"
505 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
506 {"hsm_release", lfs_hsm_release, 0,
507 "Release files from Lustre.\n"
508 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
509 {"hsm_remove", lfs_hsm_remove, 0,
510 "Remove file copy from external storage.\n"
511 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
513 " (FILE [FILE ...] | "
514 "--mntpath MOUNTPATH FID [FID ...])\n"
516 "Note: To remove an archived copy of a file already deleted from a "
518 "--mntpath option and a list of FIDs must be specified"
520 {"hsm_cancel", lfs_hsm_cancel, 0,
521 "Cancel requests related to specified files.\n"
522 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
523 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
524 "usage: swap_layouts <path1> <path2>"},
525 {"migrate", lfs_setstripe_migrate, 0,
526 "migrate directories and their inodes between MDTs.\n"
527 "usage: migrate [--mdt-count|-c STRIPE_COUNT] [--directory|-d]\n"
528 " [--mdt-hash|-H HASH_TYPE]\n"
529 " [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
532 "migrate file objects from one OST layout to another\n"
533 "(may be not safe with concurent writes).\n"
536 "To move directories between MDTs. This command is deprecated, "
537 "use \"migrate\" instead.\n"
538 "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
540 {"ladvise", lfs_ladvise, 0,
541 "Provide servers with advice about access patterns for a file.\n"
542 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
543 " [--background|-b] [--unset|-u]\n\n"
544 " {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
545 " {[--mode|-m [READ,WRITE]}\n"
547 {"mirror", lfs_mirror, mirror_cmdlist,
548 "lfs commands used to manage files with mirrored components:\n"
549 "lfs mirror create - create a mirrored file or directory\n"
550 "lfs mirror extend - add mirror(s) to an existing file\n"
551 "lfs mirror split - split a mirror from an existing mirrored file\n"
552 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
553 "lfs mirror read - read a mirror content of a mirrored file\n"
554 "lfs mirror write - write to a mirror of a mirrored file\n"
555 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
556 "lfs mirror verify - verify mirrored file(s)\n"},
557 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
558 "usage: getsom [-s] [-b] [-f] <path>\n"
559 "\t-s: Only show the size value of the SOM data for a given file\n"
560 "\t-b: Only show the blocks value of the SOM data for a given file\n"
561 "\t-f: Only show the flags value of the SOM data for a given file\n"},
562 {"heat_get", lfs_heat_get, 0,
563 "To get heat of files.\n"
564 "usage: heat_get <file> ...\n"},
565 {"heat_set", lfs_heat_set, 0,
566 "To set heat flags of files.\n"
567 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
568 "\t--clear|-c: Clear file heat for given files\n"
569 "\t--off|-o: Turn off file heat for given files\n"
570 "\t--on|-O: Turn on file heat for given files\n"},
571 {"pcc", lfs_pcc, pcc_cmdlist,
572 "lfs commands used to interact with PCC features:\n"
573 "lfs pcc attach - attach given files to Persistent Client Cache\n"
574 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
575 "lfs pcc state - display the PCC state for given files\n"
576 "lfs pcc detach - detach given files from Persistent Client Cache\n"
577 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
578 {"help", Parser_help, 0, "help"},
579 {"exit", Parser_quit, 0, "quit"},
580 {"quit", Parser_quit, 0, "quit"},
581 {"--version", Parser_version, 0,
582 "output build version of the utility and exit"},
583 {"--list-commands", lfs_list_commands, 0,
584 "list commands supported by the utility and exit"},
588 static int check_hashtype(const char *hashtype)
590 int type_num = atoi(hashtype);
593 /* numeric hash type */
594 if (hashtype && strlen(hashtype) == 1 &&
595 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
597 /* string hash type */
598 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
599 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
605 static uint32_t check_foreign_type_name(const char *foreign_type_name)
609 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
610 if (!lu_foreign_types[i].lft_name)
612 if (strcmp(foreign_type_name,
613 lu_foreign_types[i].lft_name) == 0)
614 return lu_foreign_types[i].lft_type;
617 return LU_FOREIGN_TYPE_UNKNOWN;
620 static const char *error_loc = "syserror";
623 migrate_open_files(const char *name, __u64 migration_flags,
624 const struct llapi_stripe_param *param,
625 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
632 char parent[PATH_MAX];
633 char volatile_file[PATH_MAX];
639 if (!param && !layout) {
640 error_loc = "layout information";
644 /* search for file directory pathname */
645 if (strlen(name) > sizeof(parent) - 1) {
646 error_loc = "source file name";
650 strncpy(parent, name, sizeof(parent));
651 ptr = strrchr(parent, '/');
653 if (!getcwd(parent, sizeof(parent))) {
654 error_loc = "getcwd";
658 if (ptr == parent) /* leading '/' */
663 /* even if the file is only read, WR mode is nedeed to allow
666 /* Allow migrating even without the key on encrypted files */
667 rflags = O_RDWR | O_NOATIME | O_FILE_ENC;
668 if (!(migration_flags & LLAPI_MIGRATION_NONDIRECT))
671 fd = open(name, rflags);
673 /* If encrypted file without the key,
674 * retry mirror extend in O_DIRECT.
676 if (errno == ENOKEY && !(rflags & O_DIRECT) &&
677 migration_flags & LLAPI_MIGRATION_MIRROR) {
682 error_loc = "cannot open source file";
686 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
688 error_loc = "cannot get MDT index";
693 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW |
694 /* Allow migrating without the key on encrypted files */
696 mode_t open_mode = S_IRUSR | S_IWUSR;
698 if (rflags & O_DIRECT)
699 open_flags |= O_DIRECT;
700 random_value = random();
701 rc = snprintf(volatile_file, sizeof(volatile_file),
702 "%s/%s:%.4X:%.4X:fd=%.2d", parent,
703 LUSTRE_VOLATILE_HDR, mdt_index,
705 if (rc >= sizeof(volatile_file)) {
710 /* create, open a volatile file, use caching (ie no directio) */
712 /* Returns -1 and sets errno on error: */
713 fdv = llapi_layout_file_open(volatile_file, open_flags,
718 /* Does the right thing on error: */
719 fdv = llapi_file_open_param(volatile_file, open_flags,
722 } while (fdv < 0 && (rc = fdv) == -EEXIST);
725 error_loc = "cannot create volatile file";
730 * In case the MDT does not support creation of volatile files
731 * we should try to unlink it.
733 (void)unlink(volatile_file);
736 * Not-owner (root?) special case.
737 * Need to set owner/group of volatile file like original.
738 * This will allow to pass related check during layout_swap.
743 error_loc = "cannot stat source file";
747 rc = fstat(fdv, &stv);
750 error_loc = "cannot stat volatile";
754 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
755 rc = fchown(fdv, st.st_uid, st.st_gid);
758 error_loc = "cannot change ownwership of volatile";
777 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
779 struct llapi_layout *layout;
780 size_t buf_size = 4 * 1024 * 1024;
784 size_t page_size = sysconf(_SC_PAGESIZE);
788 layout = llapi_layout_get_by_fd(fd_src, 0);
790 uint64_t stripe_size;
792 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
794 buf_size = stripe_size;
796 llapi_layout_free(layout);
799 /* Use a page-aligned buffer for direct I/O */
800 rc = posix_memalign(&buf, page_size, buf_size);
804 sparse = llapi_file_is_sparse(fd_src);
806 rc = ftruncate(fd_dst, pos);
815 size_t to_read, to_write;
818 if (sparse && pos >= data_end) {
821 data_off = llapi_data_seek(fd_src, pos, &data_size);
823 /* Non-fatal, switch to full copy */
827 /* hole at the end of file, truncate up to it */
829 rc = ftruncate(fd_dst, data_off);
833 pos = data_off & ~(page_size - 1);
834 data_end = data_off + data_size;
835 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
836 to_read = MIN(to_read, buf_size);
842 rc = check_file(fd_src);
847 rsize = pread(fd_src, buf, to_read, pos);
857 while (to_write > 0) {
860 written = pwrite(fd_dst, buf, to_write, pos);
868 if (rc || rsize < to_read)
876 /* Try to avoid page cache pollution after migration. */
877 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
878 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
884 static int migrate_set_timestamps(int fd, const struct stat *st)
886 struct timeval tv[2] = {
887 {.tv_sec = st->st_atime},
888 {.tv_sec = st->st_mtime}
891 return futimes(fd, tv);
894 static int migrate_block(int fd, int fdv)
904 error_loc = "cannot stat source file";
908 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
910 error_loc = "cannot get dataversion";
919 * The grouplock blocks all concurrent accesses to the file.
920 * It has to be taken after llapi_get_data_version as it would
923 rc = llapi_group_lock(fd, gid);
925 error_loc = "cannot get group lock";
929 rc = migrate_copy_data(fd, fdv, NULL);
931 error_loc = "data copy failed";
935 /* Make sure we keep original atime/mtime values */
936 rc = migrate_set_timestamps(fdv, &st);
938 error_loc = "set target file timestamp failed";
944 * for a migration we need to check data version on file did
947 * Pass in gid=0 since we already own grouplock.
949 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
950 SWAP_LAYOUTS_CHECK_DV1);
952 error_loc = "file changed";
955 error_loc = "cannot swap layout";
960 rc2 = llapi_group_unlock(fd, gid);
961 if (rc2 < 0 && rc == 0) {
962 error_loc = "unlock group lock";
970 * Internal helper for migrate_copy_data(). Check lease and report error if
973 * \param[in] fd File descriptor on which to check the lease.
975 * \retval 0 Migration can keep on going.
976 * \retval -errno Error occurred, abort migration.
978 static int check_lease(int fd)
982 rc = llapi_lease_check(fd);
984 return 0; /* llapi_check_lease returns > 0 on success. */
989 static int migrate_nonblock(int fd, int fdv)
998 error_loc = "cannot stat source file";
1002 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1004 error_loc = "cannot get data version";
1008 rc = migrate_copy_data(fd, fdv, check_lease);
1010 error_loc = "data copy failed";
1014 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1016 error_loc = "cannot get data version";
1022 error_loc = "source file changed";
1026 /* Make sure we keep original atime/mtime values */
1027 rc = migrate_set_timestamps(fdv, &st);
1029 error_loc = "set target file timestamp failed";
1036 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1038 struct pool_to_id_cbdata data = { .pool = pool };
1039 struct llapi_layout *layout = NULL;
1042 layout = llapi_layout_get_by_path(fname, 0);
1045 "error %s: file '%s' couldn't get layout: rc=%d\n",
1046 progname, fname, errno);
1050 rc = llapi_layout_sanity(layout, fname, false, true);
1052 llapi_layout_sanity_perror(errno);
1055 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1064 llapi_layout_free(layout);
1068 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1069 __u32 flags, __u32 neg_flags)
1072 __u32 flags_array[2];
1079 "error %s: neither component id nor pool is specified\n",
1083 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1089 ids[count] = comp_id;
1090 flags_array[count] = flags;
1095 if (neg_flags & LCME_FL_STALE) {
1097 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1102 ids[count] = comp_id;
1103 flags_array[count] = neg_flags | LCME_FL_NEG;
1107 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1109 if (errno == EUCLEAN) {
1112 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1113 progname, comp_id, fname);
1116 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1117 progname, comp_id, fname, flags, neg_flags);
1124 static int lfs_component_del(char *fname, __u32 comp_id,
1125 __u32 flags, __u32 neg_flags)
1129 if (flags && neg_flags)
1132 if (!flags && neg_flags)
1133 flags = neg_flags | LCME_FL_NEG;
1135 if ((flags && comp_id) || (!flags && !comp_id))
1139 if (flags & ~LCME_KNOWN_FLAGS) {
1141 "%s setstripe: unknown flags %#x\n",
1145 } else if (comp_id > LCME_ID_MAX) {
1146 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1151 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1154 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1155 progname, comp_id, fname, strerror(errno));
1159 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1166 rc = llapi_layout_file_comp_add(fname, layout);
1168 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1169 fname, strerror(errno));
1173 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1174 struct llapi_layout *layout)
1182 fd = lstat(fname, &st);
1183 if (fd == 0 && S_ISDIR(st.st_mode))
1184 open_flags = O_DIRECTORY | O_RDONLY;
1186 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1188 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1189 S_ISDIR(st.st_mode) ?
1190 "set default composite layout for" :
1191 "create composite file",
1192 fname, strerror(errno));
1196 static int lfs_migrate(char *name, __u64 migration_flags,
1197 struct llapi_stripe_param *param,
1198 struct llapi_layout *layout)
1200 struct llapi_layout *existing;
1201 uint64_t dom_new, dom_cur;
1206 rc = migrate_open_files(name, migration_flags, param, layout,
1211 rc = llapi_layout_dom_size(layout, &dom_new);
1213 error_loc = "cannot get new layout DoM size";
1216 /* special case for migration to DOM layout*/
1217 existing = llapi_layout_get_by_fd(fd, 0);
1219 error_loc = "cannot get existing layout";
1223 rc = llapi_layout_dom_size(existing, &dom_cur);
1225 error_loc = "cannot get current layout DoM size";
1230 * if file has DoM layout already then migration is possible to
1231 * the new layout with the same DoM component via swap layout,
1232 * if new layout used bigger DOM size, then mirroring is used
1234 if (dom_new > dom_cur) {
1235 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags);
1237 error_loc = "cannot migrate to DOM layout";
1241 if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1243 * Blocking mode (forced if servers do not support file lease).
1244 * It is also the default mode, since we cannot distinguish
1245 * between a broken lease and a server that does not support
1246 * atomic swap/close (LU-6785)
1248 rc = migrate_block(fd, fdv);
1252 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1254 error_loc = "cannot get lease";
1258 rc = migrate_nonblock(fd, fdv);
1260 llapi_lease_release(fd);
1265 * Atomically put lease, swap layouts and close.
1266 * for a migration we need to check data version on file did
1269 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1271 error_loc = "cannot swap layout";
1283 fprintf(stderr, "error: %s: %s: %s: %s\n",
1284 progname, name, error_loc, strerror(-rc));
1285 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1286 printf("%s\n", name);
1291 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1294 char *dup_string = NULL;
1300 if (!string || !string[0])
1303 dup_string = strdup(string);
1305 llapi_printf(LLAPI_MSG_ERROR,
1306 "%s: insufficient memory\n",
1311 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1315 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1316 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1317 const char *comp_name = comp_flags_table[i].cfn_name;
1319 if (strcmp(name, comp_name) == 0) {
1320 *flags |= comp_flag;
1322 } else if (strncmp(name, "^", 1) == 0 &&
1323 strcmp(name + 1, comp_name) == 0) {
1324 *neg_flags |= comp_flag;
1329 llapi_printf(LLAPI_MSG_ERROR,
1330 "%s: component flag '%s' not supported\n",
1337 if (!*flags && !*neg_flags)
1340 /* don't allow to set and exclude the same flag */
1341 if (*flags & *neg_flags)
1349 static int mdthash_input(char *string, __u32 *inflags,
1350 __u32 *exflags, __u32 *type)
1357 {"migrating", LMV_HASH_FLAG_MIGRATION},
1358 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1359 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1368 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1372 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1373 if (strcmp(name, mhflist[i].name) == 0 ||
1374 name[0] == mhflist[i].name[0]) {
1375 *inflags |= mhflist[i].flag;
1377 } else if (name[0] == '^' &&
1378 (strcmp(name + 1, mhflist[i].name) == 0 ||
1379 name[1] == mhflist[i].name[0])) {
1380 *exflags |= mhflist[i].flag;
1385 i = check_hashtype(name);
1390 llapi_printf(LLAPI_MSG_ERROR,
1391 "%s: invalid mdt_hash value '%s'\n",
1397 /* don't allow to include and exclude the same flag */
1398 if (*inflags & *exflags) {
1399 llapi_printf(LLAPI_MSG_ERROR,
1400 "%s: include and exclude same flag '%s'\n",
1408 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1416 if (strncmp(string, "^", 1) == 0) {
1417 *neg_state = llapi_layout_string_flags(string + 1);
1418 if (*neg_state != 0)
1421 *state = llapi_layout_string_flags(string);
1426 llapi_printf(LLAPI_MSG_ERROR,
1427 "%s: mirrored file state '%s' not supported\n",
1433 * struct mirror_args - Command-line arguments for mirror(s).
1434 * @m_count: Number of mirrors to be created with this layout.
1435 * @m_flags: Mirror level flags, only 'prefer' is supported.
1436 * @m_layout: Mirror layout.
1437 * @m_file: A victim file. Its layout will be split and used as a mirror.
1438 * @m_next: Point to the next node of the list.
1440 * Command-line arguments for mirror(s) will be parsed and stored in
1441 * a linked list that consists of this structure.
1443 struct mirror_args {
1446 struct llapi_layout *m_layout;
1448 struct mirror_args *m_next;
1453 * enum mirror_flags - Flags for extending a mirrored file.
1454 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1455 * in case the victim file(s) contains the same data as the
1456 * original mirrored file.
1457 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1458 * @MF_COMP_ID: specified component id instead of mirror id
1460 * Flags for extending a mirrored file.
1470 * mirror_create_sanity_check() - Check mirror list.
1471 * @list: A linked list that stores the mirror arguments.
1473 * This function does a sanity check on @list for creating
1476 * Return: 0 on success or a negative error code on failure.
1478 static int mirror_create_sanity_check(const char *fname,
1479 struct mirror_args *list,
1483 bool has_m_file = false;
1484 bool has_m_layout = false;
1489 if (fname && check_fname) {
1490 struct llapi_layout *layout;
1492 layout = llapi_layout_get_by_path(fname, 0);
1495 "error: %s: file '%s' couldn't get layout\n",
1500 rc = llapi_layout_sanity(layout, fname, false, true);
1502 llapi_layout_free(layout);
1505 llapi_layout_sanity_perror(rc);
1513 llapi_layout_free(list->m_layout);
1516 llapi_layout_get_by_path(list->m_file, 0);
1517 if (!list->m_layout) {
1519 "error: %s: file '%s' has no layout\n",
1520 progname, list->m_file);
1524 has_m_layout = true;
1525 if (!list->m_layout) {
1526 fprintf(stderr, "error: %s: no mirror layout\n",
1532 rc = llapi_layout_sanity(list->m_layout, fname, false, true);
1534 llapi_layout_sanity_perror(rc);
1538 list = list->m_next;
1541 if (has_m_file && has_m_layout) {
1543 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1551 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1553 __u32 mirror_flags = *(__u32 *)cbdata;
1557 rc = llapi_layout_comp_flags_get(layout, &flags);
1562 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1567 return LLAPI_LAYOUT_ITER_CONT;
1571 * mirror_create() - Create a mirrored file.
1572 * @fname: The file to be created.
1573 * @mirror_list: A linked list that stores the mirror arguments.
1575 * This function creates a mirrored file @fname with the mirror(s)
1576 * from @mirror_list.
1578 * Return: 0 on success or a negative error code on failure.
1580 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1582 struct llapi_layout *layout = NULL;
1583 struct mirror_args *cur_mirror = NULL;
1584 uint16_t mirror_count = 0;
1588 rc = mirror_create_sanity_check(fname, mirror_list, false);
1592 cur_mirror = mirror_list;
1593 while (cur_mirror) {
1594 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1596 &cur_mirror->m_flags);
1599 fprintf(stderr, "%s: failed to set mirror flags\n",
1604 for (i = 0; i < cur_mirror->m_count; i++) {
1605 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1609 "error: %s: merge layout failed: %s\n",
1610 progname, strerror(errno));
1614 mirror_count += cur_mirror->m_count;
1615 cur_mirror = cur_mirror->m_next;
1619 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1623 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1626 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1627 progname, strerror(errno));
1631 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1639 llapi_layout_free(layout);
1644 * Compare files and check lease on @fd.
1646 * \retval bytes number of bytes are the same
1648 static ssize_t mirror_file_compare(int fd, int fdv)
1650 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1652 ssize_t bytes_done = 0;
1653 ssize_t bytes_read = 0;
1655 buf = malloc(buflen * 2);
1660 if (!llapi_lease_check(fd)) {
1661 bytes_done = -EBUSY;
1665 bytes_read = read(fd, buf, buflen);
1666 if (bytes_read <= 0)
1669 if (bytes_read != read(fdv, buf + buflen, buflen))
1673 * XXX: should compute the checksum on each buffer and then
1674 * compare checksum to avoid cache collision
1676 if (memcmp(buf, buf + buflen, bytes_read))
1679 bytes_done += bytes_read;
1687 static int mirror_extend_file(const char *fname, const char *victim_file,
1688 enum mirror_flags mirror_flags)
1693 struct stat stbuf_v;
1694 struct ll_ioc_lease *data = NULL;
1697 fd = open(fname, O_RDWR);
1699 error_loc = "open source file";
1704 fdv = open(victim_file, O_RDWR);
1706 error_loc = "open target file";
1711 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1712 error_loc = "stat source or target file";
1717 if (stbuf.st_dev != stbuf_v.st_dev) {
1718 error_loc = "stat source and target file";
1723 /* mirrors should be of the same size */
1724 if (stbuf.st_size != stbuf_v.st_size) {
1725 error_loc = "file sizes don't match";
1730 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1732 error_loc = "cannot get lease";
1736 if (!(mirror_flags & MF_NO_VERIFY)) {
1738 /* mirrors should have the same contents */
1739 ret = mirror_file_compare(fd, fdv);
1740 if (ret != stbuf.st_size) {
1741 error_loc = "file busy or contents don't match";
1742 rc = ret < 0 ? ret : -EINVAL;
1747 /* Get rid of caching pages from clients */
1748 rc = llapi_file_flush(fd);
1750 error_loc = "cannot get data version";
1754 rc = llapi_file_flush(fdv);
1756 error_loc = "cannot get data version";
1760 rc = migrate_set_timestamps(fd, &stbuf);
1762 error_loc = "cannot set source file timestamp";
1766 /* Atomically put lease, merge layouts and close. */
1767 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1769 error_loc = "memory allocation";
1772 data->lil_mode = LL_LEASE_UNLCK;
1773 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1774 data->lil_count = 1;
1775 data->lil_ids[0] = fdv;
1776 rc = llapi_lease_set(fd, data);
1778 error_loc = "cannot merge layout";
1780 } else if (rc == 0) {
1782 error_loc = "lost lease lock";
1795 (void) unlink(victim_file);
1797 fprintf(stderr, "error: %s: %s: %s: %s\n",
1798 progname, fname, error_loc, strerror(-rc));
1802 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1803 bool inherit, uint32_t flags)
1805 struct llapi_layout *f_layout = NULL;
1806 struct ll_ioc_lease *data = NULL;
1813 f_layout = llapi_layout_get_by_path(name, 0);
1816 fprintf(stderr, "%s: cannot get layout\n", progname);
1819 rc = llapi_layout_get_last_init_comp(f_layout);
1821 fprintf(stderr, "%s: cannot get the last init comp\n",
1825 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1828 "%s: cannot inherit from the last init comp\n",
1834 llapi_layout_comp_flags_set(m_layout, flags);
1835 rc = migrate_open_files(name,
1836 LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
1837 NULL, m_layout, &fd, &fdv);
1841 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1843 error_loc = "cannot get lease";
1847 rc = fstat(fd, &st);
1849 error_loc = "cannot stat source file";
1853 rc = migrate_nonblock(fd, fdv);
1855 llapi_lease_release(fd);
1859 rc = migrate_set_timestamps(fd, &st);
1861 error_loc = "cannot set source file timestamp";
1865 /* Atomically put lease, merge layouts and close. */
1866 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1868 error_loc = "memory allocation";
1871 data->lil_mode = LL_LEASE_UNLCK;
1872 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1873 data->lil_count = 1;
1874 data->lil_ids[0] = fdv;
1875 rc = llapi_lease_set(fd, data);
1877 error_loc = "cannot merge layout";
1879 } else if (rc == 0) {
1881 error_loc = "lost lease lock";
1894 fprintf(stderr, "error: %s: %s: %s: %s\n",
1895 progname, name, error_loc, strerror(-rc));
1899 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1900 enum mirror_flags mirror_flags)
1904 while (mirror_list) {
1905 if (mirror_list->m_file) {
1906 rc = mirror_extend_file(fname, mirror_list->m_file,
1909 __u32 mirror_count = mirror_list->m_count;
1911 while (mirror_count > 0) {
1912 rc = mirror_extend_layout(fname,
1913 mirror_list->m_layout,
1914 mirror_list->m_inherit,
1915 mirror_list->m_flags);
1925 mirror_list = mirror_list->m_next;
1931 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1936 rc = llapi_layout_mirror_id_get(layout, &id);
1940 if ((__u16)id == *(__u16 *)cbdata)
1941 return LLAPI_LAYOUT_ITER_STOP;
1943 return LLAPI_LAYOUT_ITER_CONT;
1946 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1951 rc = llapi_layout_comp_id_get(layout, &id);
1955 if (id == *(__u32 *)cbdata)
1956 return LLAPI_LAYOUT_ITER_STOP;
1958 return LLAPI_LAYOUT_ITER_CONT;
1961 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
1963 char buf[LOV_MAXPOOLNAME + 1];
1964 struct pool_to_id_cbdata *d = (void *)cbdata;
1968 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1971 if (strcmp(d->pool, buf))
1972 return LLAPI_LAYOUT_ITER_CONT;
1974 rc = llapi_layout_mirror_id_get(layout, &id);
1979 return LLAPI_LAYOUT_ITER_STOP;
1982 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1984 char buf[LOV_MAXPOOLNAME + 1];
1985 struct pool_to_id_cbdata *d = (void *)cbdata;
1989 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1992 if (strcmp(d->pool, buf))
1993 return LLAPI_LAYOUT_ITER_CONT;
1995 rc = llapi_layout_comp_id_get(layout, &id);
2000 return LLAPI_LAYOUT_ITER_STOP;
2003 struct collect_ids_data {
2009 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2011 struct collect_ids_data *cid = cbdata;
2015 rc = llapi_layout_mirror_id_get(layout, &id);
2019 if ((__u16)id != cid->cid_exclude) {
2022 for (i = 0; i < cid->cid_count; i++) {
2023 /* already collected the mirror id */
2024 if (id == cid->cid_ids[i])
2025 return LLAPI_LAYOUT_ITER_CONT;
2027 cid->cid_ids[cid->cid_count] = id;
2031 return LLAPI_LAYOUT_ITER_CONT;
2035 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2036 * @mirror_id: Mirror id to be checked.
2037 * @layout: Mirror component list.
2039 * This function checks if a mirror with specified @mirror_id is the last
2040 * non-stale mirror of a layout @layout.
2042 * Return: true or false.
2045 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2047 __u16 mirror_ids[128] = { 0 };
2048 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2050 .cid_exclude = mirror_id, };
2053 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2055 for (i = 0; i < cid.cid_count; i++) {
2056 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2059 comp_size = llapi_mirror_find_stale(layout, comp_array,
2060 ARRAY_SIZE(comp_array),
2069 static int mirror_split(const char *fname, __u32 id, const char *pool,
2070 enum mirror_flags mflags, const char *victim_file)
2072 struct llapi_layout *layout;
2073 char parent[PATH_MAX];
2074 char victim[PATH_MAX];
2075 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2077 struct ll_ioc_lease *data;
2078 uint16_t mirror_count;
2082 bool purge = true; /* delete mirror by setting fdv=fd */
2086 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2088 "error %s: the source file '%s' and -f file are the same\n",
2093 /* check fname contains mirror with mirror_id/comp_id */
2094 layout = llapi_layout_get_by_path(fname, 0);
2097 "error %s: file '%s' couldn't get layout\n",
2102 rc = llapi_layout_sanity(layout, fname, false, true);
2104 llapi_layout_sanity_perror(rc);
2108 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2111 "error %s: file '%s' couldn't get mirror count\n",
2115 if (mirror_count < 2) {
2117 "error %s: file '%s' has %d component, cannot split\n",
2118 progname, fname, mirror_count);
2122 if (mflags & MF_COMP_POOL) {
2123 struct pool_to_id_cbdata data = { .pool = pool };
2125 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2127 mirror_id = data.id;
2128 } else if (mflags & MF_COMP_ID) {
2129 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2130 mirror_id = mirror_id_of(id);
2132 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2136 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2139 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2140 if (mflags & MF_COMP_POOL) {
2142 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2143 progname, fname, pool);
2145 } else if (mflags & MF_COMP_ID) {
2147 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2148 progname, fname, id);
2152 "error %s: file '%s' does not contain mirror with id %u\n",
2153 progname, fname, id);
2158 if (!victim_file && mflags & MF_DESTROY)
2159 /* Allow mirror split even without the key on encrypted files,
2160 * and in this case of a 'split -d', open file with O_DIRECT
2161 * (no IOs will be done).
2163 fd = open(fname, O_RDWR | O_DIRECT | O_FILE_ENC);
2165 fd = open(fname, O_RDWR);
2169 "error %s: open file '%s' failed: %s\n",
2170 progname, fname, strerror(errno));
2174 /* get victim file directory pathname */
2175 if (strlen(fname) > sizeof(parent) - 1) {
2176 fprintf(stderr, "error %s: file name of '%s' too long\n",
2181 strncpy(parent, fname, sizeof(parent));
2182 ptr = strrchr(parent, '/');
2184 if (!getcwd(parent, sizeof(parent))) {
2185 fprintf(stderr, "error %s: getcwd failed: %s\n",
2186 progname, strerror(errno));
2196 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2198 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2203 rc = llapi_file_is_encrypted(fd);
2205 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2206 progname, fname, rc);
2213 /* use a temp file to store the splitted layout */
2214 if (mflags & MF_DESTROY) {
2215 char file_path[PATH_MAX];
2216 unsigned int rnumber;
2219 if (last_non_stale_mirror(mirror_id, layout)) {
2222 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2228 /* don't use volatile file for mirror destroy */
2232 * try the old way to delete mirror using
2237 rc = snprintf(file_path,
2239 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2243 rc >= sizeof(file_path)) {
2244 fdv = -ENAMETOOLONG;
2248 open_flags = O_RDWR |
2249 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2250 O_CREAT | O_EXCL | O_NOFOLLOW |
2251 /* O_DIRECT for mirror split -d */
2253 /* Allow split without the key */
2255 fdv = open(file_path, open_flags,
2259 } while (fdv < 0 && rc == -EEXIST);
2265 "error %s: not permitted on encrypted file '%s': %d\n",
2266 progname, fname, rc);
2270 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2272 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2275 /* user specified victim file */
2279 "error %s: not permitted on encrypted file '%s': %d\n",
2280 progname, fname, rc);
2283 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2288 "error %s: create victim file failed: %s\n",
2289 progname, strerror(errno));
2293 /* get lease lock of fname */
2294 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2297 "error %s: cannot get lease of file '%s': %d\n",
2298 progname, fname, rc);
2302 /* Atomatically put lease, split layouts and close. */
2303 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2309 data->lil_mode = LL_LEASE_UNLCK;
2310 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2311 data->lil_count = 2;
2312 data->lil_ids[0] = fdv;
2313 data->lil_ids[1] = mirror_id;
2314 rc = llapi_lease_set(fd, data);
2316 if (rc == -EINVAL && purge) {
2317 /* could be old MDS which prohibit fd==fdv */
2322 if (rc == 0) /* lost lease lock */
2325 "error %s: cannot split '%s': %s\n",
2326 progname, fname, strerror(-rc));
2338 llapi_layout_free(layout);
2343 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2344 __u16 *mirror_ids, int ids_nr);
2346 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2347 __u64 migration_flags)
2349 struct ll_ioc_lease *data = NULL;
2352 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2354 error_loc = "cannot get lease";
2358 rc = migrate_nonblock(fd, fdv);
2362 /* Atomically put lease, merge layouts, resync and close. */
2363 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2365 error_loc = "memory allocation";
2368 data->lil_mode = LL_LEASE_UNLCK;
2369 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2370 data->lil_count = 1;
2371 data->lil_ids[0] = fdv;
2372 rc = llapi_lease_set(fd, data);
2374 error_loc = "cannot merge layout";
2376 } else if (rc == 0) {
2378 error_loc = "lost lease lock";
2384 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2386 error_loc = "cannot resync file";
2390 /* delete first mirror now */
2391 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2393 error_loc = "cannot delete old layout";
2397 llapi_lease_release(fd);
2403 fprintf(stderr, "error: %s: %s: %s: %s\n",
2404 progname, name, error_loc, strerror(-rc));
2405 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2406 printf("%s\n", name);
2413 * Parse a string containing an target index list into an array of integers.
2415 * The input string contains a comma delimited list of individual
2416 * indices and ranges, for example "1,2-4,7". Add the indices into the
2417 * \a tgts array and remove duplicates.
2419 * \param[out] tgts array to store indices in
2420 * \param[in] size size of \a tgts array
2421 * \param[in] offset starting index in \a tgts
2422 * \param[in] arg string containing OST index list
2423 * \param[in/out] overstriping index list may contain duplicates
2425 * \retval positive number of indices in \a tgts
2426 * \retval -EINVAL unable to parse \a arg
2428 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2429 unsigned long long *pattern)
2433 int slots = size - offset;
2435 bool overstriped = false;
2441 end_of_loop = false;
2442 while (!end_of_loop) {
2443 int start_index = 0;
2446 char *endptr = NULL;
2450 ptr = strchrnul(arg, ',');
2452 end_of_loop = *ptr == '\0';
2456 start_index = strtol(arg, &endptr, 0);
2457 if (endptr == arg) /* no data at all */
2459 if (errno != 0 || start_index < -1 ||
2460 (*endptr != '-' && *endptr != '\0'))
2463 end_index = start_index;
2464 if (*endptr == '-') {
2466 end_index = strtol(endptr + 1, &endptr, 0);
2467 if (errno != 0 || *endptr != '\0' || end_index < -1)
2469 if (end_index < start_index)
2473 for (i = start_index; i <= end_index && slots > 0; i++) {
2476 /* remove duplicate */
2477 for (j = 0; j < offset; j++) {
2478 if (tgts[j] == i && pattern &&
2479 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2481 else if (tgts[j] == i)
2487 if (j == offset) { /* check complete */
2493 if (slots == 0 && i < end_index)
2501 if (!end_of_loop && ptr)
2504 if (!overstriped && pattern)
2505 *pattern = LLAPI_LAYOUT_DEFAULT;
2507 return rc < 0 ? rc : nr;
2510 struct lfs_setstripe_args {
2511 unsigned long long lsa_comp_end;
2512 unsigned long long lsa_stripe_size;
2513 unsigned long long lsa_extension_size;
2514 long long lsa_stripe_count;
2515 long long lsa_stripe_off;
2516 __u32 lsa_comp_flags;
2517 __u32 lsa_comp_neg_flags;
2518 unsigned long long lsa_pattern;
2519 unsigned int lsa_mirror_count;
2521 bool lsa_first_comp;
2522 bool lsa_extension_comp;
2524 char *lsa_pool_name;
2527 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2529 unsigned int mirror_count = lsa->lsa_mirror_count;
2530 bool first_comp = lsa->lsa_first_comp;
2532 memset(lsa, 0, sizeof(*lsa));
2534 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2535 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2536 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2537 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2538 lsa->lsa_pool_name = NULL;
2540 lsa->lsa_mirror_count = mirror_count;
2541 lsa->lsa_first_comp = first_comp;
2545 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2546 * @lsa: Stripe options to be initialized and inherited.
2548 * This function initializes stripe options in @lsa and inherit
2549 * stripe_size, stripe_count and OST pool_name options.
2553 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2555 unsigned long long stripe_size;
2556 long long stripe_count;
2557 char *pool_name = NULL;
2559 stripe_size = lsa->lsa_stripe_size;
2560 stripe_count = lsa->lsa_stripe_count;
2561 pool_name = lsa->lsa_pool_name;
2563 setstripe_args_init(lsa);
2565 lsa->lsa_stripe_size = stripe_size;
2566 lsa->lsa_stripe_count = stripe_count;
2567 lsa->lsa_pool_name = pool_name;
2570 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2572 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2573 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2574 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2575 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2576 lsa->lsa_comp_end != 0);
2579 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2581 if (lsa->lsa_nr_tgts) {
2582 if (lsa->lsa_nr_tgts < 0 ||
2583 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2584 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2590 if (lsa->lsa_stripe_count > 0 &&
2591 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2592 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2593 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2594 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2595 lsa->lsa_stripe_count,
2607 * comp_args_to_layout() - Create or extend a composite layout.
2608 * @composite: Pointer to the composite layout.
2609 * @lsa: Stripe options for the new component.
2611 * This function creates or extends a composite layout by adding a new
2612 * component with stripe options from @lsa.
2614 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2616 * Return: 0 on success or an error code on failure.
2618 static int comp_args_to_layout(struct llapi_layout **composite,
2619 struct lfs_setstripe_args *lsa,
2622 struct llapi_layout *layout = *composite;
2623 uint64_t prev_end = 0;
2629 layout = llapi_layout_alloc();
2631 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2636 *composite = layout;
2637 lsa->lsa_first_comp = true;
2642 * Get current component extent, current component
2643 * must be the tail component.
2645 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2647 fprintf(stderr, "Get comp extent failed. %s\n",
2652 if (lsa->lsa_first_comp) {
2654 rc = llapi_layout_add_first_comp(layout);
2656 rc = llapi_layout_comp_add(layout);
2659 fprintf(stderr, "Add component failed. %s\n",
2665 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2667 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2668 lsa->lsa_comp_flags, strerror(errno));
2673 uint64_t comp_end = lsa->lsa_comp_end;
2676 * The extendable component is 0-length, so it can be removed
2677 * if there is insufficient space to extend it.
2679 if (lsa->lsa_extension_comp)
2680 comp_end = prev_end;
2682 rc = llapi_layout_comp_extent_set(layout, prev_end,
2685 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2686 prev_end, comp_end, strerror(errno));
2690 /* reset lsa_first_comp */
2691 lsa->lsa_first_comp = false;
2693 /* Data-on-MDT component setting */
2694 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2696 if (lsa->lsa_stripe_count == 0)
2697 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2698 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2699 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2700 if (lsa->lsa_stripe_off == -1 ||
2701 lsa->lsa_stripe_off == 0)
2702 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2704 * In case of Data-on-MDT patterns the only extra option
2705 * applicable is stripe size option.
2707 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2709 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2710 lsa->lsa_stripe_count);
2714 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2715 lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2717 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2718 lsa->lsa_stripe_size);
2722 if (lsa->lsa_nr_tgts != 0) {
2724 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2729 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2731 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2732 lsa->lsa_stripe_off);
2736 if (lsa->lsa_pool_name != 0) {
2738 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2739 lsa->lsa_pool_name);
2744 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2746 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2751 /* Data-on-MDT component has always single stripe up to end */
2752 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2753 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2754 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2756 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2763 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2764 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2766 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2767 rc = llapi_layout_extension_size_set(layout, size);
2769 rc = llapi_layout_stripe_size_set(layout, size);
2772 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2773 size, strerror(errno));
2777 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2779 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2780 lsa->lsa_stripe_count, strerror(errno));
2784 if (lsa->lsa_pool_name) {
2785 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2787 fprintf(stderr, "Set pool name: %s failed. %s\n",
2788 lsa->lsa_pool_name, strerror(errno));
2792 rc = llapi_layout_pool_name_set(layout, "");
2794 fprintf(stderr, "Clear pool name failed: %s\n",
2800 rc = lsa_args_stripe_count_check(lsa);
2804 if (lsa->lsa_nr_tgts > 0) {
2807 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2808 rc = llapi_layout_ost_index_set(layout, i,
2813 /* Make sure stripe offset is in OST list. */
2814 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2818 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2819 lsa->lsa_stripe_off);
2823 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2824 lsa->lsa_stripe_off != -1) {
2825 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2828 fprintf(stderr, "Set ost index %d failed. %s\n",
2829 i, strerror(errno));
2833 /* Create the second, virtual component of extension space */
2834 if (lsa->lsa_extension_comp) {
2835 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2836 lsa->lsa_extension_comp = false;
2843 static int build_component(struct llapi_layout **layout,
2844 struct lfs_setstripe_args *lsa, bool set_extent)
2848 rc = comp_args_to_layout(layout, lsa, set_extent);
2852 if (lsa->lsa_mirror_count > 0) {
2853 rc = llapi_layout_mirror_count_set(*layout,
2854 lsa->lsa_mirror_count);
2858 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2861 lsa->lsa_mirror_count = 0;
2867 static int build_prev_component(struct llapi_layout **layout,
2868 struct lfs_setstripe_args *prev,
2869 struct lfs_setstripe_args *lsa,
2872 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2875 if (prev->lsa_stripe_size) {
2877 prev->lsa_comp_end = lsa->lsa_comp_end;
2878 prev->lsa_extension_size = lsa->lsa_extension_size;
2879 prev->lsa_extension_comp = true;
2882 rc = build_component(layout, prev, true);
2888 * Copy lsa to previous lsa;
2889 * if this is an extension component, make the previous invalid;
2892 prev->lsa_stripe_size = 0;
2899 #ifndef LCME_TEMPLATE_FLAGS
2900 #define LCME_TEMPLATE_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
2904 static int build_layout_from_yaml_node(struct cYAML *node,
2905 struct llapi_layout **layout,
2906 struct lfs_setstripe_args *lsa,
2907 struct lfs_setstripe_args *prevp)
2909 struct lfs_setstripe_args prev = { 0 };
2910 __u32 *osts = lsa->lsa_tgts;
2918 string = node->cy_string;
2920 if (node->cy_type == CYAML_TYPE_OBJECT) {
2921 /* go deep to sub blocks */
2922 if (string && !strncmp(string, "component", 9) &&
2923 strncmp(string, "component0", 10) &&
2924 strncmp(string, "components", 10)) {
2925 rc = build_prev_component(layout, prevp, lsa,
2930 /* initialize lsa. */
2931 setstripe_args_init(lsa);
2932 lsa->lsa_first_comp = false;
2933 lsa->lsa_tgts = osts;
2936 rc = build_layout_from_yaml_node(node->cy_child, layout,
2941 if (!node->cy_string)
2944 /* skip leading lmm_ if present, to simplify parsing */
2945 if (strncmp(string, "lmm_", 4) == 0)
2948 if (node->cy_type == CYAML_TYPE_STRING) {
2949 if (!strcmp(string, "lcme_extent.e_end")) {
2950 if (!strcmp(node->cy_valuestring, "EOF") ||
2951 !strcmp(node->cy_valuestring, "eof"))
2952 lsa->lsa_comp_end = LUSTRE_EOF;
2953 } else if (!strcmp(string, "pool")) {
2954 lsa->lsa_pool_name = node->cy_valuestring;
2955 } else if (!strcmp(string, "pattern")) {
2956 if (!strcmp(node->cy_valuestring, "mdt"))
2957 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2958 if (!strcmp(node->cy_valuestring,
2959 "raid0,overstriped"))
2961 LLAPI_LAYOUT_OVERSTRIPING;
2962 } else if (!strcmp(string, "lcme_flags")) {
2963 rc = comp_str2flags(node->cy_valuestring,
2964 &lsa->lsa_comp_flags,
2965 &lsa->lsa_comp_neg_flags);
2969 * Only template flags have meaning in
2970 * the layout for a new file
2972 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2974 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2975 if (!strcmp(string, "lcm_mirror_count")) {
2976 lsa->lsa_mirror_count = node->cy_valueint;
2977 } else if (!strcmp(string, "lcme_extent.e_start")) {
2978 if (node->cy_valueint == 0)
2979 lsa->lsa_first_comp = true;
2980 } else if (!strcmp(string, "lcme_extent.e_end")) {
2981 if (node->cy_valueint == -1)
2982 lsa->lsa_comp_end = LUSTRE_EOF;
2984 lsa->lsa_comp_end = node->cy_valueint;
2985 } else if (!strcmp(string, "stripe_count")) {
2986 lsa->lsa_stripe_count = node->cy_valueint;
2987 } else if (!strcmp(string, "stripe_size")) {
2988 lsa->lsa_stripe_size = node->cy_valueint;
2989 } else if (!strcmp(string, "extension_size")) {
2990 lsa->lsa_extension_size = node->cy_valueint;
2991 lsa->lsa_extension_comp = true;
2992 } else if (!strcmp(string, "stripe_offset")) {
2993 lsa->lsa_stripe_off = node->cy_valueint;
2994 } else if (!strcmp(string, "l_ost_idx")) {
2995 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3000 node = node->cy_next;
3003 if (prevp == &prev) {
3004 rc = build_prev_component(layout, prevp, lsa, true);
3008 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3009 rc = build_component(layout, lsa, *layout != NULL);
3015 static int lfs_comp_create_from_yaml(char *template,
3016 struct llapi_layout **layout,
3017 struct lfs_setstripe_args *lsa,
3020 struct cYAML *tree = NULL, *err_rc = NULL;
3023 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3025 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3026 progname, template);
3027 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3028 "can't parse", &err_rc);
3029 cYAML_print_tree2file(stderr, err_rc);
3030 cYAML_free_tree(err_rc);
3035 /* initialize lsa for plain file */
3036 setstripe_args_init(lsa);
3037 lsa->lsa_tgts = osts;
3039 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3041 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3042 progname, template);
3045 /* clean clean lsa */
3046 setstripe_args_init(lsa);
3050 cYAML_free_tree(tree);
3055 * Get the extension size from the next (SEL) component and extend the
3056 * current component on it. The start of the next component is to be
3059 * \param[in] layout the current layout
3060 * \param[in] start the start of the current component
3061 * \param[in,out] end the end of the current component
3062 * \param[in] offset the offset to adjust the end position to instead of
3065 * \retval 0 - extended successfully
3066 * \retval < 0 - error
3068 static int layout_extend_comp(struct llapi_layout *layout,
3069 uint64_t start, uint64_t *end,
3072 uint64_t size, next_start, next_end;
3075 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3078 "%s setstripe: cannot move component cursor: %s\n",
3079 progname, strerror(errno));
3084 * Even if the @size will not be used below, this will fail if
3085 * this is not a SEL component - a good confirmation we are
3086 * working on right components.
3088 rc = llapi_layout_extension_size_get(layout, &size);
3091 "%s setstripe: cannot get component ext size: %s\n",
3092 progname, strerror(errno));
3096 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3098 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3099 progname, strerror(errno));
3103 next_start += offset ?: size;
3104 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3106 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3107 progname, strerror(errno));
3111 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3114 "%s setstripe: cannot move component cursor: %s\n",
3115 progname, strerror(errno));
3119 *end += offset ?: size;
3120 rc = llapi_layout_comp_extent_set(layout, start, *end);
3122 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3123 progname, strerror(errno));
3131 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3132 * end of the last component in the existing file, and adjust the
3133 * first extent start of the components to be added accordingly.
3135 * In the create mode, we need to check if the first component is an extendable
3136 * SEL component and extend its length to the extension size (first component
3137 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3139 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3142 struct llapi_layout *head;
3143 uint64_t start = 0, prev_end = 0;
3147 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3152 head = llapi_layout_get_by_path(fname, 0);
3155 "%s setstripe: cannot read layout from '%s': %s\n",
3156 progname, fname, strerror(errno));
3158 } else if (errno == ENODATA) {
3160 * file without LOVEA, this component-add will be turned
3161 * into a component-create.
3163 llapi_layout_free(head);
3167 * the new layout will be added to an empty one, it
3168 * still needs to be adjusted below
3172 } else if (!llapi_layout_is_composite(head)) {
3174 "%s setstripe: '%s' not a composite file\n",
3176 llapi_layout_free(head);
3180 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3183 "%s setstripe: cannot get prev extent: %s\n",
3184 progname, strerror(errno));
3185 llapi_layout_free(head);
3189 llapi_layout_free(head);
3193 /* Make sure we use the first component of the layout to be added. */
3194 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3197 "%s setstripe: cannot move component cursor: %s\n",
3198 progname, strerror(errno));
3202 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3204 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3205 progname, strerror(errno));
3209 if (start == 0 && end == 0) {
3210 rc = layout_extend_comp(layout, start, &end,
3211 comp_add ? prev_end : 0);
3216 if (start > prev_end || end < prev_end) {
3218 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3219 progname, start, end, prev_end);
3223 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3226 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3227 progname, prev_end, end, strerror(errno));
3234 static int mirror_adjust_first_extents(struct mirror_args *list)
3242 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3245 list = list->m_next;
3251 static inline bool arg_is_eof(char *arg)
3253 return !strncmp(arg, "-1", strlen("-1")) ||
3254 !strncmp(arg, "EOF", strlen("EOF")) ||
3255 !strncmp(arg, "eof", strlen("eof"));
3259 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3261 * Return: Valid mirror_args pointer on success and
3262 * NULL if memory allocation fails.
3264 static struct mirror_args *lfs_mirror_alloc(void)
3266 struct mirror_args *mirror = NULL;
3269 mirror = calloc(1, sizeof(*mirror));
3271 mirror->m_inherit = false;
3282 * lfs_mirror_free() - Free memory allocated for a mirror argument
3284 * @mirror: Previously allocated mirror argument structure by
3285 * lfs_mirror_alloc().
3287 * Free memory allocated for @mirror.
3291 static void lfs_mirror_free(struct mirror_args *mirror)
3293 if (mirror->m_layout)
3294 llapi_layout_free(mirror->m_layout);
3299 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3300 * @mirror_list: Previously allocated mirror list.
3302 * Free memory allocated for @mirror_list.
3306 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3308 struct mirror_args *next_mirror = NULL;
3310 while (mirror_list) {
3311 next_mirror = mirror_list->m_next;
3312 lfs_mirror_free(mirror_list);
3313 mirror_list = next_mirror;
3325 LFS_COMP_NO_VERIFY_OPT,
3327 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3329 LFS_MIRROR_STATE_OPT,
3331 LFS_MIRROR_INDEX_OPT,
3332 LFS_LAYOUT_FOREIGN_OPT,
3339 #ifndef LCME_USER_MIRROR_FLAGS
3340 /* The mirror flags can be set by users at creation time. */
3341 #define LCME_USER_MIRROR_FLAGS (LCME_FL_PREF_RW)
3345 static int lfs_setstripe_internal(int argc, char **argv,
3346 enum setstripe_origin opc)
3348 struct lfs_setstripe_args lsa = { 0 };
3349 struct llapi_stripe_param *param = NULL;
3350 struct find_param migrate_mdt_param = {
3360 unsigned long long size_units = 1;
3361 bool migrate_mode = false;
3362 bool migrate_mdt_mode = false;
3363 bool setstripe_mode = false;
3364 bool migration_block = false;
3365 __u64 migration_flags = 0;
3366 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3367 int comp_del = 0, comp_set = 0;
3370 struct llapi_layout *layout = NULL;
3371 struct llapi_layout **lpp = &layout;
3372 bool mirror_mode = false;
3373 bool has_m_file = false;
3374 __u32 mirror_count = 0;
3375 enum mirror_flags mirror_flags = 0;
3376 struct mirror_args *mirror_list = NULL;
3377 struct mirror_args *new_mirror = NULL;
3378 struct mirror_args *last_mirror = NULL;
3379 __u16 mirror_id = 0;
3381 bool from_yaml = false;
3382 bool from_copy = false;
3383 char *template = NULL;
3384 bool foreign_mode = false;
3386 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3387 char *mode_opt = NULL;
3388 mode_t previous_umask = 0;
3389 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3391 struct option long_opts[] = {
3392 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3393 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3394 /* --block is only valid in migrate mode */
3395 { .val = 'b', .name = "block", .has_arg = no_argument },
3396 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3397 { .val = LFS_COMP_ADD_OPT,
3398 .name = "comp-add", .has_arg = no_argument },
3399 { .val = LFS_COMP_ADD_OPT,
3400 .name = "component-add", .has_arg = no_argument },
3401 { .val = LFS_COMP_DEL_OPT,
3402 .name = "comp-del", .has_arg = no_argument },
3403 { .val = LFS_COMP_DEL_OPT,
3404 .name = "component-del", .has_arg = no_argument },
3405 { .val = LFS_COMP_FLAGS_OPT,
3406 .name = "comp-flags", .has_arg = required_argument },
3407 { .val = LFS_COMP_FLAGS_OPT,
3408 .name = "component-flags",
3409 .has_arg = required_argument },
3410 { .val = LFS_COMP_SET_OPT,
3411 .name = "comp-set", .has_arg = no_argument },
3412 { .val = LFS_COMP_SET_OPT,
3413 .name = "component-set",
3414 .has_arg = no_argument},
3415 { .val = LFS_COMP_NO_VERIFY_OPT,
3416 .name = "no-verify", .has_arg = no_argument},
3417 { .val = LFS_LAYOUT_FLAGS_OPT,
3418 .name = "flags", .has_arg = required_argument},
3419 { .val = LFS_LAYOUT_FOREIGN_OPT,
3420 .name = "foreign", .has_arg = optional_argument},
3421 { .val = LFS_MIRROR_ID_OPT,
3422 .name = "mirror-id", .has_arg = required_argument},
3423 { .val = LFS_MODE_OPT,
3424 .name = "mode", .has_arg = required_argument},
3425 { .val = LFS_LAYOUT_COPY,
3426 .name = "copy", .has_arg = required_argument},
3427 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3428 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3429 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3430 { .val = 'C', .name = "overstripe-count",
3431 .has_arg = required_argument},
3432 { .val = 'd', .name = "delete", .has_arg = no_argument},
3433 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3434 /* used with "lfs migrate -m" */
3435 { .val = 'd', .name = "directory", .has_arg = no_argument},
3436 /* --non-direct is only valid in migrate mode */
3437 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3438 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3439 { .val = 'E', .name = "component-end",
3440 .has_arg = required_argument},
3441 { .val = 'f', .name = "file", .has_arg = required_argument },
3442 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3443 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3444 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3445 { .val = 'h', .name = "help", .has_arg = no_argument },
3446 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3447 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3448 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3449 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3450 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3451 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3452 { .val = 'L', .name = "layout", .has_arg = required_argument },
3453 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3454 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3455 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3456 /* --non-block is only valid in migrate mode */
3457 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3458 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3459 { .val = 'o', .name = "ost", .has_arg = required_argument },
3460 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3461 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3462 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3464 { .val = 'p', .name = "pool", .has_arg = required_argument },
3465 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3466 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3467 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3468 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3469 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3470 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3471 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3472 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3473 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3474 /* --verbose is only valid in migrate mode */
3475 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3476 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3477 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3478 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3479 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3480 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3483 setstripe_args_init(&lsa);
3485 migrate_mode = (opc == SO_MIGRATE);
3486 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3487 setstripe_mode = (opc == SO_SETSTRIPE);
3488 if (opc == SO_MIRROR_DELETE) {
3490 mirror_flags = MF_DESTROY;
3493 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3495 while ((c = getopt_long(argc, argv,
3496 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:y:z:",
3497 long_opts, NULL)) >= 0) {
3503 case LFS_COMP_ADD_OPT:
3506 case LFS_COMP_DEL_OPT:
3509 case LFS_COMP_FLAGS_OPT:
3510 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3511 &lsa.lsa_comp_neg_flags);
3514 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3516 "%s: inverted flags are not supported\n",
3521 case LFS_COMP_SET_OPT:
3524 case LFS_COMP_NO_VERIFY_OPT:
3525 mirror_flags |= MF_NO_VERIFY;
3527 case LFS_MIRROR_ID_OPT: {
3528 unsigned long int id;
3531 id = strtoul(optarg, &end, 0);
3532 if (errno != 0 || *end != '\0' || id == 0 ||
3535 "%s %s: invalid mirror ID '%s'\n",
3536 progname, argv[0], optarg);
3540 mirror_id = (__u16)id;
3543 case LFS_LAYOUT_FLAGS_OPT: {
3546 /* check for numeric flags (foreign and mirror cases) */
3547 if (setstripe_mode && !mirror_mode && !last_mirror) {
3549 flags = strtoul(optarg, &end, 16);
3550 if (errno != 0 || *end != '\0' ||
3551 flags >= UINT32_MAX) {
3553 "%s %s: invalid hex flags '%s'\n",
3554 progname, argv[0], optarg);
3557 if (!foreign_mode) {
3559 "%s %s: hex flags must be specified with --foreign option\n",
3566 if (!mirror_mode || !last_mirror) {
3568 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3573 result = comp_str2flags(optarg, &last_mirror->m_flags,
3580 "%s: inverted flags are not supported\n",
3585 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3587 "%s: unsupported mirror flags: %s\n",
3594 case LFS_LAYOUT_FOREIGN_OPT:
3596 /* check pure numeric */
3597 type = strtoul(optarg, &end, 0);
3600 type = check_foreign_type_name(optarg);
3601 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3603 "%s %s: unrecognized foreign type '%s'\n",
3608 } else if (type >= UINT32_MAX) {
3610 "%s %s: invalid foreign type '%s'\n",
3611 progname, argv[0], optarg);
3615 foreign_mode = true;
3620 mode = strtoul(mode_opt, &end, 8);
3623 "%s %s: bad mode '%s'\n",
3624 progname, argv[0], mode_opt);
3627 previous_umask = umask(0);
3630 case LFS_LAYOUT_COPY:
3635 if (!migrate_mode) {
3637 "%s %s: -b|--block valid only for migrate command\n",
3641 migration_block = true;
3644 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3646 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3650 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3654 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3655 if (errno != 0 || *end != '\0'||
3656 lsa.lsa_stripe_count < -1 ||
3657 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3659 "%s %s: invalid stripe count '%s'\n",
3660 progname, argv[0], optarg);
3664 if (lsa.lsa_stripe_count == -1)
3665 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3669 migrate_mdt_param.fp_max_depth = 1;
3671 /* delete the default striping pattern */
3673 if (opc == SO_MIRROR_SPLIT) {
3676 "%s %s: -d cannot used with -f\n",
3680 mirror_flags |= MF_DESTROY;
3685 if (!migrate_mode) {
3687 "%s %s: -D|--non-direct is valid only for migrate command\n",
3691 migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3694 if (lsa.lsa_comp_end != 0) {
3695 result = comp_args_to_layout(lpp, &lsa, true);
3697 fprintf(stderr, "%s: invalid layout\n",
3702 setstripe_args_init_inherit(&lsa);
3705 if (arg_is_eof(optarg)) {
3706 lsa.lsa_comp_end = LUSTRE_EOF;
3708 result = llapi_parse_size(optarg,
3711 /* assume units of KB if too small */
3712 if (lsa.lsa_comp_end < 4096)
3713 lsa.lsa_comp_end *= 1024;
3715 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3717 "%s %s: invalid component end '%s'\n",
3718 progname, argv[0], optarg);
3724 if (!migrate_mode) {
3726 "--mdt-hash is valid only for migrate command\n");
3730 lsa.lsa_pattern = check_hashtype(optarg);
3731 if (lsa.lsa_pattern == 0) {
3733 "%s %s: bad stripe hash type '%s'\n",
3734 progname, argv[0], optarg);
3740 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3741 if (errno != 0 || *end != '\0' ||
3742 lsa.lsa_stripe_off < -1 ||
3743 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3745 "%s %s: invalid stripe offset '%s'\n",
3746 progname, argv[0], optarg);
3749 if (lsa.lsa_stripe_off == -1)
3750 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3753 comp_id = strtoul(optarg, &end, 0);
3754 if (*end != '\0' || comp_id == 0 ||
3755 comp_id > LCME_ID_MAX) {
3757 "%s %s: invalid component ID '%s'\n",
3758 progname, argv[0], optarg);
3763 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3765 "error: %s: invalid option: %s\n",
3766 progname, argv[optopt + 1]);
3769 if (opc == SO_MIRROR_EXTEND) {
3772 "error: %s: '-N' must exist in front of '%s'\n",
3773 progname, argv[optopt + 1]);
3776 last_mirror->m_file = optarg;
3777 last_mirror->m_count = 1;
3781 mirror_list = lfs_mirror_alloc();
3782 mirror_list->m_file = optarg;
3787 if (strcmp(argv[optind - 1], "mdt") == 0) {
3788 /* Can be only the first component */
3792 "error: 'mdt' layout can be only the first one\n");
3795 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3798 "error: 'mdt' layout size is too big\n");
3801 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3802 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
3803 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3806 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3812 if (!migrate_mode) {
3814 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3818 migrate_mdt_mode = true;
3819 lsa.lsa_nr_tgts = parse_targets(tgts,
3820 sizeof(tgts) / sizeof(__u32),
3821 lsa.lsa_nr_tgts, optarg, NULL);
3822 if (lsa.lsa_nr_tgts < 0) {
3824 "%s: invalid MDT target(s) '%s'\n",
3829 lsa.lsa_tgts = tgts;
3830 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3831 lsa.lsa_stripe_off = tgts[0];
3834 if (!migrate_mode) {
3836 "%s %s: -n|--non-block valid only for migrate command\n",
3840 migration_flags |= LLAPI_MIGRATION_NONBLOCK;
3843 if (opc == SO_SETSTRIPE) {
3844 opc = SO_MIRROR_CREATE;
3850 mirror_count = strtoul(optarg, &end, 0);
3851 if (errno != 0 || *end != '\0' ||
3852 mirror_count == 0 ||
3853 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3855 "error: %s: bad mirror count: %s\n",
3862 new_mirror = lfs_mirror_alloc();
3863 new_mirror->m_count = mirror_count;
3866 mirror_list = new_mirror;
3869 /* wrap up last mirror */
3870 if (!setstripe_args_specified(&lsa))
3871 last_mirror->m_inherit = true;
3872 if (lsa.lsa_comp_end == 0)
3873 lsa.lsa_comp_end = LUSTRE_EOF;
3875 result = comp_args_to_layout(lpp, &lsa, true);
3877 lfs_mirror_free(new_mirror);
3881 setstripe_args_init_inherit(&lsa);
3883 last_mirror->m_next = new_mirror;
3886 last_mirror = new_mirror;
3887 lpp = &last_mirror->m_layout;
3890 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3891 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3893 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3895 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3897 "%s %s: -o|--ost incompatible with DoM layout\n",
3902 * -o allows overstriping, and must note it because
3903 * parse_targets is shared with MDT striping, which
3904 * does not allow duplicates
3906 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3907 lsa.lsa_nr_tgts = parse_targets(tgts,
3908 sizeof(tgts) / sizeof(__u32),
3909 lsa.lsa_nr_tgts, optarg,
3911 if (lsa.lsa_nr_tgts < 0) {
3913 "%s %s: invalid OST target(s) '%s'\n",
3914 progname, argv[0], optarg);
3918 lsa.lsa_tgts = tgts;
3919 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3920 lsa.lsa_stripe_off = tgts[0];
3925 lsa.lsa_pool_name = optarg;
3927 if (strlen(lsa.lsa_pool_name) == 0 ||
3928 strncmp(lsa.lsa_pool_name, "none",
3929 LOV_MAXPOOLNAME) == 0)
3930 lsa.lsa_pool_name = NULL;
3933 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3935 /* assume units of KB if too small to be valid */
3936 if (lsa.lsa_stripe_size < 4096)
3937 lsa.lsa_stripe_size *= 1024;
3939 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3941 "%s %s: invalid stripe size '%s'\n",
3942 progname, argv[0], optarg);
3947 if (!migrate_mode) {
3949 "%s %s: -v|--verbose valid only for migrate command\n",
3953 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3954 migration_flags = LLAPI_MIGRATION_VERBOSE;
3964 result = llapi_parse_size(optarg,
3965 &lsa.lsa_extension_size,
3969 "%s %s: invalid extension size '%s'\n",
3970 progname, argv[0], optarg);
3974 lsa.lsa_extension_comp = true;
3977 fprintf(stderr, "%s: unrecognized option '%s'\n",
3978 progname, argv[optind - 1]);
3984 fname = argv[optind];
3986 if (optind == argc) {
3987 fprintf(stderr, "%s %s: FILE must be specified\n",
3992 /* lfs migrate $filename should keep the file's layout by default */
3993 if (migrate_mode && !layout && !from_yaml &&
3994 !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
3997 if (xattr && !foreign_mode) {
3999 * only print a warning as this is harmless and will be ignored
4002 "%s %s: xattr has been specified for non-foreign layout\n",
4004 } else if (foreign_mode && !xattr) {
4006 "%s %s: xattr must be provided in foreign mode\n",
4011 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4012 comp_set || comp_id || delete || from_copy ||
4013 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4016 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4021 if (mirror_mode && mirror_count == 0) {
4023 "error: %s: --mirror-count|-N option is required\n",
4030 if (!setstripe_args_specified(&lsa))
4031 last_mirror->m_inherit = true;
4032 if (lsa.lsa_comp_end == 0)
4033 lsa.lsa_comp_end = LUSTRE_EOF;
4036 if (lsa.lsa_comp_end != 0) {
4037 result = comp_args_to_layout(lpp, &lsa, true);
4039 fprintf(stderr, "error: %s: invalid layout\n",
4046 if (mirror_flags & MF_NO_VERIFY) {
4047 if (opc != SO_MIRROR_EXTEND) {
4049 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4053 } else if (!has_m_file) {
4055 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4062 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4064 "%s %s: --component-set doesn't have component-id set\n",
4069 if ((delete + comp_set + comp_del + comp_add) > 1) {
4071 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4076 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4077 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4079 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4084 if ((comp_set || comp_del) &&
4085 (setstripe_args_specified(&lsa) || layout != NULL)) {
4087 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4092 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4094 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4099 if (comp_add || comp_del) {
4102 result = lstat(fname, &st);
4103 if (result == 0 && S_ISDIR(st.st_mode)) {
4105 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4112 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4121 "%s %s: option -E must be specified with --component-add\n",
4127 if (from_yaml && from_copy) {
4129 "%s: can't specify --yaml and --copy together\n",
4134 if ((from_yaml || from_copy) &&
4135 (setstripe_args_specified(&lsa) || layout != NULL)) {
4137 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4142 if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4144 "%s %s: options --non-block and --block are mutually exclusive\n",
4149 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4150 opc != SO_MIRROR_DELETE && comp_id != 0) {
4152 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4157 if (migrate_mdt_mode) {
4158 struct lmv_user_md *lmu;
4160 /* initialize migrate mdt parameters */
4161 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4162 LMV_USER_MAGIC_SPECIFIC));
4165 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4166 progname, argv[0], strerror(ENOMEM));
4170 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4171 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4172 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4174 "%s %s: migrate should specify MDT index\n",
4179 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4180 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4181 lmu->lum_hash_type = lsa.lsa_pattern;
4183 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4184 if (lsa.lsa_pool_name) {
4185 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4186 sizeof(lmu->lum_pool_name) - 1);
4187 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4189 if (lsa.lsa_nr_tgts > 1) {
4192 if (lsa.lsa_stripe_count > 0 &&
4193 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4194 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4196 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4197 progname, lsa.lsa_stripe_count,
4203 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4204 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4205 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4206 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4208 lmu->lum_magic = LMV_USER_MAGIC;
4211 migrate_mdt_param.fp_lmv_md = lmu;
4212 migrate_mdt_param.fp_migrate = 1;
4213 } else if (!layout) {
4214 if (lsa_args_stripe_count_check(&lsa))
4217 /* initialize stripe parameters */
4218 param = calloc(1, offsetof(typeof(*param),
4219 lsp_osts[lsa.lsa_nr_tgts]));
4222 "%s %s: cannot allocate memory for parameters: %s\n",
4223 progname, argv[0], strerror(ENOMEM));
4228 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4229 param->lsp_stripe_size = lsa.lsa_stripe_size;
4230 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4231 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4232 param->lsp_stripe_count = -1;
4234 param->lsp_stripe_count = lsa.lsa_stripe_count;
4236 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4237 param->lsp_stripe_offset = -1;
4239 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4240 param->lsp_stripe_pattern =
4241 llapi_pattern_to_lov(lsa.lsa_pattern);
4242 if (param->lsp_stripe_pattern == EINVAL) {
4243 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4248 param->lsp_pool = lsa.lsa_pool_name;
4249 param->lsp_is_specific = false;
4251 if (lsa.lsa_nr_tgts > 0) {
4252 param->lsp_is_specific = true;
4253 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4254 memcpy(param->lsp_osts, tgts,
4255 sizeof(*tgts) * lsa.lsa_nr_tgts);
4260 /* generate a layout from a YAML template */
4261 result = lfs_comp_create_from_yaml(template, &layout,
4265 "error: %s: can't create composite layout from template file %s\n",
4271 if (layout != NULL || mirror_list != NULL) {
4273 result = mirror_adjust_first_extents(mirror_list);
4275 result = layout_adjust_first_extent(fname, layout,
4277 if (result == -ENODATA)
4279 else if (result != 0) {
4280 fprintf(stderr, "error: %s: invalid layout\n",
4286 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4288 layout = llapi_layout_get_by_path(template ?: fname, 0);
4291 "%s: can't create composite layout from file %s: %s\n",
4292 progname, template ?: fname,
4299 if (migrate_mdt_mode) {
4300 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4301 } else if (migrate_mode) {
4302 result = lfs_migrate(fname, migration_flags, param,
4304 } else if (comp_set != 0) {
4305 result = lfs_component_set(fname, comp_id,
4308 lsa.lsa_comp_neg_flags);
4309 } else if (comp_del != 0) {
4310 result = lfs_component_del(fname, comp_id,
4312 lsa.lsa_comp_neg_flags);
4313 } else if (comp_add != 0) {
4314 result = lfs_component_add(fname, layout);
4315 } else if (opc == SO_MIRROR_CREATE) {
4316 result = mirror_create(fname, mirror_list);
4317 } else if (opc == SO_MIRROR_EXTEND) {
4318 result = mirror_extend(fname, mirror_list,
4320 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4321 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4323 "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4327 if (lsa.lsa_pool_name)
4328 mirror_flags |= MF_COMP_POOL;
4329 else if (mirror_id != 0)
4330 comp_id = mirror_id;
4332 mirror_flags |= MF_COMP_ID;
4333 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4335 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4339 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4341 has_m_file ? mirror_list->m_file :
4343 } else if (layout) {
4344 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4350 } else if (foreign_mode) {
4351 result = llapi_file_create_foreign(fname, mode, type,
4358 result = llapi_file_open_param(fname,
4367 /* Save the first error encountered. */
4375 umask(previous_umask);
4378 free(migrate_mdt_param.fp_lmv_md);
4379 llapi_layout_free(layout);
4380 lfs_mirror_list_free(mirror_list);
4385 llapi_layout_free(layout);
4386 lfs_mirror_list_free(mirror_list);
4390 static int lfs_poollist(int argc, char **argv)
4395 return llapi_poollist(argv[1]);
4398 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4399 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4404 char *endptr = "AD";
4409 else if (str[0] == '-')
4415 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4416 long long val = strtoll(timebuf, &endptr, 0);
4421 unit *= 52; /* 52 weeks + 1 day below */
4422 case 'w': /* fallthrough */
4424 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4425 param->fp_time_margin *= (1 + unit / 52);
4426 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4427 case '\0': /* days are default unit if none used */
4428 case 'd': /* fallthrough */
4430 case 'h': /* fallthrough */
4432 case 'm': /* fallthrough */
4434 case 's': /* fallthrough */
4436 /* don't need to multiply by 1 for seconds */
4439 "%s find: bad time string '%s': %s\n",
4440 progname, timebuf, strerror(EINVAL));
4444 if (param->fp_time_margin == 0 ||
4445 (*endptr && unit < param->fp_time_margin))
4446 param->fp_time_margin = unit;
4453 fprintf(stderr, "%s find: bad time '%s': too large\n",
4463 static int str2quotaid(__u32 *id, const char *arg)
4465 unsigned long int projid_tmp = 0;
4466 char *endptr = NULL;
4468 projid_tmp = strtoul(arg, &endptr, 10);
4469 if (*endptr != '\0')
4471 /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4472 if (projid_tmp >= UINT32_MAX)
4479 static int name2uid(unsigned int *id, const char *name)
4481 struct passwd *passwd;
4483 passwd = getpwnam(name);
4486 *id = passwd->pw_uid;
4491 static int name2gid(unsigned int *id, const char *name)
4493 struct group *group;
4495 group = getgrnam(name);
4498 *id = group->gr_gid;
4503 static inline int name2projid(unsigned int *id, const char *name)
4508 static int uid2name(char **name, unsigned int id)
4510 struct passwd *passwd;
4512 passwd = getpwuid(id);
4515 *name = passwd->pw_name;
4520 static inline int gid2name(char **name, unsigned int id)
4522 struct group *group;
4524 group = getgrgid(id);
4527 *name = group->gr_name;
4532 static int name2layout(__u32 *layout, char *name)
4534 char *ptr, *layout_name;
4537 for (ptr = name; ; ptr = NULL) {
4538 layout_name = strtok(ptr, ",");
4541 if (strcmp(layout_name, "released") == 0)
4542 *layout |= LOV_PATTERN_F_RELEASED;
4543 else if (strcmp(layout_name, "raid0") == 0)
4544 *layout |= LOV_PATTERN_RAID0;
4545 else if (strcmp(layout_name, "mdt") == 0)
4546 *layout |= LOV_PATTERN_MDT;
4547 else if (strcmp(layout_name, "overstriping") == 0)
4548 *layout |= LOV_PATTERN_OVERSTRIPING;
4555 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4558 int user, group, other;
4563 mode_t previous_flags;
4565 user = group = other = 0;
4585 user = group = other = 1;
4596 who = user || group || other;
4599 usermask = umask(0022);
4604 if (*input == '-' || *input == '+' || *input == '=')
4607 /* operation is required */
4610 /* get the flags in *outmode */
4613 previous_flags = (*outmode & 0700);
4614 perm |= user ? previous_flags : 0;
4615 perm |= group ? (previous_flags >> 3) : 0;
4616 perm |= other ? (previous_flags >> 6) : 0;
4620 previous_flags = (*outmode & 0070);
4621 perm |= user ? (previous_flags << 3) : 0;
4622 perm |= group ? previous_flags : 0;
4623 perm |= other ? (previous_flags >> 3) : 0;
4627 previous_flags = (*outmode & 0007);
4628 perm |= user ? (previous_flags << 6) : 0;
4629 perm |= group ? (previous_flags << 3) : 0;
4630 perm |= other ? previous_flags : 0;
4637 /* this part is optional,
4638 * if empty perm = 0 and *outmode is not modified
4645 perm |= user ? 0400 : 0;
4646 perm |= group ? 0040 : 0;
4647 perm |= other ? 0004 : 0;
4648 /* set read permission for uog except for umask's
4651 perm |= who ? 0 : (0444 & ~usermask);
4654 perm |= user ? 0200 : 0;
4655 perm |= group ? 0020 : 0;
4656 perm |= other ? 0002 : 0;
4657 /* set write permission for uog except for umask'
4660 perm |= who ? 0 : (0222 & ~usermask);
4663 perm |= user ? 0100 : 0;
4664 perm |= group ? 0010 : 0;
4665 perm |= other ? 0001 : 0;
4666 /* set execute permission for uog except for umask'
4669 perm |= who ? 0 : (0111 & ~usermask);
4673 * Adds execute permission to 'u', 'g' and/or 'g' if
4674 * specified and either 'u', 'g' or 'o' already has
4675 * execute permissions.
4677 if ((*outmode & 0111) != 0) {
4678 perm |= user ? 0100 : 0;
4679 perm |= group ? 0010 : 0;
4680 perm |= other ? 0001 : 0;
4681 perm |= !who ? 0111 : 0;
4685 /* s is ignored if o is given, but it's not an error */
4686 if (other && !group && !user)
4688 perm |= user ? S_ISUID : 0;
4689 perm |= group ? S_ISGID : 0;
4692 /* 't' should be used when 'a' is given
4695 perm |= (!who || all) ? S_ISVTX : 0;
4696 /* using ugo with t is not an error */
4707 /* uog flags should be only one character long */
4708 if (previous_flags && (*input != '\0' && *input != ','))
4713 /* remove the flags from outmode */
4717 /* add the flags to outmode */
4721 /* set the flags of outmode to perm */
4731 static int str2mode_t(const char *input, mode_t *outmode)
4738 if (*input >= '0' && *input <= '7') {
4739 /* parse octal representation */
4744 /* look for invalid digits in octal representation */
4745 while (isdigit(*iter))
4750 *outmode = strtoul(input, &end, 8);
4752 if (errno != 0 || *outmode > 07777) {
4757 } else if (*input == '8' || *input == '9') {
4758 /* error: invalid octal number */
4761 /* parse coma seperated list of symbolic representation */
4770 rc = parse_symbolic(input, outmode, &end);
4775 } while (*end == ',');
4783 static int lfs_find(int argc, char **argv)
4788 struct find_param param = {
4791 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4793 struct option long_opts[] = {
4794 { .val = 'A', .name = "atime", .has_arg = required_argument },
4795 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4796 { .val = 'B', .name = "btime", .has_arg = required_argument },
4797 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4798 { .val = LFS_COMP_COUNT_OPT,
4799 .name = "comp-count", .has_arg = required_argument },
4800 { .val = LFS_COMP_COUNT_OPT,
4801 .name = "component-count",
4802 .has_arg = required_argument },
4803 { .val = LFS_COMP_FLAGS_OPT,
4804 .name = "comp-flags", .has_arg = required_argument },
4805 { .val = LFS_COMP_FLAGS_OPT,
4806 .name = "component-flags",
4807 .has_arg = required_argument },
4808 { .val = LFS_COMP_START_OPT,
4809 .name = "comp-start", .has_arg = required_argument },
4810 { .val = LFS_COMP_START_OPT,
4811 .name = "component-start",
4812 .has_arg = required_argument },
4813 { .val = LFS_MIRROR_STATE_OPT,
4814 .name = "mirror-state", .has_arg = required_argument },
4815 { .val = LFS_NEWERXY_OPT,
4816 .name = "newer", .has_arg = required_argument},
4817 { .val = LFS_NEWERXY_OPT,
4818 .name = "neweraa", .has_arg = required_argument},
4819 { .val = LFS_NEWERXY_OPT,
4820 .name = "neweram", .has_arg = required_argument},
4821 { .val = LFS_NEWERXY_OPT,
4822 .name = "newerac", .has_arg = required_argument},
4823 { .val = LFS_NEWERXY_OPT,
4824 .name = "newerab", .has_arg = required_argument},
4825 { .val = LFS_NEWERXY_OPT,
4826 .name = "newerma", .has_arg = required_argument},
4827 { .val = LFS_NEWERXY_OPT,
4828 .name = "newermm", .has_arg = required_argument},
4829 { .val = LFS_NEWERXY_OPT,
4830 .name = "newermc", .has_arg = required_argument},
4831 { .val = LFS_NEWERXY_OPT,
4832 .name = "newermb", .has_arg = required_argument},
4833 { .val = LFS_NEWERXY_OPT,
4834 .name = "newerca", .has_arg = required_argument},
4835 { .val = LFS_NEWERXY_OPT,
4836 .name = "newercm", .has_arg = required_argument},
4837 { .val = LFS_NEWERXY_OPT,
4838 .name = "newercc", .has_arg = required_argument},
4839 { .val = LFS_NEWERXY_OPT,
4840 .name = "newercb", .has_arg = required_argument},
4841 { .val = LFS_NEWERXY_OPT,
4842 .name = "newerba", .has_arg = required_argument},
4843 { .val = LFS_NEWERXY_OPT,
4844 .name = "newerbm", .has_arg = required_argument},
4845 { .val = LFS_NEWERXY_OPT,
4846 .name = "newerbc", .has_arg = required_argument},
4847 { .val = LFS_NEWERXY_OPT,
4848 .name = "newerbb", .has_arg = required_argument},
4849 { .val = LFS_NEWERXY_OPT,
4850 .name = "newerBa", .has_arg = required_argument},
4851 { .val = LFS_NEWERXY_OPT,
4852 .name = "newerBm", .has_arg = required_argument},
4853 { .val = LFS_NEWERXY_OPT,
4854 .name = "newerBc", .has_arg = required_argument},
4855 { .val = LFS_NEWERXY_OPT,
4856 .name = "newerBB", .has_arg = required_argument},
4857 { .val = LFS_NEWERXY_OPT,
4858 .name = "newerat", .has_arg = required_argument},
4859 { .val = LFS_NEWERXY_OPT,
4860 .name = "newermt", .has_arg = required_argument},
4861 { .val = LFS_NEWERXY_OPT,
4862 .name = "newerct", .has_arg = required_argument},
4863 { .val = LFS_NEWERXY_OPT,
4864 .name = "newerbt", .has_arg = required_argument},
4865 { .val = LFS_NEWERXY_OPT,
4866 .name = "newerBt", .has_arg = required_argument},
4867 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4868 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4869 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4870 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4871 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4872 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4873 { .val = 'E', .name = "component-end",
4874 .has_arg = required_argument },
4875 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4876 { .val = LFS_LAYOUT_FOREIGN_OPT,
4877 .name = "foreign", .has_arg = optional_argument},
4878 { .val = 'g', .name = "gid", .has_arg = required_argument },
4879 { .val = 'G', .name = "group", .has_arg = required_argument },
4880 { .val = 'h', .name = "help", .has_arg = no_argument },
4881 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4882 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4883 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4884 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4885 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4886 { .val = 'L', .name = "layout", .has_arg = required_argument },
4887 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4888 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4889 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4890 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4891 { .val = 'n', .name = "name", .has_arg = required_argument },
4892 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4893 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4894 { .val = 'O', .name = "obd", .has_arg = required_argument },
4895 { .val = 'O', .name = "ost", .has_arg = required_argument },
4896 { .val = LFS_FIND_PERM,
4897 .name = "perm", .has_arg = required_argument },
4898 /* no short option for pool yet, can be 'p' after 2.18 */
4899 { .val = LFS_POOL_OPT,
4900 .name = "pool", .has_arg = required_argument },
4901 { .val = '0', .name = "print0", .has_arg = no_argument },
4902 { .val = 'P', .name = "print", .has_arg = no_argument },
4903 { .val = LFS_PROJID_OPT,
4904 .name = "projid", .has_arg = required_argument },
4905 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4906 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4907 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4908 { .val = 's', .name = "size", .has_arg = required_argument },
4909 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4910 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4911 { .val = 't', .name = "type", .has_arg = required_argument },
4912 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4913 { .val = 'u', .name = "uid", .has_arg = required_argument },
4914 { .val = 'U', .name = "user", .has_arg = required_argument },
4915 { .val = 'z', .name = "extension-size",
4916 .has_arg = required_argument },
4917 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4918 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4919 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4933 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4934 while ((c = getopt_long_only(argc, argv,
4935 "-0A:b:B:c:C:D:E:g:G:hH:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4936 long_opts, &optidx)) >= 0) {
4941 /* '!' is part of option */
4943 * when getopt_long_only() finds a string which is not
4944 * an option nor a known option argument it returns 1
4945 * in that case if we already have found pathstart and pathend
4946 * (i.e. we have the list of pathnames),
4947 * the only supported value is "!"
4949 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4950 if (!isoption && pathend != -1) {
4952 "err: %s: filename|dirname must either precede options or follow options\n",
4957 if (!isoption && pathstart == -1)
4958 pathstart = optind - 1;
4959 if (isoption && pathstart != -1 && pathend == -1)
4960 pathend = optind - 2;
4967 * unknown; opt is "!" or path component,
4968 * checking done above.
4970 if (strcmp(optarg, "!") == 0)
4974 xtime = ¶m.fp_atime;
4975 xsign = ¶m.fp_asign;
4976 param.fp_exclude_atime = !!neg_opt;
4977 /* no break, this falls through to 'B' for btime */
4980 xtime = ¶m.fp_btime;
4981 xsign = ¶m.fp_bsign;
4982 param.fp_exclude_btime = !!neg_opt;
4984 /* no break, this falls through to 'C' for ctime */
4987 xtime = ¶m.fp_ctime;
4988 xsign = ¶m.fp_csign;
4989 param.fp_exclude_ctime = !!neg_opt;
4991 /* no break, this falls through to 'M' for mtime */
4994 xtime = ¶m.fp_mtime;
4995 xsign = ¶m.fp_msign;
4996 param.fp_exclude_mtime = !!neg_opt;
4998 rc = set_time(¶m, &t, xtime, optarg);
4999 if (rc == LONG_MAX) {
5007 if (optarg[0] == '+') {
5008 param.fp_blocks_sign = -1;
5010 } else if (optarg[0] == '-') {
5011 param.fp_blocks_sign = 1;
5015 param.fp_blocks_units = 1024;
5016 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5017 ¶m.fp_blocks_units, 0);
5019 fprintf(stderr, "error: bad blocks '%s'\n",
5023 param.fp_check_blocks = 1;
5024 param.fp_exclude_blocks = !!neg_opt;
5026 case LFS_COMP_COUNT_OPT:
5027 if (optarg[0] == '+') {
5028 param.fp_comp_count_sign = -1;
5030 } else if (optarg[0] == '-') {
5031 param.fp_comp_count_sign = 1;
5036 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5037 if (errno != 0 || *endptr != '\0' ||
5038 param.fp_comp_count > UINT32_MAX) {
5040 "error: bad component count '%s'\n",
5044 param.fp_check_comp_count = 1;
5045 param.fp_exclude_comp_count = !!neg_opt;
5047 case LFS_COMP_FLAGS_OPT:
5048 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5049 ¶m.fp_comp_neg_flags);
5052 "error: bad component flags '%s'\n",
5056 param.fp_check_comp_flags = 1;
5058 __u32 flags = param.fp_comp_neg_flags;
5060 param.fp_comp_neg_flags = param.fp_comp_flags;
5061 param.fp_comp_flags = flags;
5064 case LFS_COMP_START_OPT:
5065 if (optarg[0] == '+') {
5066 param.fp_comp_start_sign = -1;
5068 } else if (optarg[0] == '-') {
5069 param.fp_comp_start_sign = 1;
5073 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5074 ¶m.fp_comp_start_units, 0);
5077 "error: bad component start '%s'\n",
5081 param.fp_check_comp_start = 1;
5082 param.fp_exclude_comp_start = !!neg_opt;
5084 case LFS_MIRROR_STATE_OPT:
5085 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5086 ¶m.fp_mirror_neg_state);
5089 "error: bad mirrored file state '%s'\n",
5093 param.fp_check_mirror_state = 1;
5095 __u16 state = param.fp_mirror_neg_state;
5097 param.fp_mirror_neg_state =
5098 param.fp_mirror_state;
5099 param.fp_mirror_state = state;
5103 if (optarg[0] == '+') {
5104 param.fp_stripe_count_sign = -1;
5106 } else if (optarg[0] == '-') {
5107 param.fp_stripe_count_sign = 1;
5112 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5113 if (errno != 0 || *endptr != '\0' ||
5114 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5116 "error: bad stripe_count '%s'\n",
5121 param.fp_check_stripe_count = 1;
5122 param.fp_exclude_stripe_count = !!neg_opt;
5126 param.fp_max_depth = strtol(optarg, 0, 0);
5127 if (errno != 0 || param.fp_max_depth < 0) {
5129 "error: bad maxdepth '%s'\n",
5136 if (optarg[0] == '+') {
5137 param.fp_comp_end_sign = -1;
5139 } else if (optarg[0] == '-') {
5140 param.fp_comp_end_sign = 1;
5144 if (arg_is_eof(optarg)) {
5145 param.fp_comp_end = LUSTRE_EOF;
5146 param.fp_comp_end_units = 1;
5149 rc = llapi_parse_size(optarg,
5151 ¶m.fp_comp_end_units, 0);
5152 /* assume units of KB if too small */
5153 if (param.fp_comp_end < 4096)
5154 param.fp_comp_end *= 1024;
5158 "error: bad component end '%s'\n",
5162 param.fp_check_comp_end = 1;
5163 param.fp_exclude_comp_end = !!neg_opt;
5165 case LFS_LAYOUT_FOREIGN_OPT: {
5166 /* all types by default */
5167 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5170 /* check pure numeric */
5171 type = strtoul(optarg, &endptr, 0);
5174 type = check_foreign_type_name(optarg);
5175 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5177 "%s %s: unknown foreign type '%s'\n",
5182 } else if (type >= UINT32_MAX) {
5184 "%s %s: invalid foreign type '%s'\n",
5185 progname, argv[0], optarg);
5189 param.fp_foreign_type = type;
5190 param.fp_check_foreign = 1;
5191 param.fp_exclude_foreign = !!neg_opt;
5194 case LFS_NEWERXY_OPT: {
5200 time_t ref = time(NULL);
5202 /* no need to check bad options, they won't get here */
5203 if (strlen(long_opts[optidx].name) == 7) {
5204 x = long_opts[optidx].name[5];
5205 y = long_opts[optidx].name[6];
5209 static const char *const fmts[] = {
5210 "%Y-%m-%d %H:%M:%S",
5213 "%H:%M:%S", /* sometime today */
5222 for (i = 0; fmts[i] != NULL; i++) {
5225 /* Init for times relative to today */
5226 if (strncmp(fmts[i], "%H", 2) == 0)
5227 localtime_r(&ref, &tm);
5229 memset(&tm, 0, sizeof(tm));
5230 ptr = strptime(optarg, fmts[i], &tm);
5232 while (ptr && isspace(*ptr))
5234 if (ptr == optarg + strlen(optarg)) {
5242 "%s: invalid time '%s'\n",
5245 "supported formats are:\n ");
5246 for (i = 0; fmts[i] != NULL; i++)
5247 fprintf(stderr, "'%s', ",
5249 fprintf(stderr, "\n");
5255 } else if (y == 'b' || y == 'B') {
5258 rc = llapi_get_lum_file(optarg, NULL, &stx,
5260 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5261 if (!(stx.stx_mask & STATX_BTIME))
5266 "%s: get btime failed '%s': %s\n",
5272 ref = stx.stx_btime.tv_sec;
5274 struct stat statbuf;
5276 if (stat(optarg, &statbuf) < 0) {
5278 "%s: cannot stat file '%s': %s\n",
5287 ref = statbuf.st_atime;
5290 ref = statbuf.st_mtime;
5293 ref = statbuf.st_ctime;
5297 "%s: invalid Y argument: '%c'\n",
5306 xidx = NEWERXY_ATIME;
5309 xidx = NEWERXY_MTIME;
5312 xidx = NEWERXY_CTIME;
5316 xidx = NEWERXY_BTIME;
5320 "%s: invalid X argument: '%c'\n",
5327 newery = ¶m.fp_newery[xidx][negidx];
5333 *newery = *newery > ref ? ref : *newery;
5335 *newery = *newery > ref ? *newery : ref;
5337 param.fp_newerxy = 1;
5342 rc = name2gid(¶m.fp_gid, optarg);
5344 if (str2quotaid(¶m.fp_gid, optarg)) {
5346 "Group/GID: %s cannot be found.\n",
5352 param.fp_exclude_gid = !!neg_opt;
5353 param.fp_check_gid = 1;
5356 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5357 ¶m.fp_hash_exflags,
5358 ¶m.fp_hash_type);
5363 if (param.fp_hash_inflags || param.fp_hash_exflags)
5364 param.fp_check_hash_flag = 1;
5365 param.fp_exclude_hash_type = !!neg_opt;
5371 ret = name2layout(¶m.fp_layout, optarg);
5374 param.fp_exclude_layout = !!neg_opt;
5375 param.fp_check_layout = 1;
5379 rc = name2uid(¶m.fp_uid, optarg);
5381 if (str2quotaid(¶m.fp_uid, optarg)) {
5383 "User/UID: %s cannot be found.\n",
5389 param.fp_exclude_uid = !!neg_opt;
5390 param.fp_check_uid = 1;
5393 param.fp_pattern = (char *)optarg;
5394 param.fp_exclude_pattern = !!neg_opt;
5397 if (optarg[0] == '+') {
5398 param.fp_mirror_count_sign = -1;
5400 } else if (optarg[0] == '-') {
5401 param.fp_mirror_count_sign = 1;
5406 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5407 if (errno != 0 || *endptr != '\0' ||
5408 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5410 "error: bad mirror count '%s'\n",
5414 param.fp_check_mirror_count = 1;
5415 param.fp_exclude_mirror_count = !!neg_opt;
5420 char *buf, *token, *next, *p;
5424 buf = strdup(optarg);
5430 param.fp_exclude_obd = !!neg_opt;
5433 while (token && *token) {
5434 token = strchr(token, ',');
5441 param.fp_exclude_mdt = !!neg_opt;
5442 param.fp_num_alloc_mdts += len;
5443 tmp = realloc(param.fp_mdt_uuid,
5444 param.fp_num_alloc_mdts *
5445 sizeof(*param.fp_mdt_uuid));
5451 param.fp_mdt_uuid = tmp;
5453 param.fp_exclude_obd = !!neg_opt;
5454 param.fp_num_alloc_obds += len;
5455 tmp = realloc(param.fp_obd_uuid,
5456 param.fp_num_alloc_obds *
5457 sizeof(*param.fp_obd_uuid));
5463 param.fp_obd_uuid = tmp;
5465 for (token = buf; token && *token; token = next) {
5466 struct obd_uuid *puuid;
5470 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5473 ¶m.fp_obd_uuid[param.fp_num_obds++];
5475 p = strchr(token, ',');
5482 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5487 strncpy(puuid->uuid, token,
5488 sizeof(puuid->uuid));
5495 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5499 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5501 "Pool name %s is too long (max %d)\n",
5502 optarg, LOV_MAXPOOLNAME);
5507 * We do check for empty pool because empty pool
5508 * is used to find V1 LOV attributes
5510 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5511 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5512 param.fp_exclude_pool = !!neg_opt;
5513 param.fp_check_pool = 1;
5515 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5516 case 'p': /* want this for --pool, to match getstripe/find */
5518 "warning: -p deprecated, use --print0 or -0\n");
5521 param.fp_zero_end = 1;
5523 case 'P': /* we always print, this option is a no-op */
5525 case LFS_PROJID_OPT:
5526 rc = name2projid(¶m.fp_projid, optarg);
5528 if (str2quotaid(¶m.fp_projid, optarg)) {
5530 "Invalid project ID: %s\n",
5536 param.fp_exclude_projid = !!neg_opt;
5537 param.fp_check_projid = 1;
5540 if (optarg[0] == '+') {
5541 param.fp_size_sign = -1;
5543 } else if (optarg[0] == '-') {
5544 param.fp_size_sign = 1;
5548 ret = llapi_parse_size(optarg, ¶m.fp_size,
5549 ¶m.fp_size_units, 0);
5551 fprintf(stderr, "error: bad file size '%s'\n",
5555 param.fp_check_size = 1;
5556 param.fp_exclude_size = !!neg_opt;
5559 if (optarg[0] == '+') {
5560 param.fp_stripe_size_sign = -1;
5562 } else if (optarg[0] == '-') {
5563 param.fp_stripe_size_sign = 1;
5567 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5568 ¶m.fp_stripe_size_units, 0);
5569 /* assume units of KB if too small to be valid */
5570 if (param.fp_stripe_size < 4096)
5571 param.fp_stripe_size *= 1024;
5573 fprintf(stderr, "error: bad stripe_size '%s'\n",
5577 param.fp_check_stripe_size = 1;
5578 param.fp_exclude_stripe_size = !!neg_opt;
5581 param.fp_exclude_type = !!neg_opt;
5582 switch (optarg[0]) {
5584 param.fp_type = S_IFBLK;
5587 param.fp_type = S_IFCHR;
5590 param.fp_type = S_IFDIR;
5593 param.fp_type = S_IFREG;
5596 param.fp_type = S_IFLNK;
5599 param.fp_type = S_IFIFO;
5602 param.fp_type = S_IFSOCK;
5605 fprintf(stderr, "%s: bad type '%s'\n",
5612 param.fp_exclude_perm = !!neg_opt;
5613 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5614 if (*optarg == '/') {
5615 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5617 } else if (*optarg == '-') {
5618 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5622 if (str2mode_t(optarg, ¶m.fp_perm)) {
5623 fprintf(stderr, "error: invalid mode '%s'\n",
5630 if (optarg[0] == '+') {
5631 param.fp_mdt_count_sign = -1;
5633 } else if (optarg[0] == '-') {
5634 param.fp_mdt_count_sign = 1;
5639 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5640 if (errno != 0 || *endptr != '\0' ||
5641 param.fp_mdt_count >= UINT32_MAX) {
5642 fprintf(stderr, "error: bad mdt_count '%s'\n",
5647 param.fp_check_mdt_count = 1;
5648 param.fp_exclude_mdt_count = !!neg_opt;
5651 if (optarg[0] == '+') {
5652 param.fp_ext_size_sign = -1;
5654 } else if (optarg[0] == '-') {
5655 param.fp_ext_size_sign = 1;
5659 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5660 ¶m.fp_ext_size_units, 0);
5662 fprintf(stderr, "error: bad ext-size '%s'\n",
5666 param.fp_ext_size /= SEL_UNIT_SIZE;
5667 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5668 param.fp_check_ext_size = 1;
5669 param.fp_exclude_ext_size = !!neg_opt;
5672 fprintf(stderr, "%s: unrecognized option '%s'\n",
5673 progname, argv[optind - 1]);
5680 if (pathstart == -1) {
5681 fprintf(stderr, "error: %s: no filename|pathname\n",
5685 } else if (pathend == -1) {
5691 rc = llapi_find(argv[pathstart], ¶m);
5694 pathbad = pathstart;
5696 } while (++pathstart < pathend);
5699 fprintf(stderr, "%s: failed for '%s': %s\n",
5700 progname, argv[pathbad], strerror(-rc));
5703 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5704 free(param.fp_obd_uuid);
5706 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5707 free(param.fp_mdt_uuid);
5712 static int lfs_getstripe_internal(int argc, char **argv,
5713 struct find_param *param)
5715 struct option long_opts[] = {
5716 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5717 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5718 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5719 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5720 { .val = LFS_COMP_COUNT_OPT,
5721 .name = "comp-count", .has_arg = no_argument },
5722 { .val = LFS_COMP_COUNT_OPT,
5723 .name = "component-count", .has_arg = no_argument },
5724 { .val = LFS_COMP_FLAGS_OPT,
5725 .name = "comp-flags", .has_arg = optional_argument },
5726 { .val = LFS_COMP_FLAGS_OPT,
5727 .name = "component-flags", .has_arg = optional_argument },
5728 { .val = LFS_COMP_START_OPT,
5729 .name = "comp-start", .has_arg = optional_argument },
5730 { .val = LFS_COMP_START_OPT,
5731 .name = "component-start", .has_arg = optional_argument },
5732 { .val = LFS_MIRROR_INDEX_OPT,
5733 .name = "mirror-index", .has_arg = required_argument },
5734 { .val = LFS_MIRROR_ID_OPT,
5735 .name = "mirror-id", .has_arg = required_argument },
5736 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5737 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5738 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5739 { .val = 'd', .name = "directory", .has_arg = no_argument },
5740 { .val = 'D', .name = "default", .has_arg = no_argument },
5741 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5742 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5743 { .val = 'F', .name = "fid", .has_arg = no_argument },
5744 { .val = 'g', .name = "generation", .has_arg = no_argument },
5745 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5746 { .val = 'h', .name = "help", .has_arg = no_argument },
5747 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5748 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5749 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5750 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5751 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5752 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5753 { .val = 'L', .name = "layout", .has_arg = no_argument },
5754 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5755 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5756 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5757 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5758 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5759 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5760 { .val = 'O', .name = "obd", .has_arg = required_argument },
5761 { .val = 'O', .name = "ost", .has_arg = required_argument },
5762 { .val = 'p', .name = "pool", .has_arg = no_argument },
5763 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5764 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5765 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5766 { .val = 'R', .name = "raw", .has_arg = no_argument },
5767 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5768 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5769 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5770 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5771 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5772 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5773 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5774 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5775 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5776 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5777 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5781 int pathstart = -1, pathend = -1;
5785 while ((c = getopt_long(argc, argv,
5786 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5787 long_opts, NULL)) != -1) {
5791 /* '!' is part of option */
5792 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5793 if (!isoption && pathend != -1) {
5795 "error: %s: filename|dirname must either precede options or follow options\n",
5799 if (!isoption && pathstart == -1)
5800 pathstart = optind - 1;
5801 if (isoption && pathstart != -1 && pathend == -1)
5802 pathend = optind - 2;
5806 /* unknown: opt is "!" */
5807 if (strcmp(optarg, "!") == 0)
5811 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5812 param->fp_verbose |= VERBOSE_COUNT;
5813 param->fp_max_depth = 0;
5816 case LFS_COMP_COUNT_OPT:
5817 param->fp_verbose |= VERBOSE_COMP_COUNT;
5818 param->fp_max_depth = 0;
5820 case LFS_COMP_FLAGS_OPT:
5822 rc = comp_str2flags(optarg,
5823 ¶m->fp_comp_flags,
5824 ¶m->fp_comp_neg_flags);
5827 "error: %s bad component flags '%s'.\n",
5831 param->fp_check_comp_flags = 1;
5833 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5834 param->fp_max_depth = 0;
5837 case LFS_COMP_START_OPT:
5840 if (tmp[0] == '+') {
5841 param->fp_comp_start_sign = -1;
5843 } else if (tmp[0] == '-') {
5844 param->fp_comp_start_sign = 1;
5847 rc = llapi_parse_size(tmp,
5848 ¶m->fp_comp_start,
5849 ¶m->fp_comp_start_units, 0);
5852 "error: %s bad component start '%s'.\n",
5856 param->fp_check_comp_start = 1;
5858 param->fp_verbose |= VERBOSE_COMP_START;
5859 param->fp_max_depth = 0;
5862 case LFS_MIRROR_INDEX_OPT: {
5863 unsigned long int mirror_index;
5865 if (optarg[0] == '+') {
5866 param->fp_mirror_index_sign = -1;
5868 } else if (optarg[0] == '-') {
5869 param->fp_mirror_index_sign = 1;
5874 mirror_index = strtoul(optarg, &end, 0);
5875 if (errno != 0 || *end != '\0' ||
5876 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5877 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5879 "%s %s: invalid mirror index '%s'\n",
5880 progname, argv[0], optarg);
5884 param->fp_mirror_index = (__u16)mirror_index;
5886 if (param->fp_mirror_id != 0) {
5888 "%s %s: can't specify both mirror index and mirror ID\n",
5892 param->fp_check_mirror_index = 1;
5893 param->fp_exclude_mirror_index = !!neg_opt;
5896 case LFS_MIRROR_ID_OPT: {
5897 unsigned long int mirror_id;
5899 if (optarg[0] == '+') {
5900 param->fp_mirror_id_sign = -1;
5902 } else if (optarg[0] == '-') {
5903 param->fp_mirror_id_sign = 1;
5908 mirror_id = strtoul(optarg, &end, 0);
5909 if (errno != 0 || *end != '\0' ||
5910 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5911 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5913 "%s %s: invalid mirror ID '%s'\n",
5914 progname, argv[0], optarg);
5918 param->fp_mirror_id = (__u16)mirror_id;
5920 if (param->fp_mirror_index != 0) {
5922 "%s %s: can't specify both mirror index and mirror ID\n",
5926 param->fp_check_mirror_id = 1;
5927 param->fp_exclude_mirror_id = !!neg_opt;
5931 param->fp_max_depth = 0;
5934 param->fp_get_default_lmv = 1;
5939 if (tmp[0] == '+') {
5940 param->fp_comp_end_sign = -1;
5942 } else if (tmp[0] == '-') {
5943 param->fp_comp_end_sign = 1;
5947 if (arg_is_eof(tmp)) {
5948 param->fp_comp_end = LUSTRE_EOF;
5949 param->fp_comp_end_units = 1;
5952 rc = llapi_parse_size(tmp,
5953 ¶m->fp_comp_end,
5954 ¶m->fp_comp_end_units, 0);
5955 /* assume units of KB if too small */
5956 if (param->fp_comp_end < 4096)
5957 param->fp_comp_end *= 1024;
5961 "error: %s bad component end '%s'.\n",
5965 param->fp_check_comp_end = 1;
5967 param->fp_verbose |= VERBOSE_COMP_END;
5968 param->fp_max_depth = 0;
5972 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5973 param->fp_verbose |= VERBOSE_DFID;
5974 param->fp_max_depth = 0;
5978 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5979 param->fp_verbose |= VERBOSE_GENERATION;
5980 param->fp_max_depth = 0;
5984 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5985 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5986 param->fp_max_depth = 0;
5991 param->fp_comp_id = strtoul(optarg, &end, 0);
5992 if (*end != '\0' || param->fp_comp_id == 0 ||
5993 param->fp_comp_id > LCME_ID_MAX) {
5995 "error: %s bad component id '%s'\n",
5999 param->fp_check_comp_id = 1;
6001 param->fp_max_depth = 0;
6002 param->fp_verbose |= VERBOSE_COMP_ID;
6006 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6007 param->fp_verbose |= VERBOSE_PATTERN;
6008 param->fp_max_depth = 0;
6011 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6014 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6017 if (!(param->fp_verbose & VERBOSE_DETAIL))
6018 param->fp_max_depth = 0;
6019 param->fp_verbose |= VERBOSE_MDTINDEX;
6022 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6023 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6024 param->fp_max_depth = 0;
6028 if (param->fp_obd_uuid) {
6030 "error: %s: only one obduuid allowed",
6034 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6037 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6038 param->fp_verbose |= VERBOSE_POOL;
6039 param->fp_max_depth = 0;
6046 param->fp_recursive = 1;
6052 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6053 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6054 param->fp_max_depth = 0;
6058 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6064 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6065 param->fp_verbose |= VERBOSE_EXT_SIZE;
6066 param->fp_max_depth = 0;
6070 fprintf(stderr, "%s: unrecognized option '%s'\n",
6071 progname, argv[optind - 1]);
6077 if (pathstart == -1) {
6078 fprintf(stderr, "error: %s: no filename|pathname\n",
6081 } else if (pathend == -1) {
6089 if (param->fp_recursive)
6090 param->fp_max_depth = -1;
6091 else if (param->fp_verbose & VERBOSE_DETAIL)
6092 param->fp_max_depth = 1;
6094 if (!param->fp_verbose)
6095 param->fp_verbose = VERBOSE_DEFAULT;
6096 if (param->fp_quiet)
6097 param->fp_verbose = VERBOSE_OBJID;
6100 rc = llapi_getstripe(argv[pathstart], param);
6101 } while (++pathstart < pathend && !rc);
6104 fprintf(stderr, "error: %s failed for %s.\n",
6105 argv[0], argv[optind - 1]);
6109 static int lfs_tgts(int argc, char **argv)
6111 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6112 struct find_param param;
6113 int index = 0, rc = 0;
6118 if (argc == 2 && !realpath(argv[1], path)) {
6120 fprintf(stderr, "error: invalid path '%s': %s\n",
6121 argv[1], strerror(-rc));
6125 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6126 /* Check if we have a mount point */
6127 if (mntdir[0] == '\0')
6130 memset(¶m, 0, sizeof(param));
6131 if (!strcmp(argv[0], "mdts"))
6132 param.fp_get_lmv = 1;
6134 rc = llapi_ostlist(mntdir, ¶m);
6136 fprintf(stderr, "error: %s: failed on %s\n",
6139 if (path[0] != '\0')
6141 memset(mntdir, 0, PATH_MAX);
6147 static int lfs_getstripe(int argc, char **argv)
6149 struct find_param param = { 0 };
6151 param.fp_max_depth = 1;
6152 return lfs_getstripe_internal(argc, argv, ¶m);
6156 static int lfs_getdirstripe(int argc, char **argv)
6158 struct find_param param = { 0 };
6159 struct option long_opts[] = {
6160 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6161 { .val = 'D', .name = "default", .has_arg = no_argument },
6162 { .val = 'h', .name = "help", .has_arg = no_argument },
6163 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6164 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6165 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6166 { .val = 'O', .name = "obd", .has_arg = required_argument },
6167 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6168 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6169 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6170 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6171 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6172 { .val = LFS_INHERIT_RR_OPT,
6173 .name = "max-inherit-rr", .has_arg = no_argument },
6177 param.fp_get_lmv = 1;
6179 while ((c = getopt_long(argc, argv,
6180 "cDhHimO:rtTvXy", long_opts, NULL)) != -1) {
6184 param.fp_verbose |= VERBOSE_COUNT;
6187 param.fp_get_default_lmv = 1;
6189 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6192 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6196 param.fp_verbose |= VERBOSE_HASH_TYPE;
6198 case 'i': /* fallthrough */
6200 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6203 if (param.fp_obd_uuid) {
6205 "%s: only one obduuid allowed",
6209 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6212 param.fp_recursive = 1;
6215 param.fp_verbose |= VERBOSE_DETAIL;
6218 param.fp_verbose |= VERBOSE_INHERIT;
6220 case LFS_INHERIT_RR_OPT:
6221 param.fp_verbose |= VERBOSE_INHERIT_RR;
6227 fprintf(stderr, "%s: unrecognized option '%s'\n",
6228 progname, argv[optind - 1]);
6238 if (param.fp_recursive)
6239 param.fp_max_depth = -1;
6241 if (!param.fp_verbose)
6242 param.fp_verbose = VERBOSE_DEFAULT;
6245 rc = llapi_getstripe(argv[optind], ¶m);
6246 } while (++optind < argc && !rc);
6249 fprintf(stderr, "error: %s failed for %s.\n",
6250 argv[0], argv[optind - 1]);
6255 MNTDF_INODES = 0x0001,
6256 MNTDF_COOKED = 0x0002,
6257 MNTDF_LAZY = 0x0004,
6258 MNTDF_VERBOSE = 0x0008,
6259 MNTDF_SHOW = 0x0010,
6260 MNTDF_DECIMAL = 0x0020,
6263 #define COOK(value, base) \
6266 while (value > base) { \
6274 #define CDF "%11llu"
6275 #define HDF "%8.1f%c"
6279 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6281 double avail, used, ratio = 0;
6284 avail = st->os_ffree;
6285 used = st->os_files - st->os_ffree;
6287 avail = st->os_bavail;
6288 used = st->os_blocks - st->os_bfree;
6290 if (avail + used > 0)
6291 ratio = used / (used + avail) * 100;
6293 /* Round up to match df(1) usage percentage */
6294 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6298 * This is to identify various problem states for "lfs df" if .osn_err = true,
6299 * so only show flags reflecting those states by default. Informational states
6300 * are only shown with "-v" and use lower-case names to distinguish them.
6301 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6303 static struct obd_statfs_state_names {
6304 enum obd_statfs_state osn_state;
6305 const char osn_name;
6308 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6309 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6310 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6311 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6312 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6313 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6314 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6315 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6316 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6319 static int showdf(char *mntdir, struct obd_statfs *stat,
6320 char *uuid, enum mntdf_flags flags,
6321 char *type, int index, int rc)
6323 long long avail, used, total;
6325 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6326 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6327 char tbuf[3 * sizeof(__u64)];
6328 char ubuf[3 * sizeof(__u64)];
6329 char abuf[3 * sizeof(__u64)];
6330 char rbuf[3 * sizeof(__u64)];
6337 if (flags & MNTDF_INODES) {
6338 avail = stat->os_ffree;
6339 used = stat->os_files - stat->os_ffree;
6340 total = stat->os_files;
6342 int shift = flags & MNTDF_COOKED ? 0 : 10;
6344 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6345 used = ((stat->os_blocks - stat->os_bfree) *
6346 stat->os_bsize) >> shift;
6347 total = (stat->os_blocks * stat->os_bsize) >> shift;
6350 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6352 if (flags & MNTDF_COOKED) {
6353 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6357 cook_val = (double)total;
6358 i = COOK(cook_val, base);
6360 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6363 snprintf(tbuf, sizeof(tbuf), CDF, total);
6365 cook_val = (double)used;
6366 i = COOK(cook_val, base);
6368 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6371 snprintf(ubuf, sizeof(ubuf), CDF, used);
6373 cook_val = (double)avail;
6374 i = COOK(cook_val, base);
6376 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6379 snprintf(abuf, sizeof(abuf), CDF, avail);
6381 snprintf(tbuf, sizeof(tbuf), CDF, total);
6382 snprintf(ubuf, sizeof(tbuf), CDF, used);
6383 snprintf(abuf, sizeof(tbuf), CDF, avail);
6386 sprintf(rbuf, RDF, ratio);
6387 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6388 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6390 printf("[%s:%d]", type, index);
6392 if (stat->os_state) {
6396 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6397 if (oss_names[i].osn_state & stat->os_state &&
6398 (oss_names[i].osn_err ||
6399 flags & MNTDF_VERBOSE))
6400 printf("%c", oss_names[i].osn_name);
6407 printf(UUF": inactive device\n", uuid);
6410 printf(UUF": %s\n", uuid, strerror(-rc));
6417 struct ll_stat_type {
6422 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6424 struct ll_statfs_data {
6426 struct obd_statfs sd_st;
6429 struct ll_statfs_buf {
6431 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6434 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6435 int ops, struct ll_statfs_buf *lsb)
6437 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6438 struct obd_uuid uuid_buf;
6439 char *poolname = NULL;
6440 struct ll_stat_type types[] = {
6441 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6442 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6443 { .st_name = NULL } };
6444 struct ll_stat_type *tp;
6445 __u64 ost_files = 0;
6446 __u64 ost_ffree = 0;
6454 poolname = strchr(pool, '.');
6456 if (strncmp(fsname, pool, strlen(fsname))) {
6457 fprintf(stderr, "filesystem name incorrect\n");
6465 fd = open(mntdir, O_RDONLY);
6468 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6473 if (flags & MNTDF_SHOW) {
6474 if (flags & MNTDF_INODES)
6475 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6476 "UUID", "Inodes", "IUsed", "IFree",
6477 "IUse%", "Mounted on");
6479 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6481 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6482 "Used", "Available", "Use%", "Mounted on");
6485 for (tp = types; tp->st_name != NULL; tp++) {
6486 bool have_ost = false;
6488 if (!(tp->st_op & ops))
6491 for (index = 0; ; index++) {
6492 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6493 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6494 type = flags & MNTDF_LAZY ?
6495 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6496 rc2 = llapi_obd_fstatfs(fd, type, index,
6497 &stat_buf, &uuid_buf);
6502 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6503 if (!(flags & MNTDF_VERBOSE))
6505 } else if (rc2 < 0 && rc == 0) {
6510 * If we have OSTs then don't report MDT block counts.
6511 * For MDT-only filesystems the expectation is that all
6512 * layouts have a DoM component. For filesystems with
6513 * OSTs, files are not necessarily going to store data
6514 * on MDTs, and MDT space is limited to a fraction of
6515 * OST space, so don't include it in the summary.
6517 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6524 if (poolname && tp->st_op == LL_STATFS_LOV &&
6525 llapi_search_ost(fsname, poolname,
6526 obd_uuid2str(&uuid_buf)) != 1)
6530 * the llapi_obd_fstatfs() call may have returned with
6531 * an error, but if it filled in uuid_buf we will at
6532 * lease use that to print out a message for that OBD.
6533 * If we didn't get anything in the uuid_buf, then fill
6534 * it in so that we can print an error message.
6536 if (uuid_buf.uuid[0] == '\0')
6537 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6538 "%s%04x", tp->st_name, index);
6540 lsb->sb_buf[lsb->sb_count].sd_index = index;
6541 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6544 if (flags & MNTDF_SHOW)
6545 showdf(mntdir, &stat_buf,
6546 obd_uuid2str(&uuid_buf), flags,
6547 tp->st_name, index, rc2);
6552 if (tp->st_op == LL_STATFS_LMV) {
6553 sum.os_ffree += stat_buf.os_ffree;
6554 sum.os_files += stat_buf.os_files;
6555 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6556 ost_files += stat_buf.os_files;
6557 ost_ffree += stat_buf.os_ffree;
6559 sum.os_blocks += stat_buf.os_blocks *
6561 sum.os_bfree += stat_buf.os_bfree *
6563 sum.os_bavail += stat_buf.os_bavail *
6571 * If we have _some_ OSTs, but don't have as many free objects on the
6572 * OST as inodes on the MDTs, reduce the reported number of inodes
6573 * to compensate, so that the "inodes in use" number is correct.
6574 * This should be kept in sync with ll_statfs_internal().
6576 if (ost_files && ost_ffree < sum.os_ffree) {
6577 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6578 sum.os_ffree = ost_ffree;
6580 if (flags & MNTDF_SHOW) {
6582 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6590 LAYOUT_INHERIT_UNSET = -2,
6594 static int lfs_setdirstripe(int argc, char **argv)
6597 struct lfs_setstripe_args lsa = { 0 };
6598 struct llapi_stripe_param *param = NULL;
6599 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6602 char *mode_opt = NULL;
6603 bool default_stripe = false;
6604 bool delete = false;
6605 bool foreign_mode = false;
6606 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6607 mode_t previous_mode = 0;
6609 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6610 int max_inherit = LAYOUT_INHERIT_UNSET;
6611 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6612 struct option long_opts[] = {
6613 { .val = 'c', .name = "count", .has_arg = required_argument },
6614 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6615 { .val = 'd', .name = "delete", .has_arg = no_argument },
6616 { .val = 'D', .name = "default", .has_arg = no_argument },
6617 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6618 { .val = LFS_LAYOUT_FLAGS_OPT,
6619 .name = "flags", .has_arg = required_argument },
6620 { .val = LFS_LAYOUT_FOREIGN_OPT,
6621 .name = "foreign", .has_arg = optional_argument},
6622 { .val = 'h', .name = "help", .has_arg = no_argument },
6623 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6624 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6625 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6626 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6628 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6629 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6630 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6632 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6633 { .val = 'i', .name = "index", .has_arg = required_argument },
6635 { .val = 'o', .name = "mode", .has_arg = required_argument },
6636 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6637 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6639 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6640 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6641 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
6642 { .val = LFS_INHERIT_RR_OPT,
6643 .name = "max-inherit-rr", .has_arg = required_argument},
6644 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6648 setstripe_args_init(&lsa);
6650 while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
6651 long_opts, NULL)) >= 0) {
6659 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6660 if (errno != 0 || *end != '\0' ||
6661 lsa.lsa_stripe_count < -1 ||
6662 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6664 "%s: invalid stripe count '%s'\n",
6671 default_stripe = true;
6674 default_stripe = true;
6676 case LFS_LAYOUT_FOREIGN_OPT:
6678 /* check pure numeric */
6679 type = strtoul(optarg, &end, 0);
6682 type = check_foreign_type_name(optarg);
6683 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6685 "%s %s: unknown foreign type '%s'\n",
6690 } else if (type >= UINT32_MAX) {
6692 "%s %s: invalid foreign type '%s'\n",
6693 progname, argv[0], optarg);
6697 foreign_mode = true;
6699 case LFS_LAYOUT_FLAGS_OPT:
6701 flags = strtoul(optarg, &end, 16);
6702 if (errno != 0 || *end != '\0' ||
6703 flags >= UINT32_MAX) {
6705 "%s %s: invalid hex flags '%s'\n",
6706 progname, argv[0], optarg);
6709 if (!foreign_mode) {
6711 "%s %s: hex flags must be specified with --foreign option\n",
6716 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6719 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6723 lsa.lsa_pattern = check_hashtype(optarg);
6724 if (lsa.lsa_pattern == 0) {
6726 "%s %s: bad stripe hash type '%s'\n",
6727 progname, argv[0], optarg);
6732 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6735 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6736 if (strcmp(argv[optind - 1], "--index") == 0)
6738 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6741 lsa.lsa_nr_tgts = parse_targets(mdts,
6742 sizeof(mdts) / sizeof(__u32),
6743 lsa.lsa_nr_tgts, optarg, NULL);
6744 if (lsa.lsa_nr_tgts < 0) {
6746 "%s %s: invalid MDT target(s) '%s'\n",
6747 progname, argv[0], optarg);
6751 lsa.lsa_tgts = mdts;
6752 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6753 lsa.lsa_stripe_off = mdts[0];
6755 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6758 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6768 max_inherit = strtol(optarg, &end, 10);
6769 if (errno != 0 || *end != '\0' || max_inherit < -2) {
6771 "%s %s: invalid max-inherit '%s'\n",
6772 progname, argv[0], optarg);
6775 if (max_inherit == 0) {
6776 max_inherit = LMV_INHERIT_NONE;
6777 } else if (max_inherit == -1) {
6778 max_inherit = LMV_INHERIT_UNLIMITED;
6779 } else if (max_inherit > LMV_INHERIT_MAX) {
6781 "%s %s: max-inherit %d exceeds maximum %u\n",
6782 progname, argv[0], max_inherit,
6787 case LFS_INHERIT_RR_OPT:
6788 if (!default_stripe) {
6790 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6795 max_inherit_rr = strtol(optarg, &end, 10);
6796 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6798 "%s %s: invalid max-inherit-rr '%s'\n",
6799 progname, argv[0], optarg);
6802 if (max_inherit_rr == 0) {
6803 max_inherit_rr = LMV_INHERIT_RR_NONE;
6804 } else if (max_inherit_rr == -1) {
6805 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6806 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6808 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6809 progname, argv[0], max_inherit_rr,
6810 LMV_INHERIT_RR_MAX);
6815 fprintf(stderr, "%s: unrecognized option '%s'\n",
6816 progname, argv[optind - 1]);
6823 if (optind == argc) {
6824 fprintf(stderr, "%s %s: DIR must be specified\n",
6829 if (xattr && !foreign_mode) {
6831 * only print a warning as this is armless and will be
6835 "%s %s: xattr has been specified for non-foreign layout\n",
6837 } else if (foreign_mode && !xattr) {
6839 "%s %s: xattr must be provided in foreign mode\n",
6844 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6845 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6847 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6852 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6853 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6854 /* if no parameters set, create directory on least-used MDTs */
6855 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
6856 lsa.lsa_stripe_count = 1;
6860 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6861 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6863 "%s %s: cannot specify -d with -c or -i options\n",
6869 mode = strtoul(mode_opt, &end, 8);
6872 "%s %s: bad MODE '%s'\n",
6873 progname, argv[0], mode_opt);
6876 previous_mode = umask(0);
6879 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6880 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6881 lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
6883 "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
6884 progname, argv[0], lsa.lsa_stripe_off);
6888 /* foreign LMV/dir case */
6890 if (argc > optind + 1) {
6892 "%s %s: cannot specify multiple foreign dirs\n",
6897 dname = argv[optind];
6898 result = llapi_dir_create_foreign(dname, mode, type, flags,
6902 "%s mkdir: can't create foreign dir '%s': %s\n",
6903 progname, dname, strerror(-result));
6908 * initialize stripe parameters, in case param is converted to specific,
6909 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6911 param = calloc(1, offsetof(typeof(*param),
6912 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6913 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6916 "%s %s: cannot allocate memory for parameters: %s\n",
6917 progname, argv[0], strerror(ENOMEM));
6921 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6922 if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
6923 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6924 lsa.lsa_stripe_count == 0))
6925 lsa.lsa_stripe_count = 1;
6926 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6927 param->lsp_stripe_count = lsa.lsa_stripe_count;
6928 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6929 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
6931 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6932 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6933 param->lsp_stripe_pattern = lsa.lsa_pattern;
6935 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6936 param->lsp_pool = lsa.lsa_pool_name;
6937 param->lsp_is_specific = false;
6938 if (max_inherit == LAYOUT_INHERIT_UNSET)
6939 max_inherit = LMV_INHERIT_DEFAULT;
6940 param->lsp_max_inherit = max_inherit;
6941 if (default_stripe) {
6942 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
6943 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
6944 param->lsp_max_inherit_rr = max_inherit_rr;
6946 if (strcmp(argv[0], "mkdir") == 0)
6947 param->lsp_is_create = true;
6948 if (lsa.lsa_nr_tgts > 1) {
6949 if (lsa.lsa_stripe_count > 0 &&
6950 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6951 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6953 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6954 argv[0], lsa.lsa_stripe_count,
6960 param->lsp_is_specific = true;
6961 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6962 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6965 dname = argv[optind];
6967 if (default_stripe) {
6968 result = llapi_dir_set_default_lmv(dname, param);
6971 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6972 progname, dname, strerror(-result));
6976 result = llapi_dir_create(dname, mode, param);
6979 "%s setdirstripe: cannot create dir '%s': %s\n",
6980 progname, dname, strerror(-result));
6981 } while (!result && (dname = argv[++optind]));
6984 umask(previous_mode);
6991 static int lfs_rmentry(int argc, char **argv)
6998 fprintf(stderr, "error: %s: missing dirname\n",
7004 dname = argv[index];
7006 result = llapi_direntry_remove(dname);
7009 "error: %s: remove dir entry '%s' failed\n",
7013 dname = argv[++index];
7018 static int lfs_unlink_foreign(int argc, char **argv)
7025 fprintf(stderr, "error: %s: missing pathname\n",
7032 while (name != NULL) {
7033 result = llapi_unlink_foreign(name);
7036 "error: %s: unlink foreign entry '%s' failed\n",
7040 name = argv[++index];
7045 static int lfs_mv(int argc, char **argv)
7047 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7048 struct find_param param = {
7055 struct option long_opts[] = {
7056 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7057 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7058 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7061 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7063 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7066 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7070 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7071 if (errno != 0 || *end != '\0' ||
7072 lmu.lum_stripe_offset >= UINT32_MAX) {
7073 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7079 param.fp_verbose = VERBOSE_DETAIL;
7082 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7083 progname, argv[optind - 1]);
7088 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7089 fprintf(stderr, "%s mv: MDT index must be specified\n",
7094 if (optind >= argc) {
7095 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7099 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7101 /* initialize migrate mdt parameters */
7102 param.fp_lmv_md = &lmu;
7103 param.fp_migrate = 1;
7104 rc = llapi_migrate_mdt(argv[optind], ¶m);
7106 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7107 progname, argv[optind], lmu.lum_stripe_offset,
7112 static int lfs_osts(int argc, char **argv)
7114 return lfs_tgts(argc, argv);
7117 static int lfs_mdts(int argc, char **argv)
7119 return lfs_tgts(argc, argv);
7122 static int lfs_df(int argc, char **argv)
7124 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7125 enum mntdf_flags flags = MNTDF_SHOW;
7126 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7127 int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7128 char fsname[PATH_MAX] = "", *pool_name = NULL;
7129 struct option long_opts[] = {
7130 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7131 { .val = 'H', .name = "si", .has_arg = no_argument },
7132 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7133 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7134 { .val = 'p', .name = "pool", .has_arg = required_argument },
7135 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7138 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7141 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7144 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7147 flags |= MNTDF_INODES;
7150 flags |= MNTDF_LAZY;
7156 flags |= MNTDF_VERBOSE;
7159 fprintf(stderr, "%s: unrecognized option '%s'\n",
7160 progname, argv[optind - 1]);
7165 /* Handle case where path is not specified */
7166 if (optind == argc) {
7167 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7168 /* Check if we have a mount point */
7169 if (mntdir[0] == '\0')
7172 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7173 if (rc || path[0] != '\0')
7176 fsname[0] = '\0'; /* avoid matching in next loop */
7177 mntdir[0] = '\0'; /* avoid matching in next loop */
7178 path[0] = '\0'; /* clean for next loop */
7183 /* Loop through all the remaining arguments. These are Lustre FS
7186 for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7189 fsname[0] = '\0'; /* start clean */
7190 mntdir[0] = '\0'; /* start clean */
7191 path[0] = '\0'; /* start clean */
7193 /* path does not exists at all */
7194 if (!realpath(argv[arg_idx], path)) {
7196 fprintf(stderr, "error: invalid path '%s': %s\n",
7197 argv[arg_idx], strerror(-rc));
7198 /* save first seen error */
7205 /* path exists but may not be a Lustre filesystem */
7206 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7207 /* Check if we have a mount point */
7208 if (mntdir[0] == '\0')
7211 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7212 if (rc || path[0] != '\0') {
7215 /* save first seen error */
7223 llapi_printf(LLAPI_MSG_ERROR,
7224 "%s:%s Not a Lustre filesystem\n",
7225 argv[0], argv[arg_idx]);
7226 /* save first seen error */
7235 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7236 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7240 if (opt_fsname == opt_instance) { /* both true or both false */
7241 rc = llapi_getname(mntdir, buf, buflen);
7242 } else if (opt_fsname) {
7244 * llapi_search_mounts() fills @buf with fsname, but that is not
7245 * called if explicit paths are specified on the command-line
7248 rc = llapi_get_fsname(mntdir, buf, buflen);
7249 } else /* if (opt_instance) */ {
7250 rc = llapi_get_instance(mntdir, buf, buflen);
7254 fprintf(stderr, "cannot get instance for '%s': %s\n",
7255 mntdir, strerror(-rc));
7260 printf("%s %s\n", buf, mntdir);
7262 printf("%s\n", buf);
7267 static int lfs_getname(int argc, char **argv)
7269 struct option long_opts[] = {
7270 { .val = 'h', .name = "help", .has_arg = no_argument },
7271 { .val = 'i', .name = "instance", .has_arg = no_argument },
7272 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7274 bool opt_instance = false, opt_fsname = false;
7275 char fsname[PATH_MAX] = "";
7278 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7281 opt_instance = true;
7287 fprintf(stderr, "%s: unrecognized option '%s'\n",
7288 progname, argv[optind - 1]);
7295 if (optind == argc) { /* no paths specified, get all paths. */
7296 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7299 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7300 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7301 opt_instance, opt_fsname, true);
7304 path[0] = fsname[0] = mntdir[0] = '\0';
7306 } else { /* paths specified, only attempt to search these. */
7309 /* if only one path is given, print only requested info */
7310 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7312 for (; optind < argc; optind++) {
7313 rc2 = print_instance(argv[optind], fsname,
7314 sizeof(fsname), opt_instance,
7315 opt_fsname, opt_mntdir);
7325 static int lfs_check(int argc, char **argv)
7327 char mntdir[PATH_MAX] = {'\0'};
7336 fprintf(stderr, "%s check: server type must be specified\n",
7341 obd_types[0] = obd_type1;
7342 obd_types[1] = obd_type2;
7343 obd_types[2] = obd_type3;
7345 if (strcmp(argv[1], "osts") == 0) {
7346 strcpy(obd_types[0], "osc");
7347 } else if (strcmp(argv[1], "mdts") == 0 ||
7348 strcmp(argv[1], "mds") == 0) {
7349 strcpy(obd_types[0], "mdc");
7350 } else if (strcmp(argv[1], "mgts") == 0) {
7351 strcpy(obd_types[0], "mgc");
7352 } else if (strcmp(argv[1], "all") == 0 ||
7353 strcmp(argv[1], "servers") == 0) {
7355 strcpy(obd_types[0], "osc");
7356 strcpy(obd_types[1], "mdc");
7357 strcpy(obd_types[2], "mgc");
7359 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7364 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
7365 if (rc < 0 || mntdir[0] == '\0') {
7367 "%s check: cannot find mounted Lustre filesystem: %s\n",
7368 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7372 rc = llapi_target_check(num_types, obd_types, mntdir);
7374 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7375 progname, argv[1], strerror(-rc));
7380 #ifdef HAVE_SYS_QUOTA_H
7381 #define ADD_OVERFLOW(a, b) \
7382 ((((a) + (b)) < (a)) ? \
7383 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7385 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7386 * returns the value or ULONG_MAX on integer overflow or incorrect format
7388 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7389 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7390 * 3. empty integer value is interpreted as 0
7392 static unsigned long str2sec(const char *timestr)
7394 const char spec[] = "smhdw";
7395 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7396 unsigned long val = 0;
7399 if (strpbrk(timestr, spec) == NULL) {
7401 * no specifiers inside the time string,
7402 * should treat it as an integer value
7404 val = strtoul(timestr, &tail, 10);
7405 return *tail ? ULONG_MAX : val;
7408 /* format string is XXwXXdXXhXXmXXs */
7414 v = strtoul(timestr, &tail, 10);
7415 if (v == ULONG_MAX || *tail == '\0')
7417 * value too large (ULONG_MAX or more)
7418 * or missing specifier
7422 ptr = strchr(spec, *tail);
7424 /* unknown specifier */
7429 /* check if product will overflow the type */
7430 if (!(v < ULONG_MAX / mult[ind]))
7433 ADD_OVERFLOW(val, mult[ind] * v);
7434 if (val == ULONG_MAX)
7446 #define ARG2ULL(nr, str, def_units) \
7448 unsigned long long limit, units = def_units; \
7451 rc = llapi_parse_size(str, &limit, &units, 1); \
7453 fprintf(stderr, "%s: invalid limit '%s'\n", \
7460 static inline int has_times_option(int argc, char **argv)
7464 for (i = 1; i < argc; i++)
7465 if (!strcmp(argv[i], "-t"))
7471 static inline int lfs_verify_poolarg(char *pool)
7473 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7475 "Pool name '%.*s' is longer than %d\n",
7476 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7482 /* special grace time, only notify the user when its quota is over soft limit
7483 * but doesn't block new writes until the hard limit is reached.
7485 #define NOTIFY_GRACE "notify"
7486 #define NOTIFY_GRACE_TIME LQUOTA_GRACE_MASK
7489 static inline __u64 lustre_stoqb(size_t space)
7491 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7494 #define lustre_stoqb toqb
7497 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7500 char *mnt, *obd_type = (char *)qctl->obd_type;
7501 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7502 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7503 struct option long_opts[] = {
7504 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7505 { .val = 'g', .name = "group", .has_arg = no_argument },
7506 { .val = 'h', .name = "help", .has_arg = no_argument },
7507 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7508 { .val = 'p', .name = "projid", .has_arg = no_argument },
7509 { .val = 't', .name = "times", .has_arg = no_argument },
7510 { .val = 'u', .name = "user", .has_arg = no_argument },
7511 { .val = LFS_POOL_OPT,
7512 .name = "pool", .has_arg = required_argument },
7516 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7517 qctl->qc_type = ALLQUOTA;
7519 while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7520 long_opts, NULL)) != -1) {
7531 if (qctl->qc_type != ALLQUOTA) {
7533 "%s: -u/g/p cannot be used more than once\n",
7537 qctl->qc_type = qtype;
7540 if (strncmp(optarg, NOTIFY_GRACE,
7541 strlen(NOTIFY_GRACE)) == 0) {
7542 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7544 dqi->dqi_bgrace = str2sec(optarg);
7545 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7547 "%s: bad block-grace: %s\n",
7552 dqb->dqb_valid |= QIF_BTIME;
7555 if (strncmp(optarg, NOTIFY_GRACE,
7556 strlen(NOTIFY_GRACE)) == 0) {
7557 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7559 dqi->dqi_igrace = str2sec(optarg);
7560 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7562 "%s: bad inode-grace: %s\n",
7567 dqb->dqb_valid |= QIF_ITIME;
7569 case 't': /* Yes, of course! */
7572 if (lfs_verify_poolarg(optarg))
7574 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7575 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7577 /* getopt prints error message for us when opterr != 0 */
7579 fprintf(stderr, "%s: unrecognized option '%s'\n",
7580 progname, argv[optind - 1]);
7587 if (qctl->qc_type == ALLQUOTA) {
7588 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7593 if (optind != argc - 1) {
7594 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7595 progname, argv[optind + 1]);
7600 rc = llapi_quotactl(mnt, qctl);
7603 fprintf(stderr, "%s %s ", obd_type,
7604 obd_uuid2str(&qctl->obd_uuid));
7605 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7612 #define BSLIMIT (1 << 0)
7613 #define BHLIMIT (1 << 1)
7614 #define ISLIMIT (1 << 2)
7615 #define IHLIMIT (1 << 3)
7617 int lfs_setquota(int argc, char **argv)
7620 struct if_quotactl *qctl;
7621 char *mnt, *obd_type;
7622 struct obd_dqblk *dqb;
7623 struct option long_opts[] = {
7624 { .val = 'b', .name = "block-softlimit",
7625 .has_arg = required_argument },
7626 { .val = 'B', .name = "block-hardlimit",
7627 .has_arg = required_argument },
7628 { .val = 'd', .name = "default", .has_arg = no_argument },
7629 { .val = 'g', .name = "group", .has_arg = required_argument },
7630 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7631 { .val = 'h', .name = "help", .has_arg = no_argument },
7632 { .val = 'i', .name = "inode-softlimit",
7633 .has_arg = required_argument },
7634 { .val = 'I', .name = "inode-hardlimit",
7635 .has_arg = required_argument },
7636 { .val = 'p', .name = "projid", .has_arg = required_argument },
7637 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7638 { .val = 'u', .name = "user", .has_arg = required_argument },
7639 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7640 { .val = LFS_POOL_OPT,
7641 .name = "pool", .has_arg = required_argument },
7643 unsigned int limit_mask = 0;
7644 bool use_default = false;
7645 int qtype, qctl_len;
7647 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7648 qctl = malloc(qctl_len);
7652 memset(qctl, 0, qctl_len);
7653 obd_type = (char *)qctl->obd_type;
7654 dqb = &qctl->qc_dqblk;
7656 if (has_times_option(argc, argv)) {
7657 rc = lfs_setquota_times(argc, argv, qctl);
7661 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7662 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7663 * so it can be used as a marker that qc_type
7664 * isn't reinitialized from command line
7667 while ((c = getopt_long(argc, argv, "b:B:dg:Ghi:I:p:Pu:U",
7668 long_opts, NULL)) != -1) {
7671 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7674 goto quota_type_def;
7677 rc = name2uid(&qctl->qc_id, optarg);
7680 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7683 goto quota_type_def;
7686 rc = name2gid(&qctl->qc_id, optarg);
7689 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7692 goto quota_type_def;
7695 rc = name2projid(&qctl->qc_id, optarg);
7698 if (str2quotaid(&qctl->qc_id, optarg)) {
7700 "%s setquota: invalid id '%s'\n",
7707 if (qctl->qc_id == 0) {
7709 "%s setquota: can't set quota for root usr/group/project.\n",
7716 if (qctl->qc_type != ALLQUOTA) {
7718 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7723 qctl->qc_type = qtype;
7726 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7730 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7731 dqb->dqb_bsoftlimit >>= 10;
7732 limit_mask |= BSLIMIT;
7733 if (dqb->dqb_bsoftlimit &&
7734 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7736 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7738 (unsigned long long)dqb->dqb_bsoftlimit,
7742 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7743 dqb->dqb_bhardlimit >>= 10;
7744 limit_mask |= BHLIMIT;
7745 if (dqb->dqb_bhardlimit &&
7746 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7748 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7749 "See '%s help setquota' or Lustre manual for details\n",
7751 (unsigned long long)dqb->dqb_bhardlimit,
7755 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7756 limit_mask |= ISLIMIT;
7757 if (dqb->dqb_isoftlimit &&
7758 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7760 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7762 (unsigned long long)dqb->dqb_isoftlimit,
7766 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7767 limit_mask |= IHLIMIT;
7768 if (dqb->dqb_ihardlimit &&
7769 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7771 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7773 (unsigned long long)dqb->dqb_ihardlimit,
7777 if (lfs_verify_poolarg(optarg)) {
7781 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7782 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
7783 LUSTRE_Q_SETDEFAULT_POOL :
7784 LUSTRE_Q_SETQUOTAPOOL;
7788 "%s setquota: unrecognized option '%s'\n",
7789 progname, argv[optind - 1]);
7797 if (qctl->qc_type == ALLQUOTA) {
7799 "%s setquota: either -u or -g must be specified\n",
7805 if (!use_default && limit_mask == 0) {
7807 "%s setquota: at least one limit must be specified\n",
7813 if (use_default && limit_mask != 0) {
7815 "%s setquota: limits should not be specified when using default quota\n",
7821 if (use_default && qctl->qc_id == 0) {
7823 "%s setquota: can not set default quota for root user/group/project\n",
7829 if (optind != argc - 1) {
7831 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7832 progname, argv[optind]);
7840 dqb->dqb_bhardlimit = 0;
7841 dqb->dqb_bsoftlimit = 0;
7842 dqb->dqb_ihardlimit = 0;
7843 dqb->dqb_isoftlimit = 0;
7846 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7847 /* do not set inode limits for Pool Quotas */
7848 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
7849 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
7850 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7851 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7852 /* sigh, we can't just set blimits/ilimits */
7853 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7854 .qc_type = qctl->qc_type,
7855 .qc_id = qctl->qc_id};
7857 rc = llapi_quotactl(mnt, &tmp_qctl);
7861 if (!(limit_mask & BHLIMIT))
7862 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7863 if (!(limit_mask & BSLIMIT))
7864 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7865 if (!(limit_mask & IHLIMIT))
7866 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7867 if (!(limit_mask & ISLIMIT))
7868 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7870 /* Keep grace times if we have got no softlimit arguments */
7871 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7872 dqb->dqb_valid |= QIF_BTIME;
7873 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7876 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7877 dqb->dqb_valid |= QIF_ITIME;
7878 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7882 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7883 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7885 rc = llapi_quotactl(mnt, qctl);
7889 "%s setquota: cannot quotactl '%s' '%s': %s\n",
7891 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7894 "%s setquota: quotactl failed: %s\n",
7895 progname, strerror(-rc));
7899 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7905 /* Converts seconds value into format string
7906 * result is returned in buf
7908 * 1. result is in descenting order: 1w2d3h4m5s
7909 * 2. zero fields are not filled (except for p. 3): 5d1s
7910 * 3. zero seconds value is presented as "0s"
7912 static char *__sec2str(time_t seconds, char *buf)
7914 const char spec[] = "smhdw";
7915 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7920 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7921 c = seconds / mult[i];
7923 if (c > 0 || (i == 0 && buf == tail))
7924 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7933 static void sec2str(time_t seconds, char *buf, int rc)
7940 tail = __sec2str(seconds, tail);
7942 if (rc && tail - buf < 39) {
7948 static void diff2str(time_t seconds, char *buf, time_t now)
7953 if (seconds <= now) {
7954 strcpy(buf, "none");
7957 __sec2str(seconds - now, buf);
7960 static void print_quota_title(char *name, struct if_quotactl *qctl,
7961 bool human_readable, bool show_default)
7964 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7965 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7966 "Filesystem", "bquota", "blimit", "bgrace",
7967 "iquota", "ilimit", "igrace");
7969 printf("Disk quotas for %s %s (%cid %u):\n",
7970 qtype_name(qctl->qc_type), name,
7971 *qtype_name(qctl->qc_type), qctl->qc_id);
7972 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7973 "Filesystem", human_readable ? "used" : "kbytes",
7974 "quota", "limit", "grace",
7975 "files", "quota", "limit", "grace");
7979 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7982 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7985 snprintf(buf, buflen, "%5.4gP",
7986 (double)num / ((__u64)1 << 40));
7988 snprintf(buf, buflen, "%5.4gT",
7989 (double)num / (1 << 30));
7991 snprintf(buf, buflen, "%5.4gG",
7992 (double)num / (1 << 20));
7994 snprintf(buf, buflen, "%5.4gM",
7995 (double)num / (1 << 10));
7997 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8001 #ifdef HAVE_NATIVE_CLIENT
8002 /* In the current Lustre implementation, the grace time is either the time
8003 * or the timestamp to be used after some quota ID exceeds the soft limt,
8004 * 48 bits should be enough, its high 16 bits can be used as quota flags.
8006 #define LQUOTA_GRACE_BITS 48
8007 #define LQUOTA_GRACE_MASK ((1ULL << LQUOTA_GRACE_BITS) - 1)
8008 #define LQUOTA_GRACE_MAX LQUOTA_GRACE_MASK
8009 #define LQUOTA_GRACE(t) (t & LQUOTA_GRACE_MASK)
8010 #define LQUOTA_FLAG(t) (t >> LQUOTA_GRACE_BITS)
8011 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8014 #define STRBUF_LEN 24
8015 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8016 int rc, bool h, bool show_default)
8022 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8023 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8024 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8025 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8026 int bover = 0, iover = 0;
8027 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8028 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8030 char strbuf[STRBUF_LEN];
8032 if (dqb->dqb_bhardlimit &&
8033 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8035 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8036 if (dqb->dqb_btime > now)
8042 if (dqb->dqb_ihardlimit &&
8043 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8045 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8046 if (dqb->dqb_itime > now)
8052 if (strlen(mnt) > 15)
8053 printf("%s\n%15s", mnt, "");
8055 printf("%15s", mnt);
8058 diff2str(dqb->dqb_btime, timebuf, now);
8059 else if (show_default)
8060 snprintf(timebuf, sizeof(timebuf), "%llu",
8061 (unsigned long long)dqb->dqb_btime);
8063 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8064 strbuf, sizeof(strbuf), h);
8065 if (rc == -EREMOTEIO)
8066 sprintf(numbuf[0], "%s*", strbuf);
8068 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8069 "%s" : "[%s]", strbuf);
8071 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8072 if (type == QC_GENERAL)
8073 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8074 "%s" : "[%s]", strbuf);
8076 sprintf(numbuf[1], "%s", "-");
8078 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8079 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8080 "%s" : "[%s]", strbuf);
8083 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8085 printf(" %7s%c %6s %7s %7s",
8086 numbuf[0], bover ? '*' : ' ', numbuf[1],
8087 numbuf[2], bover > 1 ? timebuf : "-");
8090 diff2str(dqb->dqb_itime, timebuf, now);
8091 else if (show_default)
8092 snprintf(timebuf, sizeof(timebuf), "%llu",
8093 (unsigned long long)dqb->dqb_itime);
8095 snprintf(numbuf[0], sizeof(numbuf),
8096 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8097 (uintmax_t)dqb->dqb_curinodes);
8099 if (type == QC_GENERAL)
8100 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8102 (uintmax_t)dqb->dqb_isoftlimit);
8104 sprintf(numbuf[1], "%s", "-");
8106 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8107 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8110 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8111 else if (type != QC_OSTIDX)
8112 printf(" %7s%c %6s %7s %7s",
8113 numbuf[0], iover ? '*' : ' ', numbuf[1],
8114 numbuf[2], iover > 1 ? timebuf : "-");
8116 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8118 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8119 qctl->qc_cmd == Q_GETOINFO) {
8123 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8124 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8126 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8127 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8128 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8130 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8132 printf("Block grace time: %s; Inode grace time: %s\n",
8133 bgtimebuf, igtimebuf);
8137 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8141 /* format is "lustre-OST0001" */
8142 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8144 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8149 *idx = strtoul(dash, &endp, 16);
8150 if (*idx > 0xffff) {
8151 fprintf(stderr, "wrong index %s\n", tgtname);
8158 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8159 bool h, __u64 *total)
8161 int rc = 0, rc1 = 0, count = 0, i = 0;
8162 char **list = NULL, *buffer = NULL;
8163 __u32 valid = qctl->qc_valid;
8165 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8168 /* Is it correct for the case OST0000, OST0002, OST0003 -
8169 * we will ask OST0001 that is absent and won't ask OST0003? */
8170 rc = llapi_get_obd_count(mnt, &count, is_mdt);
8172 fprintf(stderr, "can not get %s count: %s\n",
8173 is_mdt ? "mdt" : "ost", strerror(-rc));
8177 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8178 char fname[PATH_MAX];
8179 char fsname[LUSTRE_MAXFSNAME + 1];
8180 int bufsize = sizeof(struct obd_uuid) * count;
8182 rc = llapi_search_fsname(mnt, fsname);
8184 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8188 buffer = malloc(bufsize + sizeof(*list) * count);
8191 list = (char **)(buffer + bufsize);
8192 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8193 count = llapi_get_poolmembers(fname, list, count,
8199 for (i = 0; i < count; i++) {
8200 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8203 if (tgt_name2index(list[i], &index))
8205 qctl->qc_idx = index;
8210 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8211 rc = llapi_quotactl(mnt, qctl);
8213 /* It is remote client case. */
8214 if (rc == -EOPNOTSUPP) {
8221 fprintf(stderr, "quotactl %s%d failed.\n",
8222 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8226 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8227 qctl->qc_valid, 0, h, false);
8228 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8229 qctl->qc_dqblk.dqb_bhardlimit;
8234 qctl->qc_valid = valid;
8238 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8239 int verbose, int quiet, bool human_readable,
8242 int rc1 = 0, rc2 = 0, rc3 = 0;
8243 char *obd_type = (char *)qctl->obd_type;
8244 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8245 __u64 total_ialloc = 0, total_balloc = 0;
8246 bool use_default_for_blk = false;
8247 bool use_default_for_file = false;
8250 rc1 = llapi_quotactl(mnt, qctl);
8254 fprintf(stderr, "%s quotas are not enabled.\n",
8255 qtype_name(qctl->qc_type));
8258 fprintf(stderr, "Permission denied.\n");
8261 /* We already got error message. */
8264 fprintf(stderr, "Unexpected quotactl error: %s\n",
8269 if (!show_default && qctl->qc_id == 0) {
8270 qctl->qc_dqblk.dqb_bhardlimit = 0;
8271 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8272 qctl->qc_dqblk.dqb_ihardlimit = 0;
8273 qctl->qc_dqblk.dqb_isoftlimit = 0;
8274 qctl->qc_dqblk.dqb_btime = 0;
8275 qctl->qc_dqblk.dqb_itime = 0;
8276 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8279 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8280 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8281 use_default_for_blk = true;
8282 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8285 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8286 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8287 use_default_for_file = true;
8288 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8291 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8292 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8293 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8294 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8295 print_quota_title(name, qctl, human_readable, show_default);
8297 if (rc1 && *obd_type)
8298 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8300 if (qctl->qc_valid != QC_GENERAL)
8303 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8304 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8305 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8306 (QIF_LIMITS|QIF_USAGE));
8308 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8310 if (!show_default && verbose &&
8311 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8312 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8313 char strbuf[STRBUF_LEN];
8315 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8317 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8319 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8321 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8322 (uintmax_t)total_ialloc, strbuf);
8325 if (use_default_for_blk)
8326 printf("%cid %u is using default block quota setting\n",
8327 *qtype_name(qctl->qc_type), qctl->qc_id);
8329 if (use_default_for_file)
8330 printf("%cid %u is using default file quota setting\n",
8331 *qtype_name(qctl->qc_type), qctl->qc_id);
8333 if (rc1 || rc2 || rc3 || inacc)
8334 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8348 static int lfs_project(int argc, char **argv)
8350 int ret = 0, err = 0, c, i;
8351 struct project_handle_control phc = { 0 };
8352 enum lfs_project_ops_t op;
8355 phc.assign_projid = false;
8356 /* default action */
8357 op = LFS_PROJECT_LIST;
8359 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8362 if (op != LFS_PROJECT_LIST) {
8364 "%s: cannot specify '-c' '-C' '-s' together\n",
8369 op = LFS_PROJECT_CHECK;
8372 if (op != LFS_PROJECT_LIST) {
8374 "%s: cannot specify '-c' '-C' '-s' together\n",
8379 op = LFS_PROJECT_CLEAR;
8382 if (op != LFS_PROJECT_LIST) {
8384 "%s: cannot specify '-c' '-C' '-s' together\n",
8389 phc.set_inherit = true;
8390 op = LFS_PROJECT_SET;
8396 phc.keep_projid = true;
8399 phc.recursive = true;
8402 if (str2quotaid(&phc.projid, optarg)) {
8404 "Invalid project ID: %s\n",
8409 phc.assign_projid = true;
8413 phc.newline = false;
8416 fprintf(stderr, "%s: invalid option '%c'\n",
8422 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8423 op = LFS_PROJECT_SET;
8424 phc.set_projid = true;
8425 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8426 phc.set_projid = true;
8430 case LFS_PROJECT_CHECK:
8431 if (phc.keep_projid) {
8433 "%s: '-k' is useless together with '-c'\n",
8438 case LFS_PROJECT_CLEAR:
8441 "%s: '-0' is useless together with '-C'\n",
8445 if (phc.assign_projid) {
8447 "%s: '-p' is useless together with '-C'\n",
8452 case LFS_PROJECT_SET:
8455 "%s: '-0' is useless together with '-s'\n",
8459 if (phc.keep_projid) {
8461 "%s: '-k' is useless together with '-s'\n",
8469 "%s: '-0' is useless for list operations\n",
8479 fprintf(stderr, "%s: missing file or directory target(s)\n",
8484 for (i = 0; i < argc; i++) {
8486 case LFS_PROJECT_CHECK:
8487 err = lfs_project_check(argv[i], &phc);
8489 case LFS_PROJECT_LIST:
8490 err = lfs_project_list(argv[i], &phc);
8492 case LFS_PROJECT_CLEAR:
8493 err = lfs_project_clear(argv[i], &phc);
8495 case LFS_PROJECT_SET:
8496 err = lfs_project_set(argv[i], &phc);
8508 static int lfs_quota(int argc, char **argv)
8511 char *mnt, *name = NULL;
8512 struct if_quotactl *qctl;
8514 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8515 __u32 valid = QC_GENERAL, idx = 0;
8516 bool human_readable = false;
8517 bool show_default = false;
8519 bool show_pools = false;
8520 struct option long_opts[] = {
8521 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
8523 char **poollist = NULL;
8527 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8531 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8532 qctl->qc_type = ALLQUOTA;
8533 obd_uuid = (char *)qctl->obd_uuid.uuid;
8535 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8536 long_opts, NULL)) != -1) {
8539 show_default = true;
8544 show_default = true;
8549 show_default = true;
8553 if (qctl->qc_type != ALLQUOTA) {
8555 "%s quota: only one of -u, -g, or -p may be specified\n",
8560 qctl->qc_type = qtype;
8563 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8566 valid = qctl->qc_valid = QC_UUID;
8567 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8570 valid = qctl->qc_valid = QC_MDTIDX;
8571 idx = qctl->qc_idx = atoi(optarg);
8572 if (idx == 0 && *optarg != '0') {
8574 "%s quota: invalid MDT index '%s'\n",
8581 valid = qctl->qc_valid = QC_OSTIDX;
8582 idx = qctl->qc_idx = atoi(optarg);
8583 if (idx == 0 && *optarg != '0') {
8585 "%s quota: invalid OST index '%s'\n",
8598 human_readable = true;
8601 if ((!optarg) && (argv[optind] != NULL) &&
8602 (argv[optind][0] != '-') &&
8603 (argv[optind][0] != '/')) {
8604 optarg = argv[optind++];
8605 if (lfs_verify_poolarg(optarg)) {
8609 strncpy(qctl->qc_poolname, optarg,
8611 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
8612 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
8614 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8618 /* optarg is NULL */
8620 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8623 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8624 progname, argv[optind - 1]);
8630 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8631 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8632 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8633 qctl->qc_type == ALLQUOTA &&
8634 optind == argc - 1 && !show_default) {
8637 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8638 qctl->qc_type = qtype;
8639 qctl->qc_valid = valid;
8640 if (qtype == USRQUOTA) {
8641 qctl->qc_id = geteuid();
8642 rc = uid2name(&name, qctl->qc_id);
8644 qctl->qc_id = getegid();
8645 rc = gid2name(&name, qctl->qc_id);
8646 memset(&qctl->qc_dqblk, 0,
8647 sizeof(qctl->qc_dqblk));
8652 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8653 human_readable, show_default);
8658 /* lfs quota -u username /path/to/lustre/mount */
8659 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8660 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8661 /* options should be followed by u/g-name and mntpoint */
8662 if ((!show_default && optind + 2 != argc) ||
8663 (show_default && optind + 1 != argc) ||
8664 qctl->qc_type == ALLQUOTA) {
8666 "%s quota: name and mount point must be specified\n",
8672 if (!show_default) {
8673 name = argv[optind++];
8674 switch (qctl->qc_type) {
8676 rc = name2uid(&qctl->qc_id, name);
8679 rc = name2gid(&qctl->qc_id, name);
8682 rc = name2projid(&qctl->qc_id, name);
8689 qctl->qc_valid = QC_GENERAL;
8690 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8691 LUSTRE_Q_GETDEFAULT_POOL :
8692 LUSTRE_Q_GETDEFAULT;
8697 if (str2quotaid(&qctl->qc_id, name)) {
8698 fprintf(stderr, "%s quota: invalid id '%s'\n",
8704 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8705 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8716 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
8720 for (i = 0; i < poolcount; i++) {
8721 p = memchr(poollist[i], '.', MAXNAMLEN);
8723 fprintf(stderr, "bad string format %.*s\n",
8724 MAXNAMLEN, poollist[i]);
8729 printf("Quotas for pool: %s\n", p);
8730 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
8731 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8732 human_readable, show_default);
8739 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8740 human_readable, show_default);
8746 #endif /* HAVE_SYS_QUOTA_H! */
8748 static int flushctx_ioctl(char *mp)
8752 fd = open(mp, O_RDONLY);
8754 fprintf(stderr, "flushctx: error open %s: %s\n",
8755 mp, strerror(errno));
8759 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8761 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8762 mp, strerror(errno));
8768 static int lfs_flushctx(int argc, char **argv)
8770 int kdestroy = 0, reap = 0, c;
8771 char mntdir[PATH_MAX] = {'\0'};
8775 while ((c = getopt(argc, argv, "kr")) != -1) {
8785 "error: %s: option '-%c' unrecognized\n",
8792 rc = system("kdestroy > /dev/null");
8794 rc = WEXITSTATUS(rc);
8796 "error destroying tickets: %d, continuing\n",
8801 if (optind >= argc) {
8802 /* flush for all mounted lustre fs. */
8803 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8804 /* Check if we have a mount point */
8805 if (mntdir[0] == '\0')
8808 if (flushctx_ioctl(mntdir))
8811 mntdir[0] = '\0'; /* avoid matching in next loop */
8814 /* flush fs as specified */
8815 while (optind < argc) {
8816 if (flushctx_ioctl(argv[optind++]))
8822 rc = system("keyctl reap > /dev/null");
8824 rc = WEXITSTATUS(rc);
8825 fprintf(stderr, "error reaping keyring: %d\n", rc);
8832 static int lfs_changelog(int argc, char **argv)
8834 void *changelog_priv;
8835 struct changelog_rec *rec;
8836 long long startrec = 0, endrec = 0;
8838 struct option long_opts[] = {
8839 { .val = 'f', .name = "follow", .has_arg = no_argument },
8841 char short_opts[] = "f";
8844 while ((rc = getopt_long(argc, argv, short_opts,
8845 long_opts, NULL)) != -1) {
8852 "%s changelog: unrecognized option '%s'\n",
8853 progname, argv[optind - 1]);
8857 if (optind >= argc) {
8858 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8863 mdd = argv[optind++];
8864 if (argc > optind) {
8866 startrec = strtoll(argv[optind++], NULL, 10);
8867 if (errno != 0 || startrec < 0) {
8869 "%s changelog: bad startrec\n",
8875 if (argc > optind) {
8877 endrec = strtoll(argv[optind++], NULL, 10);
8878 if (errno != 0 || endrec < 0) {
8880 "%s changelog: bad endrec\n",
8886 rc = llapi_changelog_start(&changelog_priv,
8887 CHANGELOG_FLAG_BLOCK |
8888 CHANGELOG_FLAG_JOBID |
8889 CHANGELOG_FLAG_EXTRA_FLAGS |
8890 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8893 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8894 progname, strerror(errno = -rc));
8898 rc = llapi_changelog_set_xflags(changelog_priv,
8899 CHANGELOG_EXTRA_FLAG_UIDGID |
8900 CHANGELOG_EXTRA_FLAG_NID |
8901 CHANGELOG_EXTRA_FLAG_OMODE |
8902 CHANGELOG_EXTRA_FLAG_XATTR);
8905 "%s changelog: cannot set xflags for changelog: %s\n",
8906 progname, strerror(errno = -rc));
8910 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8914 if (endrec && rec->cr_index > endrec) {
8915 llapi_changelog_free(&rec);
8918 if (rec->cr_index < startrec) {
8919 llapi_changelog_free(&rec);
8923 secs = rec->cr_time >> 30;
8924 gmtime_r(&secs, &ts);
8925 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8926 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8927 changelog_type2str(rec->cr_type),
8928 ts.tm_hour, ts.tm_min, ts.tm_sec,
8929 (int)(rec->cr_time & ((1 << 30) - 1)),
8930 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8931 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8933 if (rec->cr_flags & CLF_JOBID) {
8934 struct changelog_ext_jobid *jid =
8935 changelog_rec_jobid(rec);
8937 if (jid->cr_jobid[0] != '\0')
8938 printf(" j=%s", jid->cr_jobid);
8941 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8942 struct changelog_ext_extra_flags *ef =
8943 changelog_rec_extra_flags(rec);
8945 printf(" ef=0x%llx",
8946 (unsigned long long)ef->cr_extra_flags);
8948 if (ef->cr_extra_flags & CLFE_UIDGID) {
8949 struct changelog_ext_uidgid *uidgid =
8950 changelog_rec_uidgid(rec);
8952 printf(" u=%llu:%llu",
8953 (unsigned long long)uidgid->cr_uid,
8954 (unsigned long long)uidgid->cr_gid);
8956 if (ef->cr_extra_flags & CLFE_NID) {
8957 struct changelog_ext_nid *nid =
8958 changelog_rec_nid(rec);
8961 libcfs_nid2str(nid->cr_nid));
8964 if (ef->cr_extra_flags & CLFE_OPEN) {
8965 struct changelog_ext_openmode *omd =
8966 changelog_rec_openmode(rec);
8967 char mode[] = "---";
8969 /* exec mode must be exclusive */
8970 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8973 if (omd->cr_openflags & MDS_FMODE_READ)
8975 if (omd->cr_openflags &
8982 if (strcmp(mode, "---") != 0)
8983 printf(" m=%s", mode);
8986 if (ef->cr_extra_flags & CLFE_XATTR) {
8987 struct changelog_ext_xattr *xattr =
8988 changelog_rec_xattr(rec);
8990 if (xattr->cr_xattr[0] != '\0')
8991 printf(" x=%s", xattr->cr_xattr);
8995 if (!fid_is_zero(&rec->cr_pfid))
8996 printf(" p="DFID, PFID(&rec->cr_pfid));
8997 if (rec->cr_namelen)
8998 printf(" %.*s", rec->cr_namelen,
8999 changelog_rec_name(rec));
9001 if (rec->cr_flags & CLF_RENAME) {
9002 struct changelog_ext_rename *rnm =
9003 changelog_rec_rename(rec);
9005 if (!fid_is_zero(&rnm->cr_sfid))
9006 printf(" s="DFID" sp="DFID" %.*s",
9007 PFID(&rnm->cr_sfid),
9008 PFID(&rnm->cr_spfid),
9009 (int)changelog_rec_snamelen(rec),
9010 changelog_rec_sname(rec));
9014 llapi_changelog_free(&rec);
9017 llapi_changelog_fini(&changelog_priv);
9020 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9021 progname, strerror(errno = -rc));
9023 return (rc == 1 ? 0 : rc);
9026 static int lfs_changelog_clear(int argc, char **argv)
9035 endrec = strtoll(argv[3], NULL, 10);
9036 if (errno != 0 || endrec < 0) {
9038 "%s: bad endrec '%s'\n",
9043 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9046 fprintf(stderr, "%s: record out of range: %llu\n",
9048 else if (rc == -ENOENT)
9049 fprintf(stderr, "%s: no changelog user: %s\n",
9052 fprintf(stderr, "%s error: %s\n", argv[0],
9061 static void rstripc(char *str, int c)
9063 char *end = str + strlen(str);
9065 for (; str < end && end[-1] == c; --end)
9069 static int lfs_fid2path(int argc, char **argv)
9071 struct option long_opts[] = {
9072 { .val = 'c', .name = "cur", .has_arg = no_argument },
9073 { .val = 'c', .name = "current", .has_arg = no_argument },
9074 { .val = 'c', .name = "print-link", .has_arg = no_argument },
9075 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
9076 { .val = 'l', .name = "link", .has_arg = required_argument },
9078 char short_opts[] = "cfl:pr:";
9079 bool print_link = false;
9080 bool print_fid = false;
9082 char mnt_dir[PATH_MAX] = "";
9084 char *path_or_fsname;
9085 long long recno = -1;
9087 char *endptr = NULL;
9092 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9102 linkno = strtol(optarg, &endptr, 10);
9103 if (errno != 0 || *endptr != '\0' || linkno < 0) {
9105 "%s fid2path: invalid linkno '%s'\n",
9111 /* recno is something to do with changelogs
9112 * that was never implemented. We just pass it
9113 * through for the MDT to ignore.
9116 recno = strtoll(optarg, &endptr, 10);
9117 if (errno != 0 || *endptr != '\0' || recno < 0) {
9119 "%s fid2path: invalid recno '%s'\n",
9126 "%s fid2path: unrecognized option '%s'\n",
9127 progname, argv[optind - 1]);
9132 if (argc - optind < 2) {
9134 "Usage: %s fid2path FSNAME|ROOT FID...\n",
9139 path_or_fsname = argv[optind];
9141 if (*path_or_fsname == '/') {
9142 print_mnt_dir = true;
9143 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9145 print_mnt_dir = false;
9146 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9151 "%s fid2path: cannot resolve mount point for '%s': %s\n",
9152 progname, path_or_fsname, strerror(-rc));
9156 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9159 "%s fid2path: cannot open mount point for '%s': %s\n",
9160 progname, path_or_fsname, strerror(-rc));
9164 /* Strip trailing slashes from mnt_dir. */
9165 rstripc(mnt_dir + 1, '/');
9167 for (i = optind + 1; i < argc; i++) {
9168 const char *fid_str = argv[i];
9172 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9175 "%s fid2path: invalid FID '%s'\n",
9183 int linktmp = (linkno >= 0) ? linkno : 0;
9185 int oldtmp = linktmp;
9186 long long rectmp = recno;
9187 char path_buf[PATH_MAX];
9189 rc2 = llapi_fid2path_at(mnt_fd, &fid,
9190 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9193 "%s fid2path: cannot find %s %s: %s\n",
9194 progname, path_or_fsname, fid_str,
9202 printf("%s ", fid_str);
9205 printf("%d ", linktmp);
9207 /* You may think this looks wrong or weird (and it is!)
9208 * but we are actually trying to preserve the old quirky
9209 * behaviors (enforced by our old quirky tests!) that
9210 * make lfs so much fun to work on:
9212 * lustre 0x200000007:0x1:0x0 => "/"
9213 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9215 * Note that llapi_fid2path() returns "" for the root
9219 print_mnt_dir ? mnt_dir : "",
9220 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9224 /* specified linkno */
9227 if (oldtmp == linktmp)
9239 static int lfs_path2fid(int argc, char **argv)
9241 struct option long_opts[] = {
9242 { .val = 'p', .name = "parents", .has_arg = no_argument },
9245 const char short_opts[] = "p";
9246 const char *sep = "";
9249 bool show_parents = false;
9251 while ((rc = getopt_long(argc, argv, short_opts,
9252 long_opts, NULL)) != -1) {
9255 show_parents = true;
9259 "%s path2fid: unrecognized option '%s'\n",
9260 progname, argv[optind - 1]);
9265 if (optind > argc - 1) {
9266 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9269 } else if (optind < argc - 1) {
9274 for (path = argv + optind; *path != NULL; path++) {
9277 if (!show_parents) {
9278 err = llapi_path2fid(*path, &fid);
9280 printf("%s%s"DFID"\n",
9281 *sep != '\0' ? *path : "", sep,
9284 char name[NAME_MAX + 1];
9285 unsigned int linkno = 0;
9287 while ((err = llapi_path2parent(*path, linkno, &fid,
9288 name, sizeof(name))) == 0) {
9289 if (*sep != '\0' && linkno == 0)
9290 printf("%s%s", *path, sep);
9292 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9297 /* err == -ENODATA is end-of-loop */
9298 if (linkno > 0 && err == -ENODATA) {
9306 "%s path2fid: cannot get %sfid for '%s': %s\n",
9307 progname, show_parents ? "parent " : "", *path,
9319 #define MAX_ERRNO 4095
9320 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9322 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9326 rc = llapi_rmfid(device, fa);
9328 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9329 progname, strerror(-rc));
9333 for (k = 0; k < fa->fa_nr; k++) {
9334 rc2 = (__s32)fa->fa_fids[k].f_ver;
9335 if (!IS_ERR_VALUE(rc2))
9341 fa->fa_fids[k].f_ver = 0;
9342 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9343 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9349 static int lfs_rmfid(int argc, char **argv)
9351 char *fidstr, *device;
9352 int rc = 0, rc2, nr;
9353 struct fid_array *fa;
9355 if (optind > argc - 1) {
9356 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9360 device = argv[optind++];
9363 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9369 while (optind < argc) {
9372 fidstr = argv[optind++];
9373 while (*fidstr == '[')
9375 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9377 fprintf(stderr, "unrecognized FID: %s\n",
9382 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9383 /* start another batch */
9384 rc2 = lfs_rmfid_and_show_errors(device, fa);
9391 rc2 = lfs_rmfid_and_show_errors(device, fa);
9399 static int lfs_data_version(int argc, char **argv)
9401 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9409 fprintf(stderr, "%s: FILE must be specified\n",
9414 while ((c = getopt(argc, argv, "hnrw")) != -1) {
9417 data_version_flags = 0;
9420 data_version_flags |= LL_DV_RD_FLUSH;
9423 data_version_flags |= LL_DV_WR_FLUSH;
9427 "%s data_version: unrecognized option '%s'\n",
9428 progname, argv[optind - 1]);
9434 if (optind == argc) {
9435 fprintf(stderr, "%s data_version: FILE must be specified\n",
9440 path = argv[optind];
9441 fd = open(path, O_RDONLY);
9444 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9445 progname, path, strerror(-rc));
9449 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9452 "%s data_version: cannot get version for '%s': %s\n",
9453 progname, path, strerror(-rc));
9455 printf("%ju" "\n", (uintmax_t)data_version);
9461 static int lfs_hsm_state(int argc, char **argv)
9466 struct hsm_user_state hus;
9474 rc = llapi_hsm_state_get(path, &hus);
9476 fprintf(stderr, "can't get hsm state for %s: %s\n",
9477 path, strerror(errno = -rc));
9481 /* Display path name and status flags */
9482 printf("%s: (0x%08x)", path, hus.hus_states);
9484 if (hus.hus_states & HS_RELEASED)
9485 printf(" released");
9486 if (hus.hus_states & HS_EXISTS)
9488 if (hus.hus_states & HS_DIRTY)
9490 if (hus.hus_states & HS_ARCHIVED)
9491 printf(" archived");
9492 /* Display user-settable flags */
9493 if (hus.hus_states & HS_NORELEASE)
9494 printf(" never_release");
9495 if (hus.hus_states & HS_NOARCHIVE)
9496 printf(" never_archive");
9497 if (hus.hus_states & HS_LOST)
9498 printf(" lost_from_hsm");
9500 if (hus.hus_archive_id != 0)
9501 printf(", archive_id:%d", hus.hus_archive_id);
9504 } while (++i < argc);
9509 #define LFS_HSM_SET 0
9510 #define LFS_HSM_CLEAR 1
9513 * Generic function to set or clear HSM flags.
9514 * Used by hsm_set and hsm_clear.
9516 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9518 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9520 struct option long_opts[] = {
9521 { .val = 'A', .name = "archived", .has_arg = no_argument },
9522 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
9523 { .val = 'd', .name = "dirty", .has_arg = no_argument },
9524 { .val = 'e', .name = "exists", .has_arg = no_argument },
9525 { .val = 'h', .name = "help", .has_arg = no_argument },
9526 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
9527 { .val = 'l', .name = "lost", .has_arg = no_argument },
9528 { .val = 'r', .name = "norelease", .has_arg = no_argument },
9533 __u32 archive_id = 0;
9539 while ((c = getopt_long(argc, argv, "aAdehi:lr",
9540 long_opts, NULL)) != -1) {
9546 mask |= HS_NOARCHIVE;
9549 mask |= HS_ARCHIVED;
9552 mask |= HS_NORELEASE;
9562 archive_id = strtol(optarg, &end, 10);
9563 if (errno != 0 || *end != '\0' || archive_id < 0) {
9565 "%s: invalid archive_id: '%s'\n",
9571 fprintf(stderr, "%s: unrecognized option '%s'\n",
9572 progname, argv[optind - 1]);
9579 /* User should have specified a flag */
9583 while (optind < argc) {
9584 path = argv[optind];
9586 /* If mode == 0, this means we apply the mask. */
9587 if (mode == LFS_HSM_SET)
9588 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9590 rc = llapi_hsm_state_set(path, 0, mask, 0);
9593 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9594 path, strerror(errno = -rc));
9603 static int lfs_hsm_action(int argc, char **argv)
9608 struct hsm_current_action hca;
9609 struct hsm_extent he;
9610 enum hsm_user_action hua;
9611 enum hsm_progress_states hps;
9619 rc = llapi_hsm_current_action(path, &hca);
9621 fprintf(stderr, "can't get hsm action for %s: %s\n",
9622 path, strerror(errno = -rc));
9625 he = hca.hca_location;
9626 hua = hca.hca_action;
9627 hps = hca.hca_state;
9629 printf("%s: %s", path, hsm_user_action2name(hua));
9631 /* Skip file without action */
9632 if (hca.hca_action == HUA_NONE) {
9637 printf(" %s ", hsm_progress_state2name(hps));
9639 if ((hps == HPS_RUNNING) &&
9640 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9641 printf("(%llu bytes moved)\n",
9642 (unsigned long long)he.length);
9643 else if ((he.offset + he.length) == LUSTRE_EOF)
9644 printf("(from %llu to EOF)\n",
9645 (unsigned long long)he.offset);
9647 printf("(from %llu to %llu)\n",
9648 (unsigned long long)he.offset,
9649 (unsigned long long)(he.offset + he.length));
9651 } while (++i < argc);
9656 static int lfs_hsm_set(int argc, char **argv)
9658 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9661 static int lfs_hsm_clear(int argc, char **argv)
9663 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9667 * Check file state and return its fid, to be used by lfs_hsm_request().
9669 * \param[in] file Path to file to check
9670 * \param[in,out] fid Pointer to allocated lu_fid struct.
9671 * \param[in,out] last_dev Pointer to last device id used.
9673 * \return 0 on success.
9675 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9681 rc = lstat(file, &st);
9683 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9687 * Checking for regular file as archiving as posix copytool
9688 * rejects archiving files other than regular files
9690 if (!S_ISREG(st.st_mode)) {
9691 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9694 /* A request should be ... */
9695 if (*last_dev != st.st_dev && *last_dev != 0) {
9697 "All files should be on the same filesystem: %s\n",
9701 *last_dev = st.st_dev;
9703 rc = llapi_path2fid(file, fid);
9705 fprintf(stderr, "Cannot read FID of %s: %s\n",
9706 file, strerror(-rc));
9712 /* Fill an HSM HUR item with a given file name.
9714 * If mntpath is set, then the filename is actually a FID, and no
9715 * lookup on the filesystem will be performed.
9717 * \param[in] hur the user request to fill
9718 * \param[in] idx index of the item inside the HUR to fill
9719 * \param[in] mntpath mountpoint of Lustre
9720 * \param[in] fname filename (if mtnpath is NULL)
9721 * or FID (if mntpath is set)
9722 * \param[in] last_dev pointer to last device id used
9724 * \retval 0 on success
9725 * \retval CMD_HELP or a negative errno on error
9727 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9728 const char *mntpath, const char *fname,
9731 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9734 hui->hui_extent.length = -1;
9737 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9739 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9742 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9746 hur->hur_request.hr_itemcount++;
9751 static int lfs_hsm_request(int argc, char **argv, int action)
9753 struct option long_opts[] = {
9754 { .val = 'a', .name = "archive", .has_arg = required_argument },
9755 { .val = 'D', .name = "data", .has_arg = required_argument },
9756 { .val = 'h', .name = "help", .has_arg = no_argument },
9757 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9758 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9761 struct hsm_user_request *hur, *oldhur;
9766 char *filelist = NULL;
9767 char fullpath[PATH_MAX];
9768 char *opaque = NULL;
9772 int nbfile_alloc = 0;
9773 char *some_file = NULL;
9774 char *mntpath = NULL;
9780 while ((c = getopt_long(argc, argv, "a:D:hl:m:",
9781 long_opts, NULL)) != -1) {
9790 if (action != HUA_ARCHIVE &&
9791 action != HUA_REMOVE) {
9793 "error: -a is supported only when archiving or removing\n");
9796 archive_id = atoi(optarg);
9801 some_file = strdup(optarg);
9805 fprintf(stderr, "%s: unrecognized option '%s'\n",
9806 progname, argv[optind - 1]);
9813 /* All remaining args are files, so we have at least nbfile */
9814 nbfile = argc - optind;
9816 if ((nbfile == 0) && (!filelist))
9820 opaque_len = strlen(opaque);
9823 * Alloc the request structure with enough place to store all files
9824 * from command line.
9826 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9828 fprintf(stderr, "Cannot create the request: %s\n",
9832 nbfile_alloc = nbfile;
9834 hur->hur_request.hr_action = action;
9835 hur->hur_request.hr_archive_id = archive_id;
9836 hur->hur_request.hr_flags = 0;
9838 /* All remaining args are files, add them */
9839 if (nbfile != 0 && some_file == NULL)
9840 some_file = strdup(argv[optind]);
9842 for (i = 0; i < nbfile; i++) {
9843 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9849 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9851 /* If a filelist was specified, read the filelist from it. */
9853 fp = fopen(filelist, "r");
9855 fprintf(stderr, "Cannot read the file list %s: %s\n",
9856 filelist, strerror(errno));
9861 while ((rc = getline(&line, &len, fp)) != -1) {
9863 * If allocated buffer was too small, get something
9866 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9869 nbfile_alloc = nbfile_alloc * 2 + 1;
9871 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9875 "hsm: cannot allocate the request: %s\n",
9882 size = hur_len(oldhur);
9885 "hsm: cannot allocate %u files + %u bytes data\n",
9886 oldhur->hur_request.hr_itemcount,
9887 oldhur->hur_request.hr_data_len);
9894 memcpy(hur, oldhur, size);
9899 if (line[strlen(line) - 1] == '\n')
9900 line[strlen(line) - 1] = '\0';
9902 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9903 mntpath, line, &last_dev);
9919 /* If a --data was used, add it to the request */
9920 hur->hur_request.hr_data_len = opaque_len;
9922 memcpy(hur_data(hur), opaque, opaque_len);
9924 /* Send the HSM request */
9925 if (realpath(some_file, fullpath) == NULL) {
9926 fprintf(stderr, "Could not find path '%s': %s\n",
9927 some_file, strerror(errno));
9929 rc = llapi_hsm_request(fullpath, hur);
9931 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9932 some_file, strerror(-rc));
9942 static int lfs_hsm_archive(int argc, char **argv)
9944 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9947 static int lfs_hsm_restore(int argc, char **argv)
9949 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9952 static int lfs_hsm_release(int argc, char **argv)
9954 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9957 static int lfs_hsm_remove(int argc, char **argv)
9959 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9962 static int lfs_hsm_cancel(int argc, char **argv)
9964 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9967 static int lfs_swap_layouts(int argc, char **argv)
9972 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9973 SWAP_LAYOUTS_KEEP_MTIME |
9974 SWAP_LAYOUTS_KEEP_ATIME);
9977 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9979 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9981 int lfs_get_mode(const char *string)
9983 enum lock_mode_user mode;
9985 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9986 if (lock_mode_names[mode] == NULL)
9988 if (strcasecmp(string, lock_mode_names[mode]) == 0)
9995 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9997 enum lu_ladvise_type advice;
10000 advice < ARRAY_SIZE(ladvise_names); advice++) {
10001 if (ladvise_names[advice] == NULL)
10003 if (strcmp(string, ladvise_names[advice]) == 0)
10007 return LU_LADVISE_INVALID;
10010 static int lfs_ladvise(int argc, char **argv)
10012 struct option long_opts[] = {
10013 { .val = 'a', .name = "advice", .has_arg = required_argument },
10014 { .val = 'b', .name = "background", .has_arg = no_argument },
10015 { .val = 'e', .name = "end", .has_arg = required_argument },
10016 { .val = 'h', .name = "help", .has_arg = no_argument },
10017 { .val = 'l', .name = "length", .has_arg = required_argument },
10018 { .val = 'm', .name = "mode", .has_arg = required_argument },
10019 { .val = 's', .name = "start", .has_arg = required_argument },
10020 { .val = 'u', .name = "unset", .has_arg = no_argument },
10021 { .name = NULL } };
10022 struct llapi_lu_ladvise advice;
10023 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10024 unsigned long long start = 0;
10025 unsigned long long end = LUSTRE_EOF;
10026 unsigned long long length = 0;
10027 unsigned long long size_units;
10028 unsigned long long flags = 0;
10034 while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10035 long_opts, NULL)) != -1) {
10038 advice_type = lfs_get_ladvice(optarg);
10039 if (advice_type == LU_LADVISE_INVALID) {
10041 "%s: invalid advice type '%s'\n",
10043 fprintf(stderr, "Valid types:");
10045 for (advice_type = 0;
10046 advice_type < ARRAY_SIZE(ladvise_names);
10048 if (ladvise_names[advice_type] == NULL)
10050 fprintf(stderr, " %s",
10051 ladvise_names[advice_type]);
10053 fprintf(stderr, "\n");
10066 rc = llapi_parse_size(optarg, &end,
10069 fprintf(stderr, "%s: bad end offset '%s'\n",
10076 rc = llapi_parse_size(optarg, &start,
10080 "%s: bad start offset '%s'\n",
10087 rc = llapi_parse_size(optarg, &length,
10090 fprintf(stderr, "%s: bad length '%s'\n",
10096 mode = lfs_get_mode(optarg);
10099 "%s: bad mode '%s', valid modes are READ or WRITE\n",
10105 fprintf(stderr, "%s: unrecognized option '%s'\n",
10106 progname, argv[optind - 1]);
10113 if (advice_type == LU_LADVISE_INVALID) {
10114 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10115 fprintf(stderr, "Valid types:");
10116 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10118 if (ladvise_names[advice_type] == NULL)
10120 fprintf(stderr, " %s", ladvise_names[advice_type]);
10122 fprintf(stderr, "\n");
10126 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10128 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10133 if (argc <= optind) {
10134 fprintf(stderr, "%s: please give one or more file names\n",
10139 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10140 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10145 if (end == LUSTRE_EOF && length != 0)
10146 end = start + length;
10148 if (end <= start) {
10149 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10150 argv[0], start, end);
10154 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10155 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10160 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10161 fprintf(stderr, "%s: mode is required with lockahead\n",
10166 while (optind < argc) {
10169 path = argv[optind++];
10171 fd = open(path, O_RDONLY);
10173 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10174 argv[0], path, strerror(errno));
10179 advice.lla_start = start;
10180 advice.lla_end = end;
10181 advice.lla_advice = advice_type;
10182 advice.lla_value1 = 0;
10183 advice.lla_value2 = 0;
10184 advice.lla_value3 = 0;
10185 advice.lla_value4 = 0;
10186 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10187 advice.lla_lockahead_mode = mode;
10188 advice.lla_peradvice_flags = flags;
10191 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10195 "%s: cannot give advice '%s' to file '%s': %s\n",
10196 argv[0], ladvise_names[advice_type],
10197 path, strerror(errno));
10203 if (rc == 0 && rc2 < 0)
10209 static const char *const heat_names[] = LU_HEAT_NAMES;
10211 static int lfs_heat_get(int argc, char **argv)
10213 struct lu_heat *heat;
10222 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10224 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10229 while (optind < argc) {
10230 path = argv[optind++];
10232 fd = open(path, O_RDONLY);
10234 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10235 argv[0], path, strerror(errno));
10240 heat->lh_count = OBD_HEAT_COUNT;
10241 rc2 = llapi_heat_get(fd, heat);
10245 "%s: cannot get heat of file '%s': %s\n",
10246 argv[0], path, strerror(errno));
10250 printf("flags: %x\n", heat->lh_flags);
10251 for (i = 0; i < heat->lh_count; i++)
10252 printf("%s: %llu\n", heat_names[i],
10253 (unsigned long long)heat->lh_heat[i]);
10255 if (rc == 0 && rc2 < 0)
10263 static int lfs_heat_set(int argc, char **argv)
10265 struct option long_opts[] = {
10266 { .val = 'c', .name = "clear", .has_arg = no_argument },
10267 { .val = 'h', .name = "help", .has_arg = no_argument },
10268 { .val = 'o', .name = "off", .has_arg = no_argument },
10269 { .val = 'O', .name = "on", .has_arg = no_argument },
10270 { .name = NULL } };
10271 enum lu_heat_flag flags = 0;
10281 while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
10284 flags |= LU_HEAT_FLAG_CLEAR;
10287 flags |= LU_HEAT_FLAG_CLEAR;
10288 flags |= LU_HEAT_FLAG_OFF;
10291 flags &= ~LU_HEAT_FLAG_OFF;
10294 fprintf(stderr, "%s: unrecognized option '%s'\n",
10295 progname, argv[optind - 1]);
10302 if (argc <= optind) {
10303 fprintf(stderr, "%s: please give one or more file names\n",
10308 while (optind < argc) {
10309 path = argv[optind++];
10311 fd = open(path, O_RDONLY);
10313 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10314 argv[0], path, strerror(errno));
10319 rc2 = llapi_heat_set(fd, flags);
10323 "%s: cannot setflags heat of file '%s': %s\n",
10324 argv[0], path, strerror(errno));
10328 if (rc == 0 && rc2 < 0)
10335 * The input string contains a comma delimited list of component ids and
10336 * ranges, for example "1,2-4,7".
10338 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10340 bool end_of_loop = false;
10348 while (!end_of_loop) {
10352 char *endptr = NULL;
10355 ptr = strchrnul(arg, ',');
10356 end_of_loop = *ptr == '\0';
10359 start_index = strtol(arg, &endptr, 0);
10360 if (endptr == arg) /* no data at all */
10362 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10364 if (start_index < 0)
10367 end_index = start_index;
10368 if (*endptr == '-') {
10369 end_index = strtol(endptr + 1, &endptr, 0);
10370 if (*endptr != '\0')
10372 if (end_index < start_index)
10376 for (i = start_index; i <= end_index && size > 0; i++) {
10379 /* remove duplicate */
10380 for (j = 0; j < nr; j++) {
10384 if (j == nr) { /* no duplicate */
10390 if (size == 0 && i < end_index)
10397 if (!end_of_loop && ptr)
10400 return rc < 0 ? rc : nr;
10404 * struct verify_mirror_id - Mirror id to be verified.
10405 * @mirror_id: A specified mirror id.
10406 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10408 struct verify_mirror_id {
10414 * compare_mirror_ids() - Compare mirror ids.
10415 * @layout: Mirror component list.
10416 * @cbdata: Callback data in verify_mirror_id structure.
10418 * This is a callback function called by llapi_layout_comp_iterate()
10419 * to compare the specified mirror id with the one in the current
10420 * component of @layout. If they are the same, then the specified
10421 * mirror id is valid.
10423 * Return: a negative error code on failure or
10424 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10425 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
10428 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10430 struct verify_mirror_id *mirror_id_cbdata =
10431 (struct verify_mirror_id *)cbdata;
10432 uint32_t mirror_id;
10435 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10439 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10440 progname, strerror(errno));
10444 if (mirror_id_cbdata->mirror_id == mirror_id) {
10445 mirror_id_cbdata->is_valid_id = true;
10446 return LLAPI_LAYOUT_ITER_STOP;
10449 return LLAPI_LAYOUT_ITER_CONT;
10453 * verify_mirror_ids() - Verify specified mirror ids.
10454 * @fname: Mirrored file name.
10455 * @mirror_ids: Specified mirror ids to be verified.
10456 * @ids_nr: Number of specified mirror ids.
10458 * This function verifies that specified @mirror_ids are valid
10459 * in the mirrored file @fname.
10461 * Return: 0 on success or a negative error code on failure.
10464 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10466 struct llapi_layout *layout = NULL;
10467 struct verify_mirror_id mirror_id_cbdata = { 0 };
10469 uint32_t flr_state;
10478 if (stat(fname, &stbuf) < 0) {
10479 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10480 progname, fname, strerror(errno));
10485 if (!S_ISREG(stbuf.st_mode)) {
10486 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10492 fd = open(fname, O_DIRECT | O_RDONLY);
10494 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10495 progname, fname, strerror(errno));
10500 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10502 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10503 progname, fname, strerror(errno));
10507 layout = llapi_layout_get_by_fd(fd, 0);
10509 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10510 progname, fname, strerror(errno));
10512 llapi_lease_release(fd);
10516 rc = llapi_layout_flags_get(layout, &flr_state);
10518 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10519 progname, fname, strerror(errno));
10524 flr_state &= LCM_FL_FLR_MASK;
10525 switch (flr_state) {
10528 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10529 progname, fname, llapi_layout_flags_string(flr_state));
10536 for (i = 0; i < ids_nr; i++) {
10537 mirror_id_cbdata.mirror_id = mirror_ids[i];
10538 mirror_id_cbdata.is_valid_id = false;
10540 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10541 &mirror_id_cbdata);
10545 "%s: '%s' failed to verify mirror id: %u.\n",
10546 progname, fname, mirror_ids[i]);
10550 if (!mirror_id_cbdata.is_valid_id) {
10553 "%s: '%s' invalid specified mirror id: %u.\n",
10554 progname, fname, mirror_ids[i]);
10560 llapi_layout_free(layout);
10561 llapi_lease_release(fd);
10569 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10570 __u16 *mirror_ids, int ids_nr)
10572 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10573 struct llapi_layout *layout;
10575 uint32_t flr_state;
10584 if (stat(fname, &stbuf) < 0) {
10585 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10586 progname, fname, strerror(errno));
10590 if (!S_ISREG(stbuf.st_mode)) {
10591 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10597 /* Allow mirror resync even without the key on encrypted files */
10598 fd = open(fname, O_DIRECT | O_RDWR | O_FILE_ENC);
10600 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10601 progname, fname, strerror(errno));
10606 layout = llapi_layout_get_by_fd(fd, 0);
10608 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10609 progname, fname, strerror(errno));
10614 rc = llapi_layout_flags_get(layout, &flr_state);
10616 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10617 progname, fname, strerror(errno));
10622 flr_state &= LCM_FL_FLR_MASK;
10623 if (flr_state == LCM_FL_NONE) {
10625 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10630 /* get stale component info */
10631 comp_size = llapi_mirror_find_stale(layout, comp_array,
10632 ARRAY_SIZE(comp_array),
10633 mirror_ids, ids_nr);
10634 if (comp_size <= 0) {
10639 ioc->lil_mode = LL_LEASE_WRLCK;
10640 ioc->lil_flags = LL_LEASE_RESYNC;
10641 rc = llapi_lease_set(fd, ioc);
10643 if (rc == -EALREADY)
10647 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10648 progname, fname, strerror(-rc));
10652 /* get the read range [start, end) */
10653 start = comp_array[0].lrc_start;
10654 end = comp_array[0].lrc_end;
10655 for (idx = 1; idx < comp_size; idx++) {
10656 if (comp_array[idx].lrc_start < start)
10657 start = comp_array[idx].lrc_start;
10658 if (end < comp_array[idx].lrc_end)
10659 end = comp_array[idx].lrc_end;
10662 rc = llapi_lease_check(fd);
10663 if (rc != LL_LEASE_WRLCK) {
10664 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10669 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10672 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10673 progname, fname, strerror(-rc));
10675 rc = migrate_set_timestamps(fd, &stbuf);
10677 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
10678 progname, fname, strerror(-rc));
10682 /* need to do the lease unlock even resync fails */
10683 ioc->lil_mode = LL_LEASE_UNLCK;
10684 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10685 ioc->lil_count = 0;
10686 for (idx = 0; idx < comp_size; idx++) {
10687 if (comp_array[idx].lrc_synced) {
10688 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10693 rc2 = llapi_lease_set(fd, ioc);
10695 * llapi_lease_set returns lease mode when it request to unlock
10699 /* rc2 == 0 means lost lease lock */
10700 if (rc2 == 0 && rc == 0)
10704 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10706 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10708 llapi_lease_release(fd);
10713 llapi_layout_free(layout);
10720 static inline int lfs_mirror_resync(int argc, char **argv)
10722 struct option long_opts[] = {
10723 { .val = 'h', .name = "help", .has_arg = no_argument },
10724 { .val = 'o', .name = "only", .has_arg = required_argument },
10725 { .name = NULL } };
10726 struct ll_ioc_lease *ioc = NULL;
10727 __u16 mirror_ids[128] = { 0 };
10732 while ((c = getopt_long(argc, argv, "ho:", long_opts, NULL)) >= 0) {
10735 rc = parse_mirror_ids(mirror_ids,
10736 sizeof(mirror_ids) / sizeof(__u16),
10740 "%s: bad mirror ids '%s'.\n",
10747 fprintf(stderr, "%s: unrecognized option '%s'\n",
10748 progname, argv[optind - 1]);
10756 if (argc == optind) {
10757 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10762 if (ids_nr > 0 && argc > optind + 1) {
10764 "%s: option '--only' cannot be used upon multiple files.\n",
10771 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10776 /* set the lease on the file */
10777 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10779 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10780 argv[0], strerror(errno));
10785 for (; optind < argc; optind++) {
10786 rc = lfs_mirror_resync_file(argv[optind], ioc,
10787 mirror_ids, ids_nr);
10788 /* ignore previous file's error, continue with next file */
10791 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10799 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10801 struct llapi_layout *layout;
10804 layout = llapi_layout_get_by_fd(fd, 0);
10806 fprintf(stderr, "could not get layout.\n");
10810 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10812 fprintf(stderr, "failed to iterate layout\n");
10813 llapi_layout_free(layout);
10816 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10817 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10818 llapi_layout_free(layout);
10822 llapi_layout_free(layout);
10828 * Check whether two files are the same file
10829 * \retval 0 same file
10830 * \retval 1 not the same file
10831 * \retval <0 error code
10833 static inline int check_same_file(int fd, const char *f2)
10835 struct stat stbuf1;
10836 struct stat stbuf2;
10838 if (fstat(fd, &stbuf1) < 0)
10841 if (stat(f2, &stbuf2) < 0)
10844 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10845 stbuf1.st_ino == stbuf2.st_ino)
10851 static inline int lfs_mirror_read(int argc, char **argv)
10854 __u16 mirror_id = 0;
10855 const char *outfile = NULL;
10861 const size_t buflen = 4 << 20;
10863 struct option long_opts[] = {
10864 { .val = 'h', .name = "help", .has_arg = no_argument },
10865 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10866 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10867 { .name = NULL } };
10869 while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
10874 unsigned long int id;
10877 id = strtoul(optarg, &end, 0);
10878 if (errno != 0 || *end != '\0' || id == 0 ||
10881 "%s %s: invalid mirror ID '%s'\n",
10882 progname, argv[0], optarg);
10886 mirror_id = (__u16)id;
10893 fprintf(stderr, "%s: unrecognized option '%s'\n",
10894 progname, argv[optind - 1]);
10901 if (argc == optind) {
10902 fprintf(stderr, "%s %s: no mirrored file provided\n",
10903 progname, argv[0]);
10905 } else if (argc > optind + 1) {
10906 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10910 if (mirror_id == 0) {
10911 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10912 progname, argv[0]);
10916 /* open mirror file */
10917 fname = argv[optind];
10918 fd = open(fname, O_DIRECT | O_RDONLY);
10920 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10921 progname, argv[0], fname, strerror(errno));
10925 /* verify mirror id */
10926 rc = verify_mirror_id_by_fd(fd, mirror_id);
10929 "%s %s: cannot find mirror with ID %u in '%s'\n",
10930 progname, argv[0], mirror_id, fname);
10934 /* open output file - O_EXCL ensures output is not the same as input */
10936 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10938 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10939 progname, argv[0], outfile, strerror(errno));
10944 outfd = STDOUT_FILENO;
10947 /* allocate buffer */
10948 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10950 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10951 progname, argv[0], rc);
10957 ssize_t bytes_read;
10958 ssize_t written = 0;
10960 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10961 if (bytes_read < 0) {
10964 "%s %s: fail to read data from mirror %u: %s\n",
10965 progname, argv[0], mirror_id, strerror(-rc));
10970 if (bytes_read == 0)
10973 while (written < bytes_read) {
10976 written2 = write(outfd, buf + written,
10977 bytes_read - written);
10978 if (written2 < 0) {
10980 "%s %s: fail to write %s: %s\n",
10981 progname, argv[0], outfile ? : "STDOUT",
10986 written += written2;
10989 if (written != bytes_read) {
10991 "%s %s: written %ld bytes does not match with %ld read.\n",
10992 progname, argv[0], written, bytes_read);
11014 static inline int lfs_mirror_write(int argc, char **argv)
11017 __u16 mirror_id = 0;
11018 const char *inputfile = NULL;
11024 const size_t buflen = 4 << 20;
11026 size_t page_size = sysconf(_SC_PAGESIZE);
11027 struct ll_ioc_lease_id ioc;
11028 struct option long_opts[] = {
11029 { .val = 'h', .name = "help", .has_arg = no_argument },
11030 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
11031 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11032 { .name = NULL } };
11034 while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11039 unsigned long int id;
11042 id = strtoul(optarg, &end, 0);
11043 if (errno != 0 || *end != '\0' || id == 0 ||
11046 "%s %s: invalid mirror ID '%s'\n",
11047 progname, argv[0], optarg);
11051 mirror_id = (__u16)id;
11055 inputfile = optarg;
11058 fprintf(stderr, "%s: unrecognized option '%s'\n",
11059 progname, argv[optind - 1]);
11066 if (argc == optind) {
11067 fprintf(stderr, "%s %s: no mirrored file provided\n",
11068 progname, argv[0]);
11070 } else if (argc > optind + 1) {
11071 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11075 if (mirror_id == 0) {
11076 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11077 progname, argv[0]);
11081 /* open mirror file */
11082 fname = argv[optind];
11083 fd = open(fname, O_DIRECT | O_WRONLY);
11085 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11086 progname, argv[0], fname, strerror(errno));
11090 /* verify mirror id */
11091 rc = verify_mirror_id_by_fd(fd, mirror_id);
11094 "%s %s: cannot find mirror with ID %u in '%s'\n",
11095 progname, argv[0], mirror_id, fname);
11099 /* open input file */
11101 rc = check_same_file(fd, inputfile);
11104 "%s %s: input file cannot be the mirrored file\n",
11105 progname, argv[0]);
11111 inputfd = open(inputfile, O_RDONLY, 0644);
11113 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11114 progname, argv[0], inputfile, strerror(errno));
11119 inputfd = STDIN_FILENO;
11122 /* allocate buffer */
11123 rc = posix_memalign(&buf, page_size, buflen);
11125 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11126 progname, argv[0], rc);
11127 goto close_inputfd;
11130 /* prepare target mirror components instantiation */
11131 ioc.lil_mode = LL_LEASE_WRLCK;
11132 ioc.lil_flags = LL_LEASE_RESYNC;
11133 ioc.lil_mirror_id = mirror_id;
11134 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11137 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11138 progname, argv[0], fname, strerror(errno));
11144 ssize_t bytes_read;
11148 rc = llapi_lease_check(fd);
11149 if (rc != LL_LEASE_WRLCK) {
11150 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11151 progname, argv[0], fname);
11155 bytes_read = read(inputfd, buf, buflen);
11156 if (bytes_read < 0) {
11159 "%s %s: fail to read data from '%s': %s\n",
11160 progname, argv[0], inputfile ? : "STDIN",
11167 if (bytes_read == 0)
11170 /* round up to page align to make direct IO happy. */
11171 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11173 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11178 "%s %s: fail to write to mirror %u: %s\n",
11179 progname, argv[0], mirror_id,
11187 if (pos & (page_size - 1)) {
11188 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11193 ioc.lil_mode = LL_LEASE_UNLCK;
11194 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11196 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11201 "%s %s: release lease lock of '%s' failed: %s\n",
11202 progname, argv[0], fname, strerror(errno));
11219 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11221 struct llapi_layout *layout;
11222 struct collect_ids_data cid = { .cid_ids = ids,
11224 .cid_exclude = exclude_id, };
11227 layout = llapi_layout_get_by_fd(fd, 0);
11229 fprintf(stderr, "could not get layout\n");
11233 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11235 fprintf(stderr, "failed to iterate layout\n");
11236 llapi_layout_free(layout);
11240 llapi_layout_free(layout);
11242 return cid.cid_count;
11245 #ifndef MIRROR_ID_NEG
11246 #define MIRROR_ID_NEG 0x8000
11249 static inline int lfs_mirror_copy(int argc, char **argv)
11252 __u16 read_mirror_id = 0;
11253 __u16 ids[128] = { 0 };
11255 struct llapi_layout *layout = NULL;
11256 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11263 struct ll_ioc_lease *ioc = NULL;
11264 struct ll_ioc_lease_id *resync_ioc;
11265 struct option long_opts[] = {
11266 { .val = 'h', .name = "help", .has_arg = no_argument },
11267 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
11268 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
11269 { .name = NULL } };
11270 char cmd[PATH_MAX];
11272 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11274 while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
11279 unsigned long int id;
11282 id = strtoul(optarg, &end, 0);
11283 if (errno != 0 || *end != '\0' || id == 0 ||
11286 "%s: invalid read mirror ID '%s'\n",
11291 read_mirror_id = (__u16)id;
11295 if (!strcmp(optarg, "-1")) {
11296 /* specify all other mirrors */
11297 ids[0] = (__u16)-1;
11300 count = parse_mirror_ids((__u16 *)ids,
11308 fprintf(stderr, "%s: unrecognized option '%s'\n",
11309 progname, argv[optind - 1]);
11316 if (argc == optind) {
11317 fprintf(stderr, "%s %s: no mirrored file provided\n",
11318 progname, argv[0]);
11320 } else if (argc > optind + 1) {
11321 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11325 if (read_mirror_id == 0) {
11327 "%s %s: no valid read mirror ID %d is provided\n",
11328 progname, argv[0], read_mirror_id);
11334 "%s %s: no write mirror ID is provided\n",
11335 progname, argv[0]);
11339 for (i = 0; i < count; i++) {
11340 if (read_mirror_id == ids[i]) {
11342 "%s %s: read and write mirror ID cannot be the same\n",
11343 progname, argv[0]);
11348 /* open mirror file */
11349 fname = argv[optind];
11351 fd = open(fname, O_DIRECT | O_RDWR);
11353 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11354 progname, argv[0], fname, strerror(errno));
11358 /* write to all other mirrors */
11359 if (ids[0] == (__u16)-1) {
11360 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11364 "%s %s: failed to get other mirror ids in '%s': %d\n",
11365 progname, argv[0], fname, rc);
11370 /* verify mirror id */
11371 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11374 "%s %s: cannot find mirror with ID %u in '%s'\n",
11375 progname, argv[0], read_mirror_id, fname);
11379 for (i = 0; i < count; i++) {
11380 rc = verify_mirror_id_by_fd(fd, ids[i]);
11383 "%s %s: cannot find mirror with ID %u in '%s'\n",
11384 progname, argv[0], ids[i], fname);
11389 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11392 "%s %s: cannot alloc comp id array for ioc: %s\n",
11393 progname, argv[0], strerror(errno));
11398 /* get stale component info */
11399 layout = llapi_layout_get_by_fd(fd, 0);
11401 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11402 progname, argv[0], fname, strerror(errno));
11406 comp_size = llapi_mirror_find_stale(layout, comp_array,
11407 ARRAY_SIZE(comp_array),
11409 llapi_layout_free(layout);
11410 if (comp_size < 0) {
11415 /* prepare target mirror components instantiation */
11416 resync_ioc = (struct ll_ioc_lease_id *)ioc;
11417 resync_ioc->lil_mode = LL_LEASE_WRLCK;
11418 resync_ioc->lil_flags = LL_LEASE_RESYNC;
11420 resync_ioc->lil_mirror_id = ids[0];
11422 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11423 rc = llapi_lease_set(fd, ioc);
11426 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11427 progname, argv[0], fname, strerror(errno));
11431 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11434 fprintf(stderr, "%s %s: copy error: %d\n",
11435 progname, argv[0], rc);
11439 fprintf(stdout, "mirror copied successfully: ");
11440 for (i = 0; i < copied; i++)
11441 fprintf(stdout, "%d ", ids[i]);
11442 fprintf(stdout, "\n");
11444 ioc->lil_mode = LL_LEASE_UNLCK;
11445 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11446 ioc->lil_count = 0;
11447 for (i = 0; i < comp_size; i++) {
11450 for (j = 0; j < copied; j++) {
11451 if (comp_array[i].lrc_mirror_id != ids[j])
11454 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11458 rc = llapi_lease_set(fd, ioc);
11463 "%s %s: release lease lock of '%s' failed: %s\n",
11464 progname, argv[0], fname, strerror(errno));
11479 * struct verify_chunk - Mirror chunk to be verified.
11480 * @chunk: [start, end) of the chunk.
11481 * @mirror_count: Number of mirror ids in @mirror_id array.
11482 * @mirror_id: Array of valid mirror ids that cover the chunk.
11484 struct verify_chunk {
11485 struct lu_extent chunk;
11486 unsigned int mirror_count;
11487 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11491 * print_chunks() - Print chunk information.
11492 * @fname: Mirrored file name.
11493 * @chunks: Array of chunks.
11494 * @chunk_count: Number of chunks in @chunks array.
11496 * This function prints [start, end) of each chunk in @chunks
11497 * for mirrored file @fname, and also prints the valid mirror ids
11498 * that cover the chunk.
11503 void print_chunks(const char *fname, struct verify_chunk *chunks,
11509 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11510 for (i = 0; i < chunk_count; i++) {
11511 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11513 if (chunks[i].mirror_count == 0)
11514 fprintf(stdout, "\t[");
11516 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11517 for (j = 1; j < chunks[i].mirror_count; j++)
11518 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11520 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11522 fprintf(stdout, "\n");
11526 * print_checksums() - Print CRC-32 checksum values.
11527 * @chunk: A chunk and its corresponding valid mirror ids.
11528 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
11530 * This function prints CRC-32 checksum values on @chunk for
11531 * each valid mirror that covers it.
11536 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
11541 "CRC-32 checksum value for chunk "DEXT":\n",
11542 PEXT(&chunk->chunk));
11543 for (i = 0; i < chunk->mirror_count; i++)
11544 fprintf(stdout, "Mirror %u:\t%#lx\n",
11545 chunk->mirror_id[i], crc[i]);
11546 fprintf(stdout, "\n");
11550 * filter_mirror_id() - Filter specified mirror ids.
11551 * @chunks: Array of chunks.
11552 * @chunk_count: Number of chunks in @chunks array.
11553 * @mirror_ids: Specified mirror ids to be verified.
11554 * @ids_nr: Number of specified mirror ids.
11556 * This function scans valid mirror ids that cover each chunk in @chunks
11557 * and filters specified mirror ids.
11562 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11563 __u16 *mirror_ids, int ids_nr)
11568 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11569 unsigned int valid_count = 0;
11571 for (i = 0; i < chunk_count; i++) {
11572 if (chunks[i].mirror_count == 0)
11576 for (j = 0; j < ids_nr; j++) {
11577 for (k = 0; k < chunks[i].mirror_count; k++) {
11578 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11579 valid_id[valid_count] = mirror_ids[j];
11586 memcpy(chunks[i].mirror_id, valid_id,
11587 sizeof(__u16) * valid_count);
11588 chunks[i].mirror_count = valid_count;
11593 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11594 * @layout: Mirror component list.
11595 * @chunks: Array of chunks.
11596 * @chunks_size: Array size of @chunks.
11598 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11599 * to find out chunk segments and store them in @chunks array.
11601 * The @mirror_id array in each element of @chunks will store the valid
11602 * mirror ids that cover the chunk. If a mirror component covering the
11603 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11604 * will not be stored into the @mirror_id array, and the chunk for that
11605 * mirror will not be verified.
11607 * The @mirror_count in each element of @chunks will store the number of
11608 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11609 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11610 * indicates the chunk is valid in only one mirror. In both cases, the
11611 * chunk will not be verified.
11613 * Here is an example:
11615 * 0 1M 2M 3M 4M EOF
11616 * +------+-------------+--------------------+
11617 * | | | S | mirror1
11618 * +------+------+------+------+-------------+
11619 * | | S | S | | mirror2
11620 * +-------------+------+------+-------------+
11622 * prepared @chunks array will contain 5 elements:
11623 * (([0, 1M), [1, 2], 2),
11624 * ([1M, 2M), [1, 2], 2),
11625 * ([2M, 3M), [1], 1),
11626 * ([3M, 4M], [], 0),
11627 * ([4M, EOF), [2], 1))
11629 * Return: the actual array size of @chunks on success
11630 * or a negative error code on failure.
11633 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11634 struct verify_chunk *chunks,
11635 size_t chunks_size)
11639 uint32_t mirror_id;
11645 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11648 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11651 "%s: move to the first layout component: %s.\n",
11652 progname, strerror(errno));
11658 chunks[idx].chunk.e_end = LUSTRE_EOF;
11660 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11663 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11664 progname, strerror(errno));
11668 if (start > chunks[idx].chunk.e_start ||
11669 end <= chunks[idx].chunk.e_start)
11672 if (end < chunks[idx].chunk.e_end)
11673 chunks[idx].chunk.e_end = end;
11675 rc = llapi_layout_comp_flags_get(layout, &flags);
11678 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11679 progname, strerror(errno));
11683 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11686 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11689 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11690 progname, strerror(errno));
11694 chunks[idx].mirror_id[i] = mirror_id;
11696 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11698 "%s: mirror_id array is too small.\n",
11705 rc = llapi_layout_comp_use(layout,
11706 LLAPI_LAYOUT_COMP_USE_NEXT);
11709 "%s: move to the next layout component: %s.\n",
11710 progname, strerror(errno));
11713 } /* loop through all components */
11715 chunks[idx].mirror_count = i;
11717 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11721 if (idx >= chunks_size) {
11722 fprintf(stderr, "%s: chunks array is too small.\n",
11728 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11732 return rc < 0 ? rc : idx + 1;
11736 * lfs_mirror_verify_chunk() - Verify a chunk.
11737 * @fd: File descriptor of the mirrored file.
11738 * @file_size: Size of the mirrored file.
11739 * @chunk: A chunk and its corresponding valid mirror ids.
11740 * @verbose: Verbose mode.
11742 * This function verifies a @chunk contains exactly the same data
11743 * ammong the mirrors that cover it.
11745 * If @verbose is specified, then the function will print where the
11746 * differences are if the data do not match. Otherwise, it will
11747 * just return an error in that case.
11749 * Return: 0 on success or a negative error code on failure.
11752 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11753 struct verify_chunk *chunk, int verbose)
11755 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11757 size_t page_size = sysconf(_SC_PAGESIZE);
11758 ssize_t bytes_read;
11759 ssize_t bytes_done;
11763 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11767 if (file_size == 0)
11770 rc = posix_memalign(&buf, page_size, buflen);
11771 if (rc) /* error code is returned directly */
11775 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11776 PEXT(&chunk->chunk));
11777 for (i = 0; i < chunk->mirror_count; i++)
11778 fprintf(stdout, " %u", chunk->mirror_id[i]);
11779 fprintf(stdout, "\n");
11783 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11784 pos = chunk->chunk.e_start;
11785 while (bytes_done < count) {
11786 /* compute initial CRC-32 checksum */
11787 crc = crc32(0L, Z_NULL, 0);
11788 memset(crc_array, 0, sizeof(crc_array));
11791 for (i = 0; i < chunk->mirror_count; i++) {
11792 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11794 if (bytes_read < 0) {
11797 "%s: failed to read data from mirror %u: %s.\n",
11798 progname, chunk->mirror_id[i],
11803 /* compute new CRC-32 checksum */
11804 crc_array[i] = crc32(crc, buf, bytes_read);
11808 print_checksums(chunk, crc_array);
11810 /* compare CRC-32 checksum values */
11811 for (i = 1; i < chunk->mirror_count; i++) {
11812 if (crc_array[i] != crc_array[0]) {
11816 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11817 progname, PEXT(&chunk->chunk),
11818 chunk->mirror_id[0],
11819 chunk->mirror_id[i]);
11824 bytes_done += bytes_read;
11827 if (verbose > 1 && rc == 0) {
11828 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11829 PEXT(&chunk->chunk));
11830 for (i = 0; i < chunk->mirror_count; i++)
11831 fprintf(stdout, " %u", chunk->mirror_id[i]);
11832 fprintf(stdout, " PASS\n\n");
11841 * lfs_mirror_verify_file() - Verify a mirrored file.
11842 * @fname: Mirrored file name.
11843 * @mirror_ids: Specified mirror ids to be verified.
11844 * @ids_nr: Number of specified mirror ids.
11845 * @verbose: Verbose mode.
11847 * This function verifies that each SYNC mirror of a mirrored file
11848 * specified by @fname contains exactly the same data.
11850 * If @mirror_ids is specified, then the function will verify the
11851 * mirrors specified by @mirror_ids contain exactly the same data.
11853 * If @verbose is specified, then the function will print where the
11854 * differences are if the data do not match. Otherwise, it will
11855 * just return an error in that case.
11857 * Return: 0 on success or a negative error code on failure.
11860 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11863 struct verify_chunk chunks_array[1024] = { };
11864 struct llapi_layout *layout = NULL;
11866 uint32_t flr_state;
11868 int chunk_count = 0;
11874 if (stat(fname, &stbuf) < 0) {
11875 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11876 progname, fname, strerror(errno));
11881 if (!S_ISREG(stbuf.st_mode)) {
11882 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11888 if (stbuf.st_size == 0) {
11890 fprintf(stdout, "%s: '%s' file size is 0.\n",
11896 /* Allow mirror verify even without the key on encrypted files */
11897 fd = open(fname, O_DIRECT | O_RDONLY | O_FILE_ENC);
11899 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11900 progname, fname, strerror(errno));
11905 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11907 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11908 progname, fname, strerror(errno));
11912 layout = llapi_layout_get_by_fd(fd, 0);
11914 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11915 progname, fname, strerror(errno));
11917 llapi_lease_release(fd);
11921 rc = llapi_layout_flags_get(layout, &flr_state);
11923 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11924 progname, fname, strerror(errno));
11929 flr_state &= LCM_FL_FLR_MASK;
11930 switch (flr_state) {
11933 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11934 progname, fname, llapi_layout_flags_string(flr_state));
11940 /* find out mirror chunks to be verified */
11941 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11942 ARRAY_SIZE(chunks_array));
11943 if (chunk_count < 0) {
11949 /* filter specified mirror ids */
11950 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11953 print_chunks(fname, chunks_array, chunk_count);
11955 for (idx = 0; idx < chunk_count; idx++) {
11956 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11959 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11961 PEXT(&chunks_array[idx].chunk),
11962 (unsigned long long)stbuf.st_size);
11966 if (chunks_array[idx].mirror_count == 0) {
11968 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11970 PEXT(&chunks_array[idx].chunk));
11972 fprintf(stderr, "skipped\n");
11976 fprintf(stderr, "failed\n");
11980 if (chunks_array[idx].mirror_count == 1) {
11983 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11985 PEXT(&chunks_array[idx].chunk),
11986 chunks_array[idx].mirror_id[0]);
11990 rc = llapi_lease_check(fd);
11991 if (rc != LL_LEASE_RDLCK) {
11992 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11997 /* verify one chunk */
11998 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11999 &chunks_array[idx], verbose);
12013 llapi_layout_free(layout);
12014 llapi_lease_release(fd);
12022 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12023 * @argc: The count of lfs mirror verify command line arguments.
12024 * @argv: Array of strings for lfs mirror verify command line arguments.
12026 * This function parses lfs mirror verify command and verifies the
12027 * specified mirrored file(s).
12029 * Return: 0 on success or a negative error code on failure.
12031 static inline int lfs_mirror_verify(int argc, char **argv)
12033 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12039 char cmd[PATH_MAX];
12041 struct option long_opts[] = {
12042 { .val = 'h', .name = "help", .has_arg = no_argument },
12043 { .val = 'o', .name = "only", .has_arg = required_argument },
12044 { .val = 'v', .name = "verbose", .has_arg = no_argument },
12045 { .name = NULL } };
12047 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12049 while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12052 rc = parse_mirror_ids(mirror_ids,
12053 ARRAY_SIZE(mirror_ids),
12057 "%s: bad mirror ids '%s'.\n",
12064 "%s: at least 2 mirror ids needed with '--only' option.\n",
12074 fprintf(stderr, "%s: unrecognized option '%s'\n",
12075 progname, argv[optind - 1]);
12083 if (argc == optind) {
12084 fprintf(stderr, "%s: no file name given.\n", progname);
12089 if (ids_nr > 0 && argc > optind + 1) {
12091 "%s: '--only' cannot be used upon multiple files.\n",
12098 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12104 for (; optind < argc; optind++) {
12105 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12115 * lfs_mirror() - Parse and execute lfs mirror commands.
12116 * @argc: The count of lfs mirror command line arguments.
12117 * @argv: Array of strings for lfs mirror command line arguments.
12119 * This function parses lfs mirror commands and performs the
12120 * corresponding functions specified in mirror_cmdlist[].
12122 * Return: 0 on success or an error code on failure.
12124 static int lfs_mirror(int argc, char **argv)
12126 char cmd[PATH_MAX];
12129 setlinebuf(stdout);
12131 Parser_init("lfs-mirror > ", mirror_cmdlist);
12133 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12135 program_invocation_short_name = cmd;
12137 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12139 rc = Parser_commands();
12141 return rc < 0 ? -rc : rc;
12144 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12146 #if __BYTE_ORDER == __BIG_ENDIAN
12147 __swab16s(&attrs->lsa_valid);
12148 __swab64s(&attrs->lsa_size);
12149 __swab64s(&attrs->lsa_blocks);
12153 enum lfs_som_type {
12154 LFS_SOM_SIZE = 0x1,
12155 LFS_SOM_BLOCKS = 0x2,
12156 LFS_SOM_FLAGS = 0x4,
12157 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12161 static int lfs_getsom(int argc, char **argv)
12164 struct lustre_som_attrs *attrs;
12165 char buf[sizeof(*attrs) + 64];
12166 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12169 while ((c = getopt(argc, argv, "bfhs")) != -1) {
12172 type = LFS_SOM_BLOCKS;
12175 type = LFS_SOM_FLAGS;
12178 type = LFS_SOM_SIZE;
12181 fprintf(stderr, "%s: unrecognized option '%s'\n",
12182 progname, argv[optind - 1]);
12193 fprintf(stderr, "%s: %s\n",
12194 progname, argc == 0 ? "miss file target" :
12195 "input more than 2 files");
12200 attrs = (void *)buf;
12201 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12204 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12205 argv[0], strerror(errno), errno);
12209 lustre_som_swab(attrs);
12212 case LFS_SOM_ATTR_ALL:
12213 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12214 path, (unsigned long long)attrs->lsa_size,
12215 (unsigned long long)attrs->lsa_blocks,
12219 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12221 case LFS_SOM_BLOCKS:
12222 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12224 case LFS_SOM_FLAGS:
12225 printf("%x\n", attrs->lsa_valid);
12228 fprintf(stderr, "%s: unknown option\n", progname);
12236 * lfs_mirror_list_commands() - List lfs mirror commands.
12237 * @argc: The count of command line arguments.
12238 * @argv: Array of strings for command line arguments.
12240 * This function lists lfs mirror commands defined in mirror_cmdlist[].
12242 * Return: 0 on success.
12244 static int lfs_mirror_list_commands(int argc, char **argv)
12246 char buffer[81] = "";
12248 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12254 static int lfs_pcc_attach(int argc, char **argv)
12256 struct option long_opts[] = {
12257 { .val = 'h', .name = "help", .has_arg = no_argument },
12258 { .val = 'i', .name = "id", .has_arg = required_argument },
12259 { .name = NULL } };
12262 __u32 archive_id = 0;
12265 char fullpath[PATH_MAX];
12266 enum lu_pcc_type type = LU_PCC_READWRITE;
12269 while ((c = getopt_long(argc, argv, "hi:",
12270 long_opts, NULL)) != -1) {
12274 archive_id = strtoul(optarg, &end, 0);
12275 if (errno != 0 || *end != '\0' ||
12276 archive_id == 0 || archive_id > UINT32_MAX) {
12278 "error: %s: bad archive ID '%s'\n",
12284 fprintf(stderr, "%s: unrecognized option '%s'\n",
12285 progname, argv[optind - 1]);
12292 if (archive_id == 0) {
12293 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12297 if (argc <= optind) {
12298 fprintf(stderr, "%s: must specify one or more file names\n",
12303 while (optind < argc) {
12306 path = argv[optind++];
12307 if (!realpath(path, fullpath)) {
12308 fprintf(stderr, "%s: could not find path '%s': %s\n",
12309 argv[0], path, strerror(errno));
12315 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12318 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12319 argv[0], path, archive_id, strerror(-rc2));
12327 static int lfs_pcc_attach_fid(int argc, char **argv)
12329 struct option long_opts[] = {
12330 { .val = 'h', .name = "help", .has_arg = no_argument },
12331 { .val = 'i', .name = "id", .has_arg = required_argument },
12332 { .val = 'm', .name = "mnt", .has_arg = required_argument },
12333 { .name = NULL } };
12336 __u32 archive_id = 0;
12338 const char *mntpath = NULL;
12339 const char *fidstr;
12340 enum lu_pcc_type type = LU_PCC_READWRITE;
12343 while ((c = getopt_long(argc, argv, "hi:m:",
12344 long_opts, NULL)) != -1) {
12348 archive_id = strtoul(optarg, &end, 0);
12349 if (errno != 0 || *end != '\0' ||
12350 archive_id > UINT32_MAX) {
12352 "error: %s: bad archive ID '%s'\n",
12361 fprintf(stderr, "%s: unrecognized option '%s'\n",
12362 progname, argv[optind - 1]);
12369 if (archive_id == 0) {
12370 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12375 fprintf(stderr, "%s: must specify Lustre mount point\n",
12380 if (argc <= optind) {
12381 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12385 while (optind < argc) {
12388 fidstr = argv[optind++];
12390 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12394 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12395 argv[0], fidstr, mntpath, archive_id,
12398 if (rc == 0 && rc2 < 0)
12404 static int lfs_pcc_detach(int argc, char **argv)
12406 struct option long_opts[] = {
12407 { .val = 'h', .name = "help", .has_arg = no_argument },
12408 { .val = 'k', .name = "keep", .has_arg = no_argument },
12409 { .name = NULL } };
12413 char fullpath[PATH_MAX];
12414 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12417 while ((c = getopt_long(argc, argv, "hk",
12418 long_opts, NULL)) != -1) {
12421 detach_opt = PCC_DETACH_OPT_NONE;
12424 fprintf(stderr, "%s: unrecognized option '%s'\n",
12425 progname, argv[optind - 1]);
12432 while (optind < argc) {
12435 path = argv[optind++];
12436 if (!realpath(path, fullpath)) {
12437 fprintf(stderr, "%s: could not find path '%s': %s\n",
12438 argv[0], path, strerror(errno));
12444 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12448 "%s: cannot detach '%s' from PCC: %s\n",
12449 argv[0], path, strerror(errno));
12457 static int lfs_pcc_detach_fid(int argc, char **argv)
12459 struct option long_opts[] = {
12460 { .val = 'h', .name = "help", .has_arg = no_argument },
12461 { .val = 'k', .name = "keep", .has_arg = no_argument },
12462 { .name = NULL } };
12466 const char *mntpath;
12467 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12470 while ((c = getopt_long(argc, argv, "hk",
12471 long_opts, NULL)) != -1) {
12474 detach_opt = PCC_DETACH_OPT_NONE;
12477 fprintf(stderr, "%s: unrecognized option '%s'\n",
12478 progname, argv[optind - 1]);
12485 mntpath = argv[optind++];
12487 while (optind < argc) {
12490 fid = argv[optind++];
12492 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12495 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12496 argv[0], fid, mntpath, strerror(-rc2));
12504 static int lfs_pcc_state(int argc, char **argv)
12508 char fullpath[PATH_MAX];
12509 struct lu_pcc_state state;
12514 fprintf(stderr, "%s: must specify one or more file names\n",
12519 while (optind < argc) {
12522 path = argv[optind++];
12523 if (!realpath(path, fullpath)) {
12524 fprintf(stderr, "%s: could not find path '%s': %s\n",
12525 argv[0], path, strerror(errno));
12531 rc2 = llapi_pcc_state_get(fullpath, &state);
12536 "%s: cannot get PCC state of '%s': %s\n",
12537 argv[0], path, strerror(-rc2));
12541 printf("file: %s", path);
12542 printf(", type: %s", pcc_type2string(state.pccs_type));
12543 if (state.pccs_type == LU_PCC_NONE &&
12544 state.pccs_open_count == 0) {
12549 printf(", PCC file: %s", state.pccs_path);
12550 printf(", user number: %u", state.pccs_open_count);
12551 printf(", flags: %x", state.pccs_flags);
12558 * lfs_pcc_list_commands() - List lfs pcc commands.
12559 * @argc: The count of command line arguments.
12560 * @argv: Array of strings for command line arguments.
12562 * This function lists lfs pcc commands defined in pcc_cmdlist[].
12564 * Return: 0 on success.
12566 static int lfs_pcc_list_commands(int argc, char **argv)
12568 char buffer[81] = "";
12570 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12577 * lfs_pcc() - Parse and execute lfs pcc commands.
12578 * @argc: The count of lfs pcc command line arguments.
12579 * @argv: Array of strings for lfs pcc command line arguments.
12581 * This function parses lfs pcc commands and performs the
12582 * corresponding functions specified in pcc_cmdlist[].
12584 * Return: 0 on success or an error code on failure.
12586 static int lfs_pcc(int argc, char **argv)
12588 char cmd[PATH_MAX];
12591 setlinebuf(stdout);
12593 Parser_init("lfs-pcc > ", pcc_cmdlist);
12595 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12597 program_invocation_short_name = cmd;
12599 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12601 rc = Parser_commands();
12603 return rc < 0 ? -rc : rc;
12606 static int lfs_list_commands(int argc, char **argv)
12608 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12610 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12615 int main(int argc, char **argv)
12619 /* Ensure that liblustreapi constructor has run */
12620 if (!llapi_liblustreapi_initialized())
12621 fprintf(stderr, "liblustreapi was not properly initialized\n");
12623 setlinebuf(stdout);
12626 Parser_init("lfs > ", cmdlist);
12628 progname = program_invocation_short_name; /* Used in error messages */
12630 llapi_set_command_name(argv[1]);
12631 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12632 llapi_clear_command_name();
12634 rc = Parser_commands();
12637 return rc < 0 ? -rc : rc;
12640 #ifdef _LUSTRE_IDL_H_
12641 /* Everything we need here should be included by lustreapi.h. */
12642 # error "lfs should not depend on lustre_idl.h"
12643 #endif /* _LUSTRE_IDL_H_ */