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) {
1131 "%s: cannot specify both positive and negative flags\n",
1136 if (!flags && neg_flags)
1137 flags = neg_flags | LCME_FL_NEG;
1139 if (flags && comp_id) {
1141 "%s: cannot specify component ID and flags at the same time\n",
1146 if (!flags && !comp_id) {
1148 "%s: neither flags nor component ID is specified\n",
1154 if (flags & ~LCME_KNOWN_FLAGS) {
1156 "%s setstripe: unknown flags %#x\n",
1160 } else if (comp_id > LCME_ID_MAX) {
1161 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1166 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1169 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1170 progname, comp_id, fname, strerror(errno));
1174 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1181 rc = llapi_layout_file_comp_add(fname, layout);
1183 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1184 fname, strerror(errno));
1188 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1189 struct llapi_layout *layout)
1197 fd = lstat(fname, &st);
1198 if (fd == 0 && S_ISDIR(st.st_mode))
1199 open_flags = O_DIRECTORY | O_RDONLY;
1201 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1203 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1204 S_ISDIR(st.st_mode) ?
1205 "set default composite layout for" :
1206 "create composite file",
1207 fname, strerror(errno));
1211 static int lfs_migrate(char *name, __u64 migration_flags,
1212 struct llapi_stripe_param *param,
1213 struct llapi_layout *layout)
1215 struct llapi_layout *existing;
1216 uint64_t dom_new, dom_cur;
1221 rc = migrate_open_files(name, migration_flags, param, layout,
1226 rc = llapi_layout_dom_size(layout, &dom_new);
1228 error_loc = "cannot get new layout DoM size";
1231 /* special case for migration to DOM layout*/
1232 existing = llapi_layout_get_by_fd(fd, 0);
1234 error_loc = "cannot get existing layout";
1238 rc = llapi_layout_dom_size(existing, &dom_cur);
1240 error_loc = "cannot get current layout DoM size";
1245 * if file has DoM layout already then migration is possible to
1246 * the new layout with the same DoM component via swap layout,
1247 * if new layout used bigger DOM size, then mirroring is used
1249 if (dom_new > dom_cur) {
1250 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags);
1252 error_loc = "cannot migrate to DOM layout";
1256 if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1258 * Blocking mode (forced if servers do not support file lease).
1259 * It is also the default mode, since we cannot distinguish
1260 * between a broken lease and a server that does not support
1261 * atomic swap/close (LU-6785)
1263 rc = migrate_block(fd, fdv);
1267 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1269 error_loc = "cannot get lease";
1273 rc = migrate_nonblock(fd, fdv);
1275 llapi_lease_release(fd);
1280 * Atomically put lease, swap layouts and close.
1281 * for a migration we need to check data version on file did
1284 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1286 error_loc = "cannot swap layout";
1298 fprintf(stderr, "error: %s: %s: %s: %s\n",
1299 progname, name, error_loc, strerror(-rc));
1300 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1301 printf("%s\n", name);
1306 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1309 char *dup_string = NULL;
1315 if (!string || !string[0])
1318 dup_string = strdup(string);
1320 llapi_printf(LLAPI_MSG_ERROR,
1321 "%s: insufficient memory\n",
1326 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1330 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1331 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1332 const char *comp_name = comp_flags_table[i].cfn_name;
1334 if (strcmp(name, comp_name) == 0) {
1335 *flags |= comp_flag;
1337 } else if (strncmp(name, "^", 1) == 0 &&
1338 strcmp(name + 1, comp_name) == 0) {
1339 *neg_flags |= comp_flag;
1344 llapi_printf(LLAPI_MSG_ERROR,
1345 "%s: component flag '%s' not supported\n",
1352 if (!*flags && !*neg_flags)
1355 /* don't allow to set and exclude the same flag */
1356 if (*flags & *neg_flags)
1364 static int mdthash_input(char *string, __u32 *inflags,
1365 __u32 *exflags, __u32 *type)
1372 {"migrating", LMV_HASH_FLAG_MIGRATION},
1373 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1374 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1383 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1387 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1388 if (strcmp(name, mhflist[i].name) == 0 ||
1389 name[0] == mhflist[i].name[0]) {
1390 *inflags |= mhflist[i].flag;
1392 } else if (name[0] == '^' &&
1393 (strcmp(name + 1, mhflist[i].name) == 0 ||
1394 name[1] == mhflist[i].name[0])) {
1395 *exflags |= mhflist[i].flag;
1400 i = check_hashtype(name);
1405 llapi_printf(LLAPI_MSG_ERROR,
1406 "%s: invalid mdt_hash value '%s'\n",
1412 /* don't allow to include and exclude the same flag */
1413 if (*inflags & *exflags) {
1414 llapi_printf(LLAPI_MSG_ERROR,
1415 "%s: include and exclude same flag '%s'\n",
1423 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1431 if (strncmp(string, "^", 1) == 0) {
1432 *neg_state = llapi_layout_string_flags(string + 1);
1433 if (*neg_state != 0)
1436 *state = llapi_layout_string_flags(string);
1441 llapi_printf(LLAPI_MSG_ERROR,
1442 "%s: mirrored file state '%s' not supported\n",
1448 * struct mirror_args - Command-line arguments for mirror(s).
1449 * @m_count: Number of mirrors to be created with this layout.
1450 * @m_flags: Mirror level flags, only 'prefer' is supported.
1451 * @m_layout: Mirror layout.
1452 * @m_file: A victim file. Its layout will be split and used as a mirror.
1453 * @m_next: Point to the next node of the list.
1455 * Command-line arguments for mirror(s) will be parsed and stored in
1456 * a linked list that consists of this structure.
1458 struct mirror_args {
1461 struct llapi_layout *m_layout;
1463 struct mirror_args *m_next;
1468 * enum mirror_flags - Flags for extending a mirrored file.
1469 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1470 * in case the victim file(s) contains the same data as the
1471 * original mirrored file.
1472 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1473 * @MF_COMP_ID: specified component id instead of mirror id
1475 * Flags for extending a mirrored file.
1485 * mirror_create_sanity_check() - Check mirror list.
1486 * @list: A linked list that stores the mirror arguments.
1488 * This function does a sanity check on @list for creating
1491 * Return: 0 on success or a negative error code on failure.
1493 static int mirror_create_sanity_check(const char *fname,
1494 struct mirror_args *list,
1498 bool has_m_file = false;
1499 bool has_m_layout = false;
1504 if (fname && check_fname) {
1505 struct llapi_layout *layout;
1507 layout = llapi_layout_get_by_path(fname, 0);
1510 "error: %s: file '%s' couldn't get layout\n",
1515 rc = llapi_layout_sanity(layout, fname, false, true);
1517 llapi_layout_free(layout);
1520 llapi_layout_sanity_perror(rc);
1528 llapi_layout_free(list->m_layout);
1531 llapi_layout_get_by_path(list->m_file, 0);
1532 if (!list->m_layout) {
1534 "error: %s: file '%s' has no layout\n",
1535 progname, list->m_file);
1539 has_m_layout = true;
1540 if (!list->m_layout) {
1541 fprintf(stderr, "error: %s: no mirror layout\n",
1547 rc = llapi_layout_sanity(list->m_layout, fname, false, true);
1549 llapi_layout_sanity_perror(rc);
1553 list = list->m_next;
1556 if (has_m_file && has_m_layout) {
1558 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1566 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1568 __u32 mirror_flags = *(__u32 *)cbdata;
1572 rc = llapi_layout_comp_flags_get(layout, &flags);
1577 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1582 return LLAPI_LAYOUT_ITER_CONT;
1586 * mirror_create() - Create a mirrored file.
1587 * @fname: The file to be created.
1588 * @mirror_list: A linked list that stores the mirror arguments.
1590 * This function creates a mirrored file @fname with the mirror(s)
1591 * from @mirror_list.
1593 * Return: 0 on success or a negative error code on failure.
1595 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1597 struct llapi_layout *layout = NULL;
1598 struct mirror_args *cur_mirror = NULL;
1599 uint16_t mirror_count = 0;
1603 rc = mirror_create_sanity_check(fname, mirror_list, false);
1607 cur_mirror = mirror_list;
1608 while (cur_mirror) {
1609 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1611 &cur_mirror->m_flags);
1614 fprintf(stderr, "%s: failed to set mirror flags\n",
1619 for (i = 0; i < cur_mirror->m_count; i++) {
1620 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1624 "error: %s: merge layout failed: %s\n",
1625 progname, strerror(errno));
1629 mirror_count += cur_mirror->m_count;
1630 cur_mirror = cur_mirror->m_next;
1634 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1638 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1641 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1642 progname, strerror(errno));
1646 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1654 llapi_layout_free(layout);
1659 * Compare files and check lease on @fd.
1661 * \retval bytes number of bytes are the same
1663 static ssize_t mirror_file_compare(int fd, int fdv)
1665 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1667 ssize_t bytes_done = 0;
1668 ssize_t bytes_read = 0;
1670 buf = malloc(buflen * 2);
1675 if (!llapi_lease_check(fd)) {
1676 bytes_done = -EBUSY;
1680 bytes_read = read(fd, buf, buflen);
1681 if (bytes_read <= 0)
1684 if (bytes_read != read(fdv, buf + buflen, buflen))
1688 * XXX: should compute the checksum on each buffer and then
1689 * compare checksum to avoid cache collision
1691 if (memcmp(buf, buf + buflen, bytes_read))
1694 bytes_done += bytes_read;
1702 static int mirror_extend_file(const char *fname, const char *victim_file,
1703 enum mirror_flags mirror_flags)
1708 struct stat stbuf_v;
1709 struct ll_ioc_lease *data = NULL;
1712 fd = open(fname, O_RDWR);
1714 error_loc = "open source file";
1719 fdv = open(victim_file, O_RDWR);
1721 error_loc = "open target file";
1726 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1727 error_loc = "stat source or target file";
1732 if (stbuf.st_dev != stbuf_v.st_dev) {
1733 error_loc = "stat source and target file";
1738 /* mirrors should be of the same size */
1739 if (stbuf.st_size != stbuf_v.st_size) {
1740 error_loc = "file sizes don't match";
1745 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1747 error_loc = "cannot get lease";
1751 if (!(mirror_flags & MF_NO_VERIFY)) {
1753 /* mirrors should have the same contents */
1754 ret = mirror_file_compare(fd, fdv);
1755 if (ret != stbuf.st_size) {
1756 error_loc = "file busy or contents don't match";
1757 rc = ret < 0 ? ret : -EINVAL;
1762 /* Get rid of caching pages from clients */
1763 rc = llapi_file_flush(fd);
1765 error_loc = "cannot get data version";
1769 rc = llapi_file_flush(fdv);
1771 error_loc = "cannot get data version";
1775 rc = migrate_set_timestamps(fd, &stbuf);
1777 error_loc = "cannot set source file timestamp";
1781 /* Atomically put lease, merge layouts and close. */
1782 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1784 error_loc = "memory allocation";
1787 data->lil_mode = LL_LEASE_UNLCK;
1788 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1789 data->lil_count = 1;
1790 data->lil_ids[0] = fdv;
1791 rc = llapi_lease_set(fd, data);
1793 error_loc = "cannot merge layout";
1795 } else if (rc == 0) {
1797 error_loc = "lost lease lock";
1810 (void) unlink(victim_file);
1812 fprintf(stderr, "error: %s: %s: %s: %s\n",
1813 progname, fname, error_loc, strerror(-rc));
1817 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1818 bool inherit, uint32_t flags)
1820 struct llapi_layout *f_layout = NULL;
1821 struct ll_ioc_lease *data = NULL;
1828 f_layout = llapi_layout_get_by_path(name, 0);
1831 fprintf(stderr, "%s: cannot get layout\n", progname);
1834 rc = llapi_layout_get_last_init_comp(f_layout);
1836 fprintf(stderr, "%s: cannot get the last init comp\n",
1840 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1843 "%s: cannot inherit from the last init comp\n",
1849 llapi_layout_comp_flags_set(m_layout, flags);
1850 rc = migrate_open_files(name,
1851 LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
1852 NULL, m_layout, &fd, &fdv);
1856 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1858 error_loc = "cannot get lease";
1862 rc = fstat(fd, &st);
1864 error_loc = "cannot stat source file";
1868 rc = migrate_nonblock(fd, fdv);
1870 llapi_lease_release(fd);
1874 rc = migrate_set_timestamps(fd, &st);
1876 error_loc = "cannot set source file timestamp";
1880 /* Atomically put lease, merge layouts and close. */
1881 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1883 error_loc = "memory allocation";
1886 data->lil_mode = LL_LEASE_UNLCK;
1887 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1888 data->lil_count = 1;
1889 data->lil_ids[0] = fdv;
1890 rc = llapi_lease_set(fd, data);
1892 error_loc = "cannot merge layout";
1894 } else if (rc == 0) {
1896 error_loc = "lost lease lock";
1909 fprintf(stderr, "error: %s: %s: %s: %s\n",
1910 progname, name, error_loc, strerror(-rc));
1914 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1915 enum mirror_flags mirror_flags)
1919 while (mirror_list) {
1920 if (mirror_list->m_file) {
1921 rc = mirror_extend_file(fname, mirror_list->m_file,
1924 __u32 mirror_count = mirror_list->m_count;
1926 while (mirror_count > 0) {
1927 rc = mirror_extend_layout(fname,
1928 mirror_list->m_layout,
1929 mirror_list->m_inherit,
1930 mirror_list->m_flags);
1940 mirror_list = mirror_list->m_next;
1946 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1951 rc = llapi_layout_mirror_id_get(layout, &id);
1955 if ((__u16)id == *(__u16 *)cbdata)
1956 return LLAPI_LAYOUT_ITER_STOP;
1958 return LLAPI_LAYOUT_ITER_CONT;
1961 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1966 rc = llapi_layout_comp_id_get(layout, &id);
1970 if (id == *(__u32 *)cbdata)
1971 return LLAPI_LAYOUT_ITER_STOP;
1973 return LLAPI_LAYOUT_ITER_CONT;
1976 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
1978 char buf[LOV_MAXPOOLNAME + 1];
1979 struct pool_to_id_cbdata *d = (void *)cbdata;
1983 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1986 if (strcmp(d->pool, buf))
1987 return LLAPI_LAYOUT_ITER_CONT;
1989 rc = llapi_layout_mirror_id_get(layout, &id);
1994 return LLAPI_LAYOUT_ITER_STOP;
1997 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
1999 char buf[LOV_MAXPOOLNAME + 1];
2000 struct pool_to_id_cbdata *d = (void *)cbdata;
2004 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2007 if (strcmp(d->pool, buf))
2008 return LLAPI_LAYOUT_ITER_CONT;
2010 rc = llapi_layout_comp_id_get(layout, &id);
2015 return LLAPI_LAYOUT_ITER_STOP;
2018 struct collect_ids_data {
2024 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2026 struct collect_ids_data *cid = cbdata;
2030 rc = llapi_layout_mirror_id_get(layout, &id);
2034 if ((__u16)id != cid->cid_exclude) {
2037 for (i = 0; i < cid->cid_count; i++) {
2038 /* already collected the mirror id */
2039 if (id == cid->cid_ids[i])
2040 return LLAPI_LAYOUT_ITER_CONT;
2042 cid->cid_ids[cid->cid_count] = id;
2046 return LLAPI_LAYOUT_ITER_CONT;
2050 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2051 * @mirror_id: Mirror id to be checked.
2052 * @layout: Mirror component list.
2054 * This function checks if a mirror with specified @mirror_id is the last
2055 * non-stale mirror of a layout @layout.
2057 * Return: true or false.
2060 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2062 __u16 mirror_ids[128] = { 0 };
2063 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2065 .cid_exclude = mirror_id, };
2068 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2070 for (i = 0; i < cid.cid_count; i++) {
2071 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2074 comp_size = llapi_mirror_find_stale(layout, comp_array,
2075 ARRAY_SIZE(comp_array),
2084 static int mirror_split(const char *fname, __u32 id, const char *pool,
2085 enum mirror_flags mflags, const char *victim_file)
2087 struct llapi_layout *layout;
2088 char parent[PATH_MAX];
2089 char victim[PATH_MAX];
2090 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2092 struct ll_ioc_lease *data;
2093 uint16_t mirror_count;
2097 bool purge = true; /* delete mirror by setting fdv=fd */
2101 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2103 "error %s: the source file '%s' and -f file are the same\n",
2108 /* check fname contains mirror with mirror_id/comp_id */
2109 layout = llapi_layout_get_by_path(fname, 0);
2112 "error %s: file '%s' couldn't get layout\n",
2117 rc = llapi_layout_sanity(layout, fname, false, true);
2119 llapi_layout_sanity_perror(rc);
2123 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2126 "error %s: file '%s' couldn't get mirror count\n",
2130 if (mirror_count < 2) {
2132 "error %s: file '%s' has %d component, cannot split\n",
2133 progname, fname, mirror_count);
2137 if (mflags & MF_COMP_POOL) {
2138 struct pool_to_id_cbdata data = { .pool = pool };
2140 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2142 mirror_id = data.id;
2143 } else if (mflags & MF_COMP_ID) {
2144 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2145 mirror_id = mirror_id_of(id);
2147 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2151 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2154 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2155 if (mflags & MF_COMP_POOL) {
2157 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2158 progname, fname, pool);
2160 } else if (mflags & MF_COMP_ID) {
2162 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2163 progname, fname, id);
2167 "error %s: file '%s' does not contain mirror with id %u\n",
2168 progname, fname, id);
2173 if (!victim_file && mflags & MF_DESTROY)
2174 /* Allow mirror split even without the key on encrypted files,
2175 * and in this case of a 'split -d', open file with O_DIRECT
2176 * (no IOs will be done).
2178 fd = open(fname, O_RDWR | O_DIRECT | O_FILE_ENC);
2180 fd = open(fname, O_RDWR);
2184 "error %s: open file '%s' failed: %s\n",
2185 progname, fname, strerror(errno));
2189 /* get victim file directory pathname */
2190 if (strlen(fname) > sizeof(parent) - 1) {
2191 fprintf(stderr, "error %s: file name of '%s' too long\n",
2196 strncpy(parent, fname, sizeof(parent));
2197 ptr = strrchr(parent, '/');
2199 if (!getcwd(parent, sizeof(parent))) {
2200 fprintf(stderr, "error %s: getcwd failed: %s\n",
2201 progname, strerror(errno));
2211 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2213 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2218 rc = llapi_file_is_encrypted(fd);
2220 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2221 progname, fname, rc);
2228 /* use a temp file to store the splitted layout */
2229 if (mflags & MF_DESTROY) {
2230 char file_path[PATH_MAX];
2231 unsigned int rnumber;
2234 if (last_non_stale_mirror(mirror_id, layout)) {
2237 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2243 /* don't use volatile file for mirror destroy */
2247 * try the old way to delete mirror using
2252 rc = snprintf(file_path,
2254 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2258 rc >= sizeof(file_path)) {
2259 fdv = -ENAMETOOLONG;
2263 open_flags = O_RDWR |
2264 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2265 O_CREAT | O_EXCL | O_NOFOLLOW |
2266 /* O_DIRECT for mirror split -d */
2268 /* Allow split without the key */
2270 fdv = open(file_path, open_flags,
2274 } while (fdv < 0 && rc == -EEXIST);
2280 "error %s: not permitted on encrypted file '%s': %d\n",
2281 progname, fname, rc);
2285 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2287 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2290 /* user specified victim file */
2294 "error %s: not permitted on encrypted file '%s': %d\n",
2295 progname, fname, rc);
2298 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2303 "error %s: create victim file failed: %s\n",
2304 progname, strerror(errno));
2308 /* get lease lock of fname */
2309 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2312 "error %s: cannot get lease of file '%s': %d\n",
2313 progname, fname, rc);
2317 /* Atomatically put lease, split layouts and close. */
2318 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2324 data->lil_mode = LL_LEASE_UNLCK;
2325 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2326 data->lil_count = 2;
2327 data->lil_ids[0] = fdv;
2328 data->lil_ids[1] = mirror_id;
2329 rc = llapi_lease_set(fd, data);
2331 if (rc == -EINVAL && purge) {
2332 /* could be old MDS which prohibit fd==fdv */
2337 if (rc == 0) /* lost lease lock */
2340 "error %s: cannot split '%s': %s\n",
2341 progname, fname, strerror(-rc));
2353 llapi_layout_free(layout);
2358 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2359 __u16 *mirror_ids, int ids_nr);
2361 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2362 __u64 migration_flags)
2364 struct ll_ioc_lease *data = NULL;
2367 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2369 error_loc = "cannot get lease";
2373 rc = migrate_nonblock(fd, fdv);
2377 /* Atomically put lease, merge layouts, resync and close. */
2378 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2380 error_loc = "memory allocation";
2383 data->lil_mode = LL_LEASE_UNLCK;
2384 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2385 data->lil_count = 1;
2386 data->lil_ids[0] = fdv;
2387 rc = llapi_lease_set(fd, data);
2389 error_loc = "cannot merge layout";
2391 } else if (rc == 0) {
2393 error_loc = "lost lease lock";
2399 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2401 error_loc = "cannot resync file";
2405 /* delete first mirror now */
2406 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2408 error_loc = "cannot delete old layout";
2412 llapi_lease_release(fd);
2418 fprintf(stderr, "error: %s: %s: %s: %s\n",
2419 progname, name, error_loc, strerror(-rc));
2420 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2421 printf("%s\n", name);
2428 * Parse a string containing an target index list into an array of integers.
2430 * The input string contains a comma delimited list of individual
2431 * indices and ranges, for example "1,2-4,7". Add the indices into the
2432 * \a tgts array and remove duplicates.
2434 * \param[out] tgts array to store indices in
2435 * \param[in] size size of \a tgts array
2436 * \param[in] offset starting index in \a tgts
2437 * \param[in] arg string containing OST index list
2438 * \param[in/out] overstriping index list may contain duplicates
2440 * \retval positive number of indices in \a tgts
2441 * \retval -EINVAL unable to parse \a arg
2443 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2444 unsigned long long *pattern)
2448 int slots = size - offset;
2450 bool overstriped = false;
2456 end_of_loop = false;
2457 while (!end_of_loop) {
2458 int start_index = 0;
2461 char *endptr = NULL;
2465 ptr = strchrnul(arg, ',');
2467 end_of_loop = *ptr == '\0';
2471 start_index = strtol(arg, &endptr, 0);
2472 if (endptr == arg) /* no data at all */
2474 if (errno != 0 || start_index < -1 ||
2475 (*endptr != '-' && *endptr != '\0'))
2478 end_index = start_index;
2479 if (*endptr == '-') {
2481 end_index = strtol(endptr + 1, &endptr, 0);
2482 if (errno != 0 || *endptr != '\0' || end_index < -1)
2484 if (end_index < start_index)
2488 for (i = start_index; i <= end_index && slots > 0; i++) {
2491 /* remove duplicate */
2492 for (j = 0; j < offset; j++) {
2493 if (tgts[j] == i && pattern &&
2494 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2496 else if (tgts[j] == i)
2502 if (j == offset) { /* check complete */
2508 if (slots == 0 && i < end_index)
2516 if (!end_of_loop && ptr)
2519 if (!overstriped && pattern)
2520 *pattern = LLAPI_LAYOUT_DEFAULT;
2522 return rc < 0 ? rc : nr;
2525 struct lfs_setstripe_args {
2526 unsigned long long lsa_comp_end;
2527 unsigned long long lsa_stripe_size;
2528 unsigned long long lsa_extension_size;
2529 long long lsa_stripe_count;
2530 long long lsa_stripe_off;
2531 __u32 lsa_comp_flags;
2532 __u32 lsa_comp_neg_flags;
2533 unsigned long long lsa_pattern;
2534 unsigned int lsa_mirror_count;
2536 bool lsa_first_comp;
2537 bool lsa_extension_comp;
2539 char *lsa_pool_name;
2542 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2544 unsigned int mirror_count = lsa->lsa_mirror_count;
2545 bool first_comp = lsa->lsa_first_comp;
2547 memset(lsa, 0, sizeof(*lsa));
2549 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2550 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2551 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2552 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2553 lsa->lsa_pool_name = NULL;
2555 lsa->lsa_mirror_count = mirror_count;
2556 lsa->lsa_first_comp = first_comp;
2560 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2561 * @lsa: Stripe options to be initialized and inherited.
2563 * This function initializes stripe options in @lsa and inherit
2564 * stripe_size, stripe_count and OST pool_name options.
2568 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2570 unsigned long long stripe_size;
2571 long long stripe_count;
2572 char *pool_name = NULL;
2574 stripe_size = lsa->lsa_stripe_size;
2575 stripe_count = lsa->lsa_stripe_count;
2576 pool_name = lsa->lsa_pool_name;
2578 setstripe_args_init(lsa);
2580 lsa->lsa_stripe_size = stripe_size;
2581 lsa->lsa_stripe_count = stripe_count;
2582 lsa->lsa_pool_name = pool_name;
2585 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2587 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2588 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2589 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2590 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2591 lsa->lsa_comp_end != 0);
2594 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2596 if (lsa->lsa_nr_tgts) {
2597 if (lsa->lsa_nr_tgts < 0 ||
2598 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2599 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2605 if (lsa->lsa_stripe_count > 0 &&
2606 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2607 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2608 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2609 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2610 lsa->lsa_stripe_count,
2622 * comp_args_to_layout() - Create or extend a composite layout.
2623 * @composite: Pointer to the composite layout.
2624 * @lsa: Stripe options for the new component.
2626 * This function creates or extends a composite layout by adding a new
2627 * component with stripe options from @lsa.
2629 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2631 * Return: 0 on success or an error code on failure.
2633 static int comp_args_to_layout(struct llapi_layout **composite,
2634 struct lfs_setstripe_args *lsa,
2637 struct llapi_layout *layout = *composite;
2638 uint64_t prev_end = 0;
2644 layout = llapi_layout_alloc();
2646 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2651 *composite = layout;
2652 lsa->lsa_first_comp = true;
2657 * Get current component extent, current component
2658 * must be the tail component.
2660 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2662 fprintf(stderr, "Get comp extent failed. %s\n",
2667 if (lsa->lsa_first_comp) {
2669 rc = llapi_layout_add_first_comp(layout);
2671 rc = llapi_layout_comp_add(layout);
2674 fprintf(stderr, "Add component failed. %s\n",
2680 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2682 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2683 lsa->lsa_comp_flags, strerror(errno));
2688 uint64_t comp_end = lsa->lsa_comp_end;
2691 * The extendable component is 0-length, so it can be removed
2692 * if there is insufficient space to extend it.
2694 if (lsa->lsa_extension_comp)
2695 comp_end = prev_end;
2697 rc = llapi_layout_comp_extent_set(layout, prev_end,
2700 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2701 prev_end, comp_end, strerror(errno));
2705 /* reset lsa_first_comp */
2706 lsa->lsa_first_comp = false;
2708 /* Data-on-MDT component setting */
2709 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2711 if (lsa->lsa_stripe_count == 0)
2712 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2713 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2714 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2715 if (lsa->lsa_stripe_off == -1 ||
2716 lsa->lsa_stripe_off == 0)
2717 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2719 * In case of Data-on-MDT patterns the only extra option
2720 * applicable is stripe size option.
2722 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2724 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2725 lsa->lsa_stripe_count);
2729 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2730 lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2732 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2733 lsa->lsa_stripe_size);
2737 if (lsa->lsa_nr_tgts != 0) {
2739 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2744 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2746 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2747 lsa->lsa_stripe_off);
2751 if (lsa->lsa_pool_name != 0) {
2753 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2754 lsa->lsa_pool_name);
2759 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2761 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2766 /* Data-on-MDT component has always single stripe up to end */
2767 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2768 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2769 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2771 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2778 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2779 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2781 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2782 rc = llapi_layout_extension_size_set(layout, size);
2784 rc = llapi_layout_stripe_size_set(layout, size);
2787 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2788 size, strerror(errno));
2792 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2794 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2795 lsa->lsa_stripe_count, strerror(errno));
2799 if (lsa->lsa_pool_name) {
2800 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2802 fprintf(stderr, "Set pool name: %s failed. %s\n",
2803 lsa->lsa_pool_name, strerror(errno));
2807 rc = llapi_layout_pool_name_set(layout, "");
2809 fprintf(stderr, "Clear pool name failed: %s\n",
2815 rc = lsa_args_stripe_count_check(lsa);
2819 if (lsa->lsa_nr_tgts > 0) {
2822 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2823 rc = llapi_layout_ost_index_set(layout, i,
2828 /* Make sure stripe offset is in OST list. */
2829 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2833 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2834 lsa->lsa_stripe_off);
2838 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2839 lsa->lsa_stripe_off != -1) {
2840 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2843 fprintf(stderr, "Set ost index %d failed. %s\n",
2844 i, strerror(errno));
2848 /* Create the second, virtual component of extension space */
2849 if (lsa->lsa_extension_comp) {
2850 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2851 lsa->lsa_extension_comp = false;
2858 static int build_component(struct llapi_layout **layout,
2859 struct lfs_setstripe_args *lsa, bool set_extent)
2863 rc = comp_args_to_layout(layout, lsa, set_extent);
2867 if (lsa->lsa_mirror_count > 0) {
2868 rc = llapi_layout_mirror_count_set(*layout,
2869 lsa->lsa_mirror_count);
2873 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2876 lsa->lsa_mirror_count = 0;
2882 static int build_prev_component(struct llapi_layout **layout,
2883 struct lfs_setstripe_args *prev,
2884 struct lfs_setstripe_args *lsa,
2887 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2890 if (prev->lsa_stripe_size) {
2892 prev->lsa_comp_end = lsa->lsa_comp_end;
2893 prev->lsa_extension_size = lsa->lsa_extension_size;
2894 prev->lsa_extension_comp = true;
2897 rc = build_component(layout, prev, true);
2903 * Copy lsa to previous lsa;
2904 * if this is an extension component, make the previous invalid;
2907 prev->lsa_stripe_size = 0;
2914 #ifndef LCME_TEMPLATE_FLAGS
2915 #define LCME_TEMPLATE_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
2919 static int build_layout_from_yaml_node(struct cYAML *node,
2920 struct llapi_layout **layout,
2921 struct lfs_setstripe_args *lsa,
2922 struct lfs_setstripe_args *prevp)
2924 struct lfs_setstripe_args prev = { 0 };
2925 __u32 *osts = lsa->lsa_tgts;
2933 string = node->cy_string;
2935 if (node->cy_type == CYAML_TYPE_OBJECT) {
2936 /* go deep to sub blocks */
2937 if (string && !strncmp(string, "component", 9) &&
2938 strncmp(string, "component0", 10) &&
2939 strncmp(string, "components", 10)) {
2940 rc = build_prev_component(layout, prevp, lsa,
2945 /* initialize lsa. */
2946 setstripe_args_init(lsa);
2947 lsa->lsa_first_comp = false;
2948 lsa->lsa_tgts = osts;
2951 rc = build_layout_from_yaml_node(node->cy_child, layout,
2956 if (!node->cy_string)
2959 /* skip leading lmm_ if present, to simplify parsing */
2960 if (strncmp(string, "lmm_", 4) == 0)
2963 if (node->cy_type == CYAML_TYPE_STRING) {
2964 if (!strcmp(string, "lcme_extent.e_end")) {
2965 if (!strcmp(node->cy_valuestring, "EOF") ||
2966 !strcmp(node->cy_valuestring, "eof"))
2967 lsa->lsa_comp_end = LUSTRE_EOF;
2968 } else if (!strcmp(string, "pool")) {
2969 lsa->lsa_pool_name = node->cy_valuestring;
2970 } else if (!strcmp(string, "pattern")) {
2971 if (!strcmp(node->cy_valuestring, "mdt"))
2972 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2973 if (!strcmp(node->cy_valuestring,
2974 "raid0,overstriped"))
2976 LLAPI_LAYOUT_OVERSTRIPING;
2977 } else if (!strcmp(string, "lcme_flags")) {
2978 rc = comp_str2flags(node->cy_valuestring,
2979 &lsa->lsa_comp_flags,
2980 &lsa->lsa_comp_neg_flags);
2984 * Only template flags have meaning in
2985 * the layout for a new file
2987 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2989 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2990 if (!strcmp(string, "lcm_mirror_count")) {
2991 lsa->lsa_mirror_count = node->cy_valueint;
2992 } else if (!strcmp(string, "lcme_extent.e_start")) {
2993 if (node->cy_valueint == 0)
2994 lsa->lsa_first_comp = true;
2995 } else if (!strcmp(string, "lcme_extent.e_end")) {
2996 if (node->cy_valueint == -1)
2997 lsa->lsa_comp_end = LUSTRE_EOF;
2999 lsa->lsa_comp_end = node->cy_valueint;
3000 } else if (!strcmp(string, "stripe_count")) {
3001 lsa->lsa_stripe_count = node->cy_valueint;
3002 } else if (!strcmp(string, "stripe_size")) {
3003 lsa->lsa_stripe_size = node->cy_valueint;
3004 } else if (!strcmp(string, "extension_size")) {
3005 lsa->lsa_extension_size = node->cy_valueint;
3006 lsa->lsa_extension_comp = true;
3007 } else if (!strcmp(string, "stripe_offset")) {
3008 lsa->lsa_stripe_off = node->cy_valueint;
3009 } else if (!strcmp(string, "l_ost_idx")) {
3010 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3015 node = node->cy_next;
3018 if (prevp == &prev) {
3019 rc = build_prev_component(layout, prevp, lsa, true);
3023 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3024 rc = build_component(layout, lsa, *layout != NULL);
3030 static int lfs_comp_create_from_yaml(char *template,
3031 struct llapi_layout **layout,
3032 struct lfs_setstripe_args *lsa,
3035 struct cYAML *tree = NULL, *err_rc = NULL;
3038 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3040 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3041 progname, template);
3042 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3043 "can't parse", &err_rc);
3044 cYAML_print_tree2file(stderr, err_rc);
3045 cYAML_free_tree(err_rc);
3050 /* initialize lsa for plain file */
3051 setstripe_args_init(lsa);
3052 lsa->lsa_tgts = osts;
3054 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3056 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3057 progname, template);
3060 /* clean clean lsa */
3061 setstripe_args_init(lsa);
3065 cYAML_free_tree(tree);
3070 * Get the extension size from the next (SEL) component and extend the
3071 * current component on it. The start of the next component is to be
3074 * \param[in] layout the current layout
3075 * \param[in] start the start of the current component
3076 * \param[in,out] end the end of the current component
3077 * \param[in] offset the offset to adjust the end position to instead of
3080 * \retval 0 - extended successfully
3081 * \retval < 0 - error
3083 static int layout_extend_comp(struct llapi_layout *layout,
3084 uint64_t start, uint64_t *end,
3087 uint64_t size, next_start, next_end;
3090 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3093 "%s setstripe: cannot move component cursor: %s\n",
3094 progname, strerror(errno));
3099 * Even if the @size will not be used below, this will fail if
3100 * this is not a SEL component - a good confirmation we are
3101 * working on right components.
3103 rc = llapi_layout_extension_size_get(layout, &size);
3106 "%s setstripe: cannot get component ext size: %s\n",
3107 progname, strerror(errno));
3111 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3113 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3114 progname, strerror(errno));
3118 next_start += offset ?: size;
3119 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3121 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3122 progname, strerror(errno));
3126 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3129 "%s setstripe: cannot move component cursor: %s\n",
3130 progname, strerror(errno));
3134 *end += offset ?: size;
3135 rc = llapi_layout_comp_extent_set(layout, start, *end);
3137 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3138 progname, strerror(errno));
3146 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3147 * end of the last component in the existing file, and adjust the
3148 * first extent start of the components to be added accordingly.
3150 * In the create mode, we need to check if the first component is an extendable
3151 * SEL component and extend its length to the extension size (first component
3152 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3154 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3157 struct llapi_layout *head;
3158 uint64_t start = 0, prev_end = 0;
3162 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3167 head = llapi_layout_get_by_path(fname, 0);
3170 "%s setstripe: cannot read layout from '%s': %s\n",
3171 progname, fname, strerror(errno));
3173 } else if (errno == ENODATA) {
3175 * file without LOVEA, this component-add will be turned
3176 * into a component-create.
3178 llapi_layout_free(head);
3182 * the new layout will be added to an empty one, it
3183 * still needs to be adjusted below
3187 } else if (!llapi_layout_is_composite(head)) {
3189 "%s setstripe: '%s' not a composite file\n",
3191 llapi_layout_free(head);
3195 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3198 "%s setstripe: cannot get prev extent: %s\n",
3199 progname, strerror(errno));
3200 llapi_layout_free(head);
3204 llapi_layout_free(head);
3208 /* Make sure we use the first component of the layout to be added. */
3209 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3212 "%s setstripe: cannot move component cursor: %s\n",
3213 progname, strerror(errno));
3217 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3219 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3220 progname, strerror(errno));
3224 if (start == 0 && end == 0) {
3225 rc = layout_extend_comp(layout, start, &end,
3226 comp_add ? prev_end : 0);
3231 if (start > prev_end || end < prev_end) {
3233 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3234 progname, start, end, prev_end);
3238 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3241 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3242 progname, prev_end, end, strerror(errno));
3249 static int mirror_adjust_first_extents(struct mirror_args *list)
3257 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3260 list = list->m_next;
3266 static inline bool arg_is_eof(char *arg)
3268 return !strncmp(arg, "-1", strlen("-1")) ||
3269 !strncmp(arg, "EOF", strlen("EOF")) ||
3270 !strncmp(arg, "eof", strlen("eof"));
3274 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3276 * Return: Valid mirror_args pointer on success and
3277 * NULL if memory allocation fails.
3279 static struct mirror_args *lfs_mirror_alloc(void)
3281 struct mirror_args *mirror = NULL;
3284 mirror = calloc(1, sizeof(*mirror));
3286 mirror->m_inherit = false;
3297 * lfs_mirror_free() - Free memory allocated for a mirror argument
3299 * @mirror: Previously allocated mirror argument structure by
3300 * lfs_mirror_alloc().
3302 * Free memory allocated for @mirror.
3306 static void lfs_mirror_free(struct mirror_args *mirror)
3308 if (mirror->m_layout)
3309 llapi_layout_free(mirror->m_layout);
3314 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3315 * @mirror_list: Previously allocated mirror list.
3317 * Free memory allocated for @mirror_list.
3321 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3323 struct mirror_args *next_mirror = NULL;
3325 while (mirror_list) {
3326 next_mirror = mirror_list->m_next;
3327 lfs_mirror_free(mirror_list);
3328 mirror_list = next_mirror;
3340 LFS_COMP_NO_VERIFY_OPT,
3342 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3344 LFS_MIRROR_STATE_OPT,
3346 LFS_MIRROR_INDEX_OPT,
3347 LFS_LAYOUT_FOREIGN_OPT,
3354 #ifndef LCME_USER_MIRROR_FLAGS
3355 /* The mirror flags can be set by users at creation time. */
3356 #define LCME_USER_MIRROR_FLAGS (LCME_FL_PREF_RW)
3360 static int lfs_setstripe_internal(int argc, char **argv,
3361 enum setstripe_origin opc)
3363 struct lfs_setstripe_args lsa = { 0 };
3364 struct llapi_stripe_param *param = NULL;
3365 struct find_param migrate_mdt_param = {
3375 unsigned long long size_units = 1;
3376 bool migrate_mode = false;
3377 bool migrate_mdt_mode = false;
3378 bool setstripe_mode = false;
3379 bool migration_block = false;
3380 __u64 migration_flags = 0;
3381 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3382 int comp_del = 0, comp_set = 0;
3385 struct llapi_layout *layout = NULL;
3386 struct llapi_layout **lpp = &layout;
3387 bool mirror_mode = false;
3388 bool has_m_file = false;
3389 __u32 mirror_count = 0;
3390 enum mirror_flags mirror_flags = 0;
3391 struct mirror_args *mirror_list = NULL;
3392 struct mirror_args *new_mirror = NULL;
3393 struct mirror_args *last_mirror = NULL;
3394 __u16 mirror_id = 0;
3396 bool from_yaml = false;
3397 bool from_copy = false;
3398 char *template = NULL;
3399 bool foreign_mode = false;
3401 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3402 char *mode_opt = NULL;
3403 mode_t previous_umask = 0;
3404 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3406 struct option long_opts[] = {
3407 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3408 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3409 /* --block is only valid in migrate mode */
3410 { .val = 'b', .name = "block", .has_arg = no_argument },
3411 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3412 { .val = LFS_COMP_ADD_OPT,
3413 .name = "comp-add", .has_arg = no_argument },
3414 { .val = LFS_COMP_ADD_OPT,
3415 .name = "component-add", .has_arg = no_argument },
3416 { .val = LFS_COMP_DEL_OPT,
3417 .name = "comp-del", .has_arg = no_argument },
3418 { .val = LFS_COMP_DEL_OPT,
3419 .name = "component-del", .has_arg = no_argument },
3420 { .val = LFS_COMP_FLAGS_OPT,
3421 .name = "comp-flags", .has_arg = required_argument },
3422 { .val = LFS_COMP_FLAGS_OPT,
3423 .name = "component-flags",
3424 .has_arg = required_argument },
3425 { .val = LFS_COMP_SET_OPT,
3426 .name = "comp-set", .has_arg = no_argument },
3427 { .val = LFS_COMP_SET_OPT,
3428 .name = "component-set",
3429 .has_arg = no_argument},
3430 { .val = LFS_COMP_NO_VERIFY_OPT,
3431 .name = "no-verify", .has_arg = no_argument},
3432 { .val = LFS_LAYOUT_FLAGS_OPT,
3433 .name = "flags", .has_arg = required_argument},
3434 { .val = LFS_LAYOUT_FOREIGN_OPT,
3435 .name = "foreign", .has_arg = optional_argument},
3436 { .val = LFS_MIRROR_ID_OPT,
3437 .name = "mirror-id", .has_arg = required_argument},
3438 { .val = LFS_MODE_OPT,
3439 .name = "mode", .has_arg = required_argument},
3440 { .val = LFS_LAYOUT_COPY,
3441 .name = "copy", .has_arg = required_argument},
3442 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3443 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3444 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3445 { .val = 'C', .name = "overstripe-count",
3446 .has_arg = required_argument},
3447 { .val = 'd', .name = "delete", .has_arg = no_argument},
3448 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3449 /* used with "lfs migrate -m" */
3450 { .val = 'd', .name = "directory", .has_arg = no_argument},
3451 /* --non-direct is only valid in migrate mode */
3452 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3453 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3454 { .val = 'E', .name = "component-end",
3455 .has_arg = required_argument},
3456 { .val = 'f', .name = "file", .has_arg = required_argument },
3457 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3458 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3459 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3460 { .val = 'h', .name = "help", .has_arg = no_argument },
3461 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3462 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3463 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3464 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3465 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3466 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3467 { .val = 'L', .name = "layout", .has_arg = required_argument },
3468 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3469 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3470 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3471 /* --non-block is only valid in migrate mode */
3472 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3473 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3474 { .val = 'o', .name = "ost", .has_arg = required_argument },
3475 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3476 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3477 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3479 { .val = 'p', .name = "pool", .has_arg = required_argument },
3480 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3481 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3482 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3483 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3484 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3485 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3486 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3487 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3488 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3489 /* --verbose is only valid in migrate mode */
3490 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3491 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3492 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3493 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3494 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3495 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3498 setstripe_args_init(&lsa);
3500 migrate_mode = (opc == SO_MIGRATE);
3501 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3502 setstripe_mode = (opc == SO_SETSTRIPE);
3503 if (opc == SO_MIRROR_DELETE) {
3505 mirror_flags = MF_DESTROY;
3508 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3510 while ((c = getopt_long(argc, argv,
3511 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:y:z:",
3512 long_opts, NULL)) >= 0) {
3518 case LFS_COMP_ADD_OPT:
3521 case LFS_COMP_DEL_OPT:
3524 case LFS_COMP_FLAGS_OPT:
3525 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3526 &lsa.lsa_comp_neg_flags);
3529 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3531 "%s: inverted flags are not supported\n",
3536 case LFS_COMP_SET_OPT:
3539 case LFS_COMP_NO_VERIFY_OPT:
3540 mirror_flags |= MF_NO_VERIFY;
3542 case LFS_MIRROR_ID_OPT: {
3543 unsigned long int id;
3546 id = strtoul(optarg, &end, 0);
3547 if (errno != 0 || *end != '\0' || id == 0 ||
3550 "%s %s: invalid mirror ID '%s'\n",
3551 progname, argv[0], optarg);
3555 mirror_id = (__u16)id;
3558 case LFS_LAYOUT_FLAGS_OPT: {
3561 /* check for numeric flags (foreign and mirror cases) */
3562 if (setstripe_mode && !mirror_mode && !last_mirror) {
3564 flags = strtoul(optarg, &end, 16);
3565 if (errno != 0 || *end != '\0' ||
3566 flags >= UINT32_MAX) {
3568 "%s %s: invalid hex flags '%s'\n",
3569 progname, argv[0], optarg);
3572 if (!foreign_mode) {
3574 "%s %s: hex flags must be specified with --foreign option\n",
3581 if (!mirror_mode || !last_mirror) {
3583 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3588 result = comp_str2flags(optarg, &last_mirror->m_flags,
3595 "%s: inverted flags are not supported\n",
3600 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3602 "%s: unsupported mirror flags: %s\n",
3609 case LFS_LAYOUT_FOREIGN_OPT:
3611 /* check pure numeric */
3612 type = strtoul(optarg, &end, 0);
3615 type = check_foreign_type_name(optarg);
3616 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3618 "%s %s: unrecognized foreign type '%s'\n",
3623 } else if (type >= UINT32_MAX) {
3625 "%s %s: invalid foreign type '%s'\n",
3626 progname, argv[0], optarg);
3630 foreign_mode = true;
3635 mode = strtoul(mode_opt, &end, 8);
3638 "%s %s: bad mode '%s'\n",
3639 progname, argv[0], mode_opt);
3642 previous_umask = umask(0);
3645 case LFS_LAYOUT_COPY:
3650 if (!migrate_mode) {
3652 "%s %s: -b|--block valid only for migrate command\n",
3656 migration_block = true;
3659 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3661 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3665 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3669 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3670 if (errno != 0 || *end != '\0'||
3671 lsa.lsa_stripe_count < -1 ||
3672 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3674 "%s %s: invalid stripe count '%s'\n",
3675 progname, argv[0], optarg);
3679 if (lsa.lsa_stripe_count == -1)
3680 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3684 migrate_mdt_param.fp_max_depth = 1;
3686 /* delete the default striping pattern */
3688 if (opc == SO_MIRROR_SPLIT) {
3691 "%s %s: -d cannot used with -f\n",
3695 mirror_flags |= MF_DESTROY;
3700 if (!migrate_mode) {
3702 "%s %s: -D|--non-direct is valid only for migrate command\n",
3706 migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3709 if (lsa.lsa_comp_end != 0) {
3710 result = comp_args_to_layout(lpp, &lsa, true);
3712 fprintf(stderr, "%s: invalid layout\n",
3717 setstripe_args_init_inherit(&lsa);
3720 if (arg_is_eof(optarg)) {
3721 lsa.lsa_comp_end = LUSTRE_EOF;
3723 result = llapi_parse_size(optarg,
3726 /* assume units of KB if too small */
3727 if (lsa.lsa_comp_end < 4096)
3728 lsa.lsa_comp_end *= 1024;
3730 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3732 "%s %s: invalid component end '%s'\n",
3733 progname, argv[0], optarg);
3739 if (!migrate_mode) {
3741 "--mdt-hash is valid only for migrate command\n");
3745 lsa.lsa_pattern = check_hashtype(optarg);
3746 if (lsa.lsa_pattern == 0) {
3748 "%s %s: bad stripe hash type '%s'\n",
3749 progname, argv[0], optarg);
3755 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3756 if (errno != 0 || *end != '\0' ||
3757 lsa.lsa_stripe_off < -1 ||
3758 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3760 "%s %s: invalid stripe offset '%s'\n",
3761 progname, argv[0], optarg);
3764 if (lsa.lsa_stripe_off == -1)
3765 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3768 comp_id = strtoul(optarg, &end, 0);
3769 if (*end != '\0' || comp_id == 0 ||
3770 comp_id > LCME_ID_MAX) {
3772 "%s %s: invalid component ID '%s'\n",
3773 progname, argv[0], optarg);
3778 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3780 "error: %s: invalid option: %s\n",
3781 progname, argv[optopt + 1]);
3784 if (opc == SO_MIRROR_EXTEND) {
3787 "error: %s: '-N' must exist in front of '%s'\n",
3788 progname, argv[optopt + 1]);
3791 last_mirror->m_file = optarg;
3792 last_mirror->m_count = 1;
3796 mirror_list = lfs_mirror_alloc();
3797 mirror_list->m_file = optarg;
3802 if (strcmp(argv[optind - 1], "mdt") == 0) {
3803 /* Can be only the first component */
3807 "error: 'mdt' layout can be only the first one\n");
3810 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3813 "error: 'mdt' layout size is too big\n");
3816 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3817 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
3818 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3821 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3827 if (!migrate_mode) {
3829 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3833 migrate_mdt_mode = true;
3834 lsa.lsa_nr_tgts = parse_targets(tgts,
3835 sizeof(tgts) / sizeof(__u32),
3836 lsa.lsa_nr_tgts, optarg, NULL);
3837 if (lsa.lsa_nr_tgts < 0) {
3839 "%s: invalid MDT target(s) '%s'\n",
3844 lsa.lsa_tgts = tgts;
3845 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3846 lsa.lsa_stripe_off = tgts[0];
3849 if (!migrate_mode) {
3851 "%s %s: -n|--non-block valid only for migrate command\n",
3855 migration_flags |= LLAPI_MIGRATION_NONBLOCK;
3858 if (opc == SO_SETSTRIPE) {
3859 opc = SO_MIRROR_CREATE;
3865 mirror_count = strtoul(optarg, &end, 0);
3866 if (errno != 0 || *end != '\0' ||
3867 mirror_count == 0 ||
3868 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3870 "error: %s: bad mirror count: %s\n",
3877 new_mirror = lfs_mirror_alloc();
3878 new_mirror->m_count = mirror_count;
3881 mirror_list = new_mirror;
3884 /* wrap up last mirror */
3885 if (!setstripe_args_specified(&lsa))
3886 last_mirror->m_inherit = true;
3887 if (lsa.lsa_comp_end == 0)
3888 lsa.lsa_comp_end = LUSTRE_EOF;
3890 result = comp_args_to_layout(lpp, &lsa, true);
3892 lfs_mirror_free(new_mirror);
3896 setstripe_args_init_inherit(&lsa);
3898 last_mirror->m_next = new_mirror;
3901 last_mirror = new_mirror;
3902 lpp = &last_mirror->m_layout;
3905 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3906 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3908 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3910 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3912 "%s %s: -o|--ost incompatible with DoM layout\n",
3917 * -o allows overstriping, and must note it because
3918 * parse_targets is shared with MDT striping, which
3919 * does not allow duplicates
3921 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3922 lsa.lsa_nr_tgts = parse_targets(tgts,
3923 sizeof(tgts) / sizeof(__u32),
3924 lsa.lsa_nr_tgts, optarg,
3926 if (lsa.lsa_nr_tgts < 0) {
3928 "%s %s: invalid OST target(s) '%s'\n",
3929 progname, argv[0], optarg);
3933 lsa.lsa_tgts = tgts;
3934 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3935 lsa.lsa_stripe_off = tgts[0];
3940 lsa.lsa_pool_name = optarg;
3942 if (strlen(lsa.lsa_pool_name) == 0 ||
3943 strncmp(lsa.lsa_pool_name, "none",
3944 LOV_MAXPOOLNAME) == 0)
3945 lsa.lsa_pool_name = NULL;
3948 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3950 /* assume units of KB if too small to be valid */
3951 if (lsa.lsa_stripe_size < 4096)
3952 lsa.lsa_stripe_size *= 1024;
3954 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3956 "%s %s: invalid stripe size '%s'\n",
3957 progname, argv[0], optarg);
3962 if (!migrate_mode) {
3964 "%s %s: -v|--verbose valid only for migrate command\n",
3968 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3969 migration_flags = LLAPI_MIGRATION_VERBOSE;
3979 result = llapi_parse_size(optarg,
3980 &lsa.lsa_extension_size,
3984 "%s %s: invalid extension size '%s'\n",
3985 progname, argv[0], optarg);
3989 lsa.lsa_extension_comp = true;
3992 fprintf(stderr, "%s: unrecognized option '%s'\n",
3993 progname, argv[optind - 1]);
3999 fname = argv[optind];
4001 if (optind == argc) {
4002 fprintf(stderr, "%s %s: FILE must be specified\n",
4007 /* lfs migrate $filename should keep the file's layout by default */
4008 if (migrate_mode && !layout && !from_yaml &&
4009 !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4012 if (xattr && !foreign_mode) {
4014 * only print a warning as this is harmless and will be ignored
4017 "%s %s: xattr has been specified for non-foreign layout\n",
4019 } else if (foreign_mode && !xattr) {
4021 "%s %s: xattr must be provided in foreign mode\n",
4026 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4027 comp_set || comp_id || delete || from_copy ||
4028 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4031 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4036 if (mirror_mode && mirror_count == 0) {
4038 "error: %s: --mirror-count|-N option is required\n",
4045 if (!setstripe_args_specified(&lsa))
4046 last_mirror->m_inherit = true;
4047 if (lsa.lsa_comp_end == 0)
4048 lsa.lsa_comp_end = LUSTRE_EOF;
4051 if (lsa.lsa_comp_end != 0) {
4052 result = comp_args_to_layout(lpp, &lsa, true);
4054 fprintf(stderr, "error: %s: invalid layout\n",
4061 if (mirror_flags & MF_NO_VERIFY) {
4062 if (opc != SO_MIRROR_EXTEND) {
4064 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4068 } else if (!has_m_file) {
4070 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4077 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4079 "%s %s: --component-set doesn't have component-id set\n",
4084 if ((delete + comp_set + comp_del + comp_add) > 1) {
4086 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4091 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4092 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4094 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4099 if ((comp_set || comp_del) &&
4100 (setstripe_args_specified(&lsa) || layout != NULL)) {
4102 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4107 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4109 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4114 if (comp_add || comp_del) {
4117 result = lstat(fname, &st);
4118 if (result == 0 && S_ISDIR(st.st_mode)) {
4120 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4127 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4136 "%s %s: option -E must be specified with --component-add\n",
4142 if (from_yaml && from_copy) {
4144 "%s: can't specify --yaml and --copy together\n",
4149 if ((from_yaml || from_copy) &&
4150 (setstripe_args_specified(&lsa) || layout != NULL)) {
4152 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4157 if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4159 "%s %s: options --non-block and --block are mutually exclusive\n",
4164 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4165 opc != SO_MIRROR_DELETE && comp_id != 0) {
4167 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4172 if (migrate_mdt_mode) {
4173 struct lmv_user_md *lmu;
4175 /* initialize migrate mdt parameters */
4176 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4177 LMV_USER_MAGIC_SPECIFIC));
4180 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4181 progname, argv[0], strerror(ENOMEM));
4185 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4186 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4187 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4189 "%s %s: migrate should specify MDT index\n",
4194 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4195 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4196 lmu->lum_hash_type = lsa.lsa_pattern;
4198 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4199 if (lsa.lsa_pool_name) {
4200 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4201 sizeof(lmu->lum_pool_name) - 1);
4202 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4204 if (lsa.lsa_nr_tgts > 1) {
4207 if (lsa.lsa_stripe_count > 0 &&
4208 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4209 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4211 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4212 progname, lsa.lsa_stripe_count,
4218 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4219 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4220 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4221 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4223 lmu->lum_magic = LMV_USER_MAGIC;
4226 migrate_mdt_param.fp_lmv_md = lmu;
4227 migrate_mdt_param.fp_migrate = 1;
4228 } else if (!layout) {
4229 if (lsa_args_stripe_count_check(&lsa))
4232 /* initialize stripe parameters */
4233 param = calloc(1, offsetof(typeof(*param),
4234 lsp_osts[lsa.lsa_nr_tgts]));
4237 "%s %s: cannot allocate memory for parameters: %s\n",
4238 progname, argv[0], strerror(ENOMEM));
4243 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4244 param->lsp_stripe_size = lsa.lsa_stripe_size;
4245 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4246 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4247 param->lsp_stripe_count = -1;
4249 param->lsp_stripe_count = lsa.lsa_stripe_count;
4251 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4252 param->lsp_stripe_offset = -1;
4254 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4255 param->lsp_stripe_pattern =
4256 llapi_pattern_to_lov(lsa.lsa_pattern);
4257 if (param->lsp_stripe_pattern == EINVAL) {
4258 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4263 param->lsp_pool = lsa.lsa_pool_name;
4264 param->lsp_is_specific = false;
4266 if (lsa.lsa_nr_tgts > 0) {
4267 param->lsp_is_specific = true;
4268 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4269 memcpy(param->lsp_osts, tgts,
4270 sizeof(*tgts) * lsa.lsa_nr_tgts);
4275 /* generate a layout from a YAML template */
4276 result = lfs_comp_create_from_yaml(template, &layout,
4280 "error: %s: can't create composite layout from template file %s\n",
4286 if (layout != NULL || mirror_list != NULL) {
4288 result = mirror_adjust_first_extents(mirror_list);
4290 result = layout_adjust_first_extent(fname, layout,
4292 if (result == -ENODATA)
4294 else if (result != 0) {
4295 fprintf(stderr, "error: %s: invalid layout\n",
4301 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4303 layout = llapi_layout_get_by_path(template ?: fname, 0);
4306 "%s: can't create composite layout from file %s: %s\n",
4307 progname, template ?: fname,
4314 if (migrate_mdt_mode) {
4315 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4316 } else if (migrate_mode) {
4317 result = lfs_migrate(fname, migration_flags, param,
4319 } else if (comp_set != 0) {
4320 result = lfs_component_set(fname, comp_id,
4323 lsa.lsa_comp_neg_flags);
4324 } else if (comp_del != 0) {
4325 result = lfs_component_del(fname, comp_id,
4327 lsa.lsa_comp_neg_flags);
4328 } else if (comp_add != 0) {
4329 result = lfs_component_add(fname, layout);
4330 } else if (opc == SO_MIRROR_CREATE) {
4331 result = mirror_create(fname, mirror_list);
4332 } else if (opc == SO_MIRROR_EXTEND) {
4333 result = mirror_extend(fname, mirror_list,
4335 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4336 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4338 "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4342 if (lsa.lsa_pool_name)
4343 mirror_flags |= MF_COMP_POOL;
4344 else if (mirror_id != 0)
4345 comp_id = mirror_id;
4347 mirror_flags |= MF_COMP_ID;
4348 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4350 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4354 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4356 has_m_file ? mirror_list->m_file :
4358 } else if (layout) {
4359 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4365 } else if (foreign_mode) {
4366 result = llapi_file_create_foreign(fname, mode, type,
4373 result = llapi_file_open_param(fname,
4382 /* Save the first error encountered. */
4390 umask(previous_umask);
4393 free(migrate_mdt_param.fp_lmv_md);
4394 llapi_layout_free(layout);
4395 lfs_mirror_list_free(mirror_list);
4400 llapi_layout_free(layout);
4401 lfs_mirror_list_free(mirror_list);
4405 static int lfs_poollist(int argc, char **argv)
4410 return llapi_poollist(argv[1]);
4413 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4414 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4419 char *endptr = "AD";
4424 else if (str[0] == '-')
4430 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4431 long long val = strtoll(timebuf, &endptr, 0);
4436 unit *= 52; /* 52 weeks + 1 day below */
4437 case 'w': /* fallthrough */
4439 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4440 param->fp_time_margin *= (1 + unit / 52);
4441 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4442 case '\0': /* days are default unit if none used */
4443 case 'd': /* fallthrough */
4445 case 'h': /* fallthrough */
4447 case 'm': /* fallthrough */
4449 case 's': /* fallthrough */
4451 /* don't need to multiply by 1 for seconds */
4454 "%s find: bad time string '%s': %s\n",
4455 progname, timebuf, strerror(EINVAL));
4459 if (param->fp_time_margin == 0 ||
4460 (*endptr && unit < param->fp_time_margin))
4461 param->fp_time_margin = unit;
4468 fprintf(stderr, "%s find: bad time '%s': too large\n",
4478 static int str2quotaid(__u32 *id, const char *arg)
4480 unsigned long int projid_tmp = 0;
4481 char *endptr = NULL;
4483 projid_tmp = strtoul(arg, &endptr, 10);
4484 if (*endptr != '\0')
4486 /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4487 if (projid_tmp >= UINT32_MAX)
4494 static int name2uid(unsigned int *id, const char *name)
4496 struct passwd *passwd;
4498 passwd = getpwnam(name);
4501 *id = passwd->pw_uid;
4506 static int name2gid(unsigned int *id, const char *name)
4508 struct group *group;
4510 group = getgrnam(name);
4513 *id = group->gr_gid;
4518 static inline int name2projid(unsigned int *id, const char *name)
4523 static int uid2name(char **name, unsigned int id)
4525 struct passwd *passwd;
4527 passwd = getpwuid(id);
4530 *name = passwd->pw_name;
4535 static inline int gid2name(char **name, unsigned int id)
4537 struct group *group;
4539 group = getgrgid(id);
4542 *name = group->gr_name;
4547 static int name2layout(__u32 *layout, char *name)
4549 char *ptr, *layout_name;
4552 for (ptr = name; ; ptr = NULL) {
4553 layout_name = strtok(ptr, ",");
4556 if (strcmp(layout_name, "released") == 0)
4557 *layout |= LOV_PATTERN_F_RELEASED;
4558 else if (strcmp(layout_name, "raid0") == 0)
4559 *layout |= LOV_PATTERN_RAID0;
4560 else if (strcmp(layout_name, "mdt") == 0)
4561 *layout |= LOV_PATTERN_MDT;
4562 else if (strcmp(layout_name, "overstriping") == 0)
4563 *layout |= LOV_PATTERN_OVERSTRIPING;
4570 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4573 int user, group, other;
4578 mode_t previous_flags;
4580 user = group = other = 0;
4600 user = group = other = 1;
4611 who = user || group || other;
4614 usermask = umask(0022);
4619 if (*input == '-' || *input == '+' || *input == '=')
4622 /* operation is required */
4625 /* get the flags in *outmode */
4628 previous_flags = (*outmode & 0700);
4629 perm |= user ? previous_flags : 0;
4630 perm |= group ? (previous_flags >> 3) : 0;
4631 perm |= other ? (previous_flags >> 6) : 0;
4635 previous_flags = (*outmode & 0070);
4636 perm |= user ? (previous_flags << 3) : 0;
4637 perm |= group ? previous_flags : 0;
4638 perm |= other ? (previous_flags >> 3) : 0;
4642 previous_flags = (*outmode & 0007);
4643 perm |= user ? (previous_flags << 6) : 0;
4644 perm |= group ? (previous_flags << 3) : 0;
4645 perm |= other ? previous_flags : 0;
4652 /* this part is optional,
4653 * if empty perm = 0 and *outmode is not modified
4660 perm |= user ? 0400 : 0;
4661 perm |= group ? 0040 : 0;
4662 perm |= other ? 0004 : 0;
4663 /* set read permission for uog except for umask's
4666 perm |= who ? 0 : (0444 & ~usermask);
4669 perm |= user ? 0200 : 0;
4670 perm |= group ? 0020 : 0;
4671 perm |= other ? 0002 : 0;
4672 /* set write permission for uog except for umask'
4675 perm |= who ? 0 : (0222 & ~usermask);
4678 perm |= user ? 0100 : 0;
4679 perm |= group ? 0010 : 0;
4680 perm |= other ? 0001 : 0;
4681 /* set execute permission for uog except for umask'
4684 perm |= who ? 0 : (0111 & ~usermask);
4688 * Adds execute permission to 'u', 'g' and/or 'g' if
4689 * specified and either 'u', 'g' or 'o' already has
4690 * execute permissions.
4692 if ((*outmode & 0111) != 0) {
4693 perm |= user ? 0100 : 0;
4694 perm |= group ? 0010 : 0;
4695 perm |= other ? 0001 : 0;
4696 perm |= !who ? 0111 : 0;
4700 /* s is ignored if o is given, but it's not an error */
4701 if (other && !group && !user)
4703 perm |= user ? S_ISUID : 0;
4704 perm |= group ? S_ISGID : 0;
4707 /* 't' should be used when 'a' is given
4710 perm |= (!who || all) ? S_ISVTX : 0;
4711 /* using ugo with t is not an error */
4722 /* uog flags should be only one character long */
4723 if (previous_flags && (*input != '\0' && *input != ','))
4728 /* remove the flags from outmode */
4732 /* add the flags to outmode */
4736 /* set the flags of outmode to perm */
4746 static int str2mode_t(const char *input, mode_t *outmode)
4753 if (*input >= '0' && *input <= '7') {
4754 /* parse octal representation */
4759 /* look for invalid digits in octal representation */
4760 while (isdigit(*iter))
4765 *outmode = strtoul(input, &end, 8);
4767 if (errno != 0 || *outmode > 07777) {
4772 } else if (*input == '8' || *input == '9') {
4773 /* error: invalid octal number */
4776 /* parse coma seperated list of symbolic representation */
4785 rc = parse_symbolic(input, outmode, &end);
4790 } while (*end == ',');
4798 static int lfs_find(int argc, char **argv)
4803 struct find_param param = {
4806 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4808 struct option long_opts[] = {
4809 { .val = 'A', .name = "atime", .has_arg = required_argument },
4810 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4811 { .val = 'B', .name = "btime", .has_arg = required_argument },
4812 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4813 { .val = LFS_COMP_COUNT_OPT,
4814 .name = "comp-count", .has_arg = required_argument },
4815 { .val = LFS_COMP_COUNT_OPT,
4816 .name = "component-count",
4817 .has_arg = required_argument },
4818 { .val = LFS_COMP_FLAGS_OPT,
4819 .name = "comp-flags", .has_arg = required_argument },
4820 { .val = LFS_COMP_FLAGS_OPT,
4821 .name = "component-flags",
4822 .has_arg = required_argument },
4823 { .val = LFS_COMP_START_OPT,
4824 .name = "comp-start", .has_arg = required_argument },
4825 { .val = LFS_COMP_START_OPT,
4826 .name = "component-start",
4827 .has_arg = required_argument },
4828 { .val = LFS_MIRROR_STATE_OPT,
4829 .name = "mirror-state", .has_arg = required_argument },
4830 { .val = LFS_NEWERXY_OPT,
4831 .name = "newer", .has_arg = required_argument},
4832 { .val = LFS_NEWERXY_OPT,
4833 .name = "neweraa", .has_arg = required_argument},
4834 { .val = LFS_NEWERXY_OPT,
4835 .name = "neweram", .has_arg = required_argument},
4836 { .val = LFS_NEWERXY_OPT,
4837 .name = "newerac", .has_arg = required_argument},
4838 { .val = LFS_NEWERXY_OPT,
4839 .name = "newerab", .has_arg = required_argument},
4840 { .val = LFS_NEWERXY_OPT,
4841 .name = "newerma", .has_arg = required_argument},
4842 { .val = LFS_NEWERXY_OPT,
4843 .name = "newermm", .has_arg = required_argument},
4844 { .val = LFS_NEWERXY_OPT,
4845 .name = "newermc", .has_arg = required_argument},
4846 { .val = LFS_NEWERXY_OPT,
4847 .name = "newermb", .has_arg = required_argument},
4848 { .val = LFS_NEWERXY_OPT,
4849 .name = "newerca", .has_arg = required_argument},
4850 { .val = LFS_NEWERXY_OPT,
4851 .name = "newercm", .has_arg = required_argument},
4852 { .val = LFS_NEWERXY_OPT,
4853 .name = "newercc", .has_arg = required_argument},
4854 { .val = LFS_NEWERXY_OPT,
4855 .name = "newercb", .has_arg = required_argument},
4856 { .val = LFS_NEWERXY_OPT,
4857 .name = "newerba", .has_arg = required_argument},
4858 { .val = LFS_NEWERXY_OPT,
4859 .name = "newerbm", .has_arg = required_argument},
4860 { .val = LFS_NEWERXY_OPT,
4861 .name = "newerbc", .has_arg = required_argument},
4862 { .val = LFS_NEWERXY_OPT,
4863 .name = "newerbb", .has_arg = required_argument},
4864 { .val = LFS_NEWERXY_OPT,
4865 .name = "newerBa", .has_arg = required_argument},
4866 { .val = LFS_NEWERXY_OPT,
4867 .name = "newerBm", .has_arg = required_argument},
4868 { .val = LFS_NEWERXY_OPT,
4869 .name = "newerBc", .has_arg = required_argument},
4870 { .val = LFS_NEWERXY_OPT,
4871 .name = "newerBB", .has_arg = required_argument},
4872 { .val = LFS_NEWERXY_OPT,
4873 .name = "newerat", .has_arg = required_argument},
4874 { .val = LFS_NEWERXY_OPT,
4875 .name = "newermt", .has_arg = required_argument},
4876 { .val = LFS_NEWERXY_OPT,
4877 .name = "newerct", .has_arg = required_argument},
4878 { .val = LFS_NEWERXY_OPT,
4879 .name = "newerbt", .has_arg = required_argument},
4880 { .val = LFS_NEWERXY_OPT,
4881 .name = "newerBt", .has_arg = required_argument},
4882 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4883 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4884 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4885 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4886 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4887 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4888 { .val = 'E', .name = "component-end",
4889 .has_arg = required_argument },
4890 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4891 { .val = LFS_LAYOUT_FOREIGN_OPT,
4892 .name = "foreign", .has_arg = optional_argument},
4893 { .val = 'g', .name = "gid", .has_arg = required_argument },
4894 { .val = 'G', .name = "group", .has_arg = required_argument },
4895 { .val = 'h', .name = "help", .has_arg = no_argument },
4896 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4897 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4898 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4899 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4900 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4901 { .val = 'L', .name = "layout", .has_arg = required_argument },
4902 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4903 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4904 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4905 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4906 { .val = 'n', .name = "name", .has_arg = required_argument },
4907 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4908 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4909 { .val = 'O', .name = "obd", .has_arg = required_argument },
4910 { .val = 'O', .name = "ost", .has_arg = required_argument },
4911 { .val = LFS_FIND_PERM,
4912 .name = "perm", .has_arg = required_argument },
4913 /* no short option for pool yet, can be 'p' after 2.18 */
4914 { .val = LFS_POOL_OPT,
4915 .name = "pool", .has_arg = required_argument },
4916 { .val = '0', .name = "print0", .has_arg = no_argument },
4917 { .val = 'P', .name = "print", .has_arg = no_argument },
4918 { .val = LFS_PROJID_OPT,
4919 .name = "projid", .has_arg = required_argument },
4920 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4921 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4922 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4923 { .val = 's', .name = "size", .has_arg = required_argument },
4924 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4925 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4926 { .val = 't', .name = "type", .has_arg = required_argument },
4927 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4928 { .val = 'u', .name = "uid", .has_arg = required_argument },
4929 { .val = 'U', .name = "user", .has_arg = required_argument },
4930 { .val = 'z', .name = "extension-size",
4931 .has_arg = required_argument },
4932 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4933 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4934 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4948 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4949 while ((c = getopt_long_only(argc, argv,
4950 "-0A:b:B:c:C:D:E:g:G:hH:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4951 long_opts, &optidx)) >= 0) {
4956 /* '!' is part of option */
4958 * when getopt_long_only() finds a string which is not
4959 * an option nor a known option argument it returns 1
4960 * in that case if we already have found pathstart and pathend
4961 * (i.e. we have the list of pathnames),
4962 * the only supported value is "!"
4964 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4965 if (!isoption && pathend != -1) {
4967 "err: %s: filename|dirname must either precede options or follow options\n",
4972 if (!isoption && pathstart == -1)
4973 pathstart = optind - 1;
4974 if (isoption && pathstart != -1 && pathend == -1)
4975 pathend = optind - 2;
4982 * unknown; opt is "!" or path component,
4983 * checking done above.
4985 if (strcmp(optarg, "!") == 0)
4989 xtime = ¶m.fp_atime;
4990 xsign = ¶m.fp_asign;
4991 param.fp_exclude_atime = !!neg_opt;
4992 /* no break, this falls through to 'B' for btime */
4995 xtime = ¶m.fp_btime;
4996 xsign = ¶m.fp_bsign;
4997 param.fp_exclude_btime = !!neg_opt;
4999 /* no break, this falls through to 'C' for ctime */
5002 xtime = ¶m.fp_ctime;
5003 xsign = ¶m.fp_csign;
5004 param.fp_exclude_ctime = !!neg_opt;
5006 /* no break, this falls through to 'M' for mtime */
5009 xtime = ¶m.fp_mtime;
5010 xsign = ¶m.fp_msign;
5011 param.fp_exclude_mtime = !!neg_opt;
5013 rc = set_time(¶m, &t, xtime, optarg);
5014 if (rc == LONG_MAX) {
5022 if (optarg[0] == '+') {
5023 param.fp_blocks_sign = -1;
5025 } else if (optarg[0] == '-') {
5026 param.fp_blocks_sign = 1;
5030 param.fp_blocks_units = 1024;
5031 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5032 ¶m.fp_blocks_units, 0);
5034 fprintf(stderr, "error: bad blocks '%s'\n",
5038 param.fp_check_blocks = 1;
5039 param.fp_exclude_blocks = !!neg_opt;
5041 case LFS_COMP_COUNT_OPT:
5042 if (optarg[0] == '+') {
5043 param.fp_comp_count_sign = -1;
5045 } else if (optarg[0] == '-') {
5046 param.fp_comp_count_sign = 1;
5051 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5052 if (errno != 0 || *endptr != '\0' ||
5053 param.fp_comp_count > UINT32_MAX) {
5055 "error: bad component count '%s'\n",
5059 param.fp_check_comp_count = 1;
5060 param.fp_exclude_comp_count = !!neg_opt;
5062 case LFS_COMP_FLAGS_OPT:
5063 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5064 ¶m.fp_comp_neg_flags);
5067 "error: bad component flags '%s'\n",
5071 param.fp_check_comp_flags = 1;
5073 __u32 flags = param.fp_comp_neg_flags;
5075 param.fp_comp_neg_flags = param.fp_comp_flags;
5076 param.fp_comp_flags = flags;
5079 case LFS_COMP_START_OPT:
5080 if (optarg[0] == '+') {
5081 param.fp_comp_start_sign = -1;
5083 } else if (optarg[0] == '-') {
5084 param.fp_comp_start_sign = 1;
5088 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5089 ¶m.fp_comp_start_units, 0);
5092 "error: bad component start '%s'\n",
5096 param.fp_check_comp_start = 1;
5097 param.fp_exclude_comp_start = !!neg_opt;
5099 case LFS_MIRROR_STATE_OPT:
5100 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5101 ¶m.fp_mirror_neg_state);
5104 "error: bad mirrored file state '%s'\n",
5108 param.fp_check_mirror_state = 1;
5110 __u16 state = param.fp_mirror_neg_state;
5112 param.fp_mirror_neg_state =
5113 param.fp_mirror_state;
5114 param.fp_mirror_state = state;
5118 if (optarg[0] == '+') {
5119 param.fp_stripe_count_sign = -1;
5121 } else if (optarg[0] == '-') {
5122 param.fp_stripe_count_sign = 1;
5127 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5128 if (errno != 0 || *endptr != '\0' ||
5129 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5131 "error: bad stripe_count '%s'\n",
5136 param.fp_check_stripe_count = 1;
5137 param.fp_exclude_stripe_count = !!neg_opt;
5141 param.fp_max_depth = strtol(optarg, 0, 0);
5142 if (errno != 0 || param.fp_max_depth < 0) {
5144 "error: bad maxdepth '%s'\n",
5151 if (optarg[0] == '+') {
5152 param.fp_comp_end_sign = -1;
5154 } else if (optarg[0] == '-') {
5155 param.fp_comp_end_sign = 1;
5159 if (arg_is_eof(optarg)) {
5160 param.fp_comp_end = LUSTRE_EOF;
5161 param.fp_comp_end_units = 1;
5164 rc = llapi_parse_size(optarg,
5166 ¶m.fp_comp_end_units, 0);
5167 /* assume units of KB if too small */
5168 if (param.fp_comp_end < 4096)
5169 param.fp_comp_end *= 1024;
5173 "error: bad component end '%s'\n",
5177 param.fp_check_comp_end = 1;
5178 param.fp_exclude_comp_end = !!neg_opt;
5180 case LFS_LAYOUT_FOREIGN_OPT: {
5181 /* all types by default */
5182 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5185 /* check pure numeric */
5186 type = strtoul(optarg, &endptr, 0);
5189 type = check_foreign_type_name(optarg);
5190 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5192 "%s %s: unknown foreign type '%s'\n",
5197 } else if (type >= UINT32_MAX) {
5199 "%s %s: invalid foreign type '%s'\n",
5200 progname, argv[0], optarg);
5204 param.fp_foreign_type = type;
5205 param.fp_check_foreign = 1;
5206 param.fp_exclude_foreign = !!neg_opt;
5209 case LFS_NEWERXY_OPT: {
5215 time_t ref = time(NULL);
5217 /* no need to check bad options, they won't get here */
5218 if (strlen(long_opts[optidx].name) == 7) {
5219 x = long_opts[optidx].name[5];
5220 y = long_opts[optidx].name[6];
5224 static const char *const fmts[] = {
5225 "%Y-%m-%d %H:%M:%S",
5228 "%H:%M:%S", /* sometime today */
5237 for (i = 0; fmts[i] != NULL; i++) {
5240 /* Init for times relative to today */
5241 if (strncmp(fmts[i], "%H", 2) == 0)
5242 localtime_r(&ref, &tm);
5244 memset(&tm, 0, sizeof(tm));
5245 ptr = strptime(optarg, fmts[i], &tm);
5247 while (ptr && isspace(*ptr))
5249 if (ptr == optarg + strlen(optarg)) {
5257 "%s: invalid time '%s'\n",
5260 "supported formats are:\n ");
5261 for (i = 0; fmts[i] != NULL; i++)
5262 fprintf(stderr, "'%s', ",
5264 fprintf(stderr, "\n");
5270 } else if (y == 'b' || y == 'B') {
5273 rc = llapi_get_lum_file(optarg, NULL, &stx,
5275 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5276 if (!(stx.stx_mask & STATX_BTIME))
5281 "%s: get btime failed '%s': %s\n",
5287 ref = stx.stx_btime.tv_sec;
5289 struct stat statbuf;
5291 if (stat(optarg, &statbuf) < 0) {
5293 "%s: cannot stat file '%s': %s\n",
5302 ref = statbuf.st_atime;
5305 ref = statbuf.st_mtime;
5308 ref = statbuf.st_ctime;
5312 "%s: invalid Y argument: '%c'\n",
5321 xidx = NEWERXY_ATIME;
5324 xidx = NEWERXY_MTIME;
5327 xidx = NEWERXY_CTIME;
5331 xidx = NEWERXY_BTIME;
5335 "%s: invalid X argument: '%c'\n",
5342 newery = ¶m.fp_newery[xidx][negidx];
5348 *newery = *newery > ref ? ref : *newery;
5350 *newery = *newery > ref ? *newery : ref;
5352 param.fp_newerxy = 1;
5357 rc = name2gid(¶m.fp_gid, optarg);
5359 if (str2quotaid(¶m.fp_gid, optarg)) {
5361 "Group/GID: %s cannot be found.\n",
5367 param.fp_exclude_gid = !!neg_opt;
5368 param.fp_check_gid = 1;
5371 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5372 ¶m.fp_hash_exflags,
5373 ¶m.fp_hash_type);
5378 if (param.fp_hash_inflags || param.fp_hash_exflags)
5379 param.fp_check_hash_flag = 1;
5380 param.fp_exclude_hash_type = !!neg_opt;
5386 ret = name2layout(¶m.fp_layout, optarg);
5389 param.fp_exclude_layout = !!neg_opt;
5390 param.fp_check_layout = 1;
5394 rc = name2uid(¶m.fp_uid, optarg);
5396 if (str2quotaid(¶m.fp_uid, optarg)) {
5398 "User/UID: %s cannot be found.\n",
5404 param.fp_exclude_uid = !!neg_opt;
5405 param.fp_check_uid = 1;
5408 param.fp_pattern = (char *)optarg;
5409 param.fp_exclude_pattern = !!neg_opt;
5412 if (optarg[0] == '+') {
5413 param.fp_mirror_count_sign = -1;
5415 } else if (optarg[0] == '-') {
5416 param.fp_mirror_count_sign = 1;
5421 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5422 if (errno != 0 || *endptr != '\0' ||
5423 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5425 "error: bad mirror count '%s'\n",
5429 param.fp_check_mirror_count = 1;
5430 param.fp_exclude_mirror_count = !!neg_opt;
5435 char *buf, *token, *next, *p;
5439 buf = strdup(optarg);
5445 param.fp_exclude_obd = !!neg_opt;
5448 while (token && *token) {
5449 token = strchr(token, ',');
5456 param.fp_exclude_mdt = !!neg_opt;
5457 param.fp_num_alloc_mdts += len;
5458 tmp = realloc(param.fp_mdt_uuid,
5459 param.fp_num_alloc_mdts *
5460 sizeof(*param.fp_mdt_uuid));
5466 param.fp_mdt_uuid = tmp;
5468 param.fp_exclude_obd = !!neg_opt;
5469 param.fp_num_alloc_obds += len;
5470 tmp = realloc(param.fp_obd_uuid,
5471 param.fp_num_alloc_obds *
5472 sizeof(*param.fp_obd_uuid));
5478 param.fp_obd_uuid = tmp;
5480 for (token = buf; token && *token; token = next) {
5481 struct obd_uuid *puuid;
5485 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5488 ¶m.fp_obd_uuid[param.fp_num_obds++];
5490 p = strchr(token, ',');
5497 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5502 strncpy(puuid->uuid, token,
5503 sizeof(puuid->uuid));
5510 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5514 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5516 "Pool name %s is too long (max %d)\n",
5517 optarg, LOV_MAXPOOLNAME);
5522 * We do check for empty pool because empty pool
5523 * is used to find V1 LOV attributes
5525 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5526 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5527 param.fp_exclude_pool = !!neg_opt;
5528 param.fp_check_pool = 1;
5530 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5531 case 'p': /* want this for --pool, to match getstripe/find */
5533 "warning: -p deprecated, use --print0 or -0\n");
5536 param.fp_zero_end = 1;
5538 case 'P': /* we always print, this option is a no-op */
5540 case LFS_PROJID_OPT:
5541 rc = name2projid(¶m.fp_projid, optarg);
5543 if (str2quotaid(¶m.fp_projid, optarg)) {
5545 "Invalid project ID: %s\n",
5551 param.fp_exclude_projid = !!neg_opt;
5552 param.fp_check_projid = 1;
5555 if (optarg[0] == '+') {
5556 param.fp_size_sign = -1;
5558 } else if (optarg[0] == '-') {
5559 param.fp_size_sign = 1;
5563 ret = llapi_parse_size(optarg, ¶m.fp_size,
5564 ¶m.fp_size_units, 0);
5566 fprintf(stderr, "error: bad file size '%s'\n",
5570 param.fp_check_size = 1;
5571 param.fp_exclude_size = !!neg_opt;
5574 if (optarg[0] == '+') {
5575 param.fp_stripe_size_sign = -1;
5577 } else if (optarg[0] == '-') {
5578 param.fp_stripe_size_sign = 1;
5582 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5583 ¶m.fp_stripe_size_units, 0);
5584 /* assume units of KB if too small to be valid */
5585 if (param.fp_stripe_size < 4096)
5586 param.fp_stripe_size *= 1024;
5588 fprintf(stderr, "error: bad stripe_size '%s'\n",
5592 param.fp_check_stripe_size = 1;
5593 param.fp_exclude_stripe_size = !!neg_opt;
5596 param.fp_exclude_type = !!neg_opt;
5597 switch (optarg[0]) {
5599 param.fp_type = S_IFBLK;
5602 param.fp_type = S_IFCHR;
5605 param.fp_type = S_IFDIR;
5608 param.fp_type = S_IFREG;
5611 param.fp_type = S_IFLNK;
5614 param.fp_type = S_IFIFO;
5617 param.fp_type = S_IFSOCK;
5620 fprintf(stderr, "%s: bad type '%s'\n",
5627 param.fp_exclude_perm = !!neg_opt;
5628 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5629 if (*optarg == '/') {
5630 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5632 } else if (*optarg == '-') {
5633 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5637 if (str2mode_t(optarg, ¶m.fp_perm)) {
5638 fprintf(stderr, "error: invalid mode '%s'\n",
5645 if (optarg[0] == '+') {
5646 param.fp_mdt_count_sign = -1;
5648 } else if (optarg[0] == '-') {
5649 param.fp_mdt_count_sign = 1;
5654 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5655 if (errno != 0 || *endptr != '\0' ||
5656 param.fp_mdt_count >= UINT32_MAX) {
5657 fprintf(stderr, "error: bad mdt_count '%s'\n",
5662 param.fp_check_mdt_count = 1;
5663 param.fp_exclude_mdt_count = !!neg_opt;
5666 if (optarg[0] == '+') {
5667 param.fp_ext_size_sign = -1;
5669 } else if (optarg[0] == '-') {
5670 param.fp_ext_size_sign = 1;
5674 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5675 ¶m.fp_ext_size_units, 0);
5677 fprintf(stderr, "error: bad ext-size '%s'\n",
5681 param.fp_ext_size /= SEL_UNIT_SIZE;
5682 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5683 param.fp_check_ext_size = 1;
5684 param.fp_exclude_ext_size = !!neg_opt;
5687 fprintf(stderr, "%s: unrecognized option '%s'\n",
5688 progname, argv[optind - 1]);
5695 if (pathstart == -1) {
5696 fprintf(stderr, "error: %s: no filename|pathname\n",
5700 } else if (pathend == -1) {
5706 rc = llapi_find(argv[pathstart], ¶m);
5709 pathbad = pathstart;
5711 } while (++pathstart < pathend);
5714 fprintf(stderr, "%s: failed for '%s': %s\n",
5715 progname, argv[pathbad], strerror(-rc));
5718 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5719 free(param.fp_obd_uuid);
5721 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5722 free(param.fp_mdt_uuid);
5727 static int lfs_getstripe_internal(int argc, char **argv,
5728 struct find_param *param)
5730 struct option long_opts[] = {
5731 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5732 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5733 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5734 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5735 { .val = LFS_COMP_COUNT_OPT,
5736 .name = "comp-count", .has_arg = no_argument },
5737 { .val = LFS_COMP_COUNT_OPT,
5738 .name = "component-count", .has_arg = no_argument },
5739 { .val = LFS_COMP_FLAGS_OPT,
5740 .name = "comp-flags", .has_arg = optional_argument },
5741 { .val = LFS_COMP_FLAGS_OPT,
5742 .name = "component-flags", .has_arg = optional_argument },
5743 { .val = LFS_COMP_START_OPT,
5744 .name = "comp-start", .has_arg = optional_argument },
5745 { .val = LFS_COMP_START_OPT,
5746 .name = "component-start", .has_arg = optional_argument },
5747 { .val = LFS_MIRROR_INDEX_OPT,
5748 .name = "mirror-index", .has_arg = required_argument },
5749 { .val = LFS_MIRROR_ID_OPT,
5750 .name = "mirror-id", .has_arg = required_argument },
5751 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5752 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5753 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5754 { .val = 'd', .name = "directory", .has_arg = no_argument },
5755 { .val = 'D', .name = "default", .has_arg = no_argument },
5756 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5757 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5758 { .val = 'F', .name = "fid", .has_arg = no_argument },
5759 { .val = 'g', .name = "generation", .has_arg = no_argument },
5760 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5761 { .val = 'h', .name = "help", .has_arg = no_argument },
5762 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5763 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5764 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5765 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5766 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5767 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5768 { .val = 'L', .name = "layout", .has_arg = no_argument },
5769 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5770 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5771 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5772 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5773 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5774 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5775 { .val = 'O', .name = "obd", .has_arg = required_argument },
5776 { .val = 'O', .name = "ost", .has_arg = required_argument },
5777 { .val = 'p', .name = "pool", .has_arg = no_argument },
5778 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5779 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5780 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5781 { .val = 'R', .name = "raw", .has_arg = no_argument },
5782 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5783 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5784 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5785 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5786 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5787 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5788 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5789 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5790 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5791 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5792 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5796 int pathstart = -1, pathend = -1;
5800 while ((c = getopt_long(argc, argv,
5801 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5802 long_opts, NULL)) != -1) {
5806 /* '!' is part of option */
5807 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5808 if (!isoption && pathend != -1) {
5810 "error: %s: filename|dirname must either precede options or follow options\n",
5814 if (!isoption && pathstart == -1)
5815 pathstart = optind - 1;
5816 if (isoption && pathstart != -1 && pathend == -1)
5817 pathend = optind - 2;
5821 /* unknown: opt is "!" */
5822 if (strcmp(optarg, "!") == 0)
5826 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5827 param->fp_verbose |= VERBOSE_COUNT;
5828 param->fp_max_depth = 0;
5831 case LFS_COMP_COUNT_OPT:
5832 param->fp_verbose |= VERBOSE_COMP_COUNT;
5833 param->fp_max_depth = 0;
5835 case LFS_COMP_FLAGS_OPT:
5837 rc = comp_str2flags(optarg,
5838 ¶m->fp_comp_flags,
5839 ¶m->fp_comp_neg_flags);
5842 "error: %s bad component flags '%s'.\n",
5846 param->fp_check_comp_flags = 1;
5848 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5849 param->fp_max_depth = 0;
5852 case LFS_COMP_START_OPT:
5855 if (tmp[0] == '+') {
5856 param->fp_comp_start_sign = -1;
5858 } else if (tmp[0] == '-') {
5859 param->fp_comp_start_sign = 1;
5862 rc = llapi_parse_size(tmp,
5863 ¶m->fp_comp_start,
5864 ¶m->fp_comp_start_units, 0);
5867 "error: %s bad component start '%s'.\n",
5871 param->fp_check_comp_start = 1;
5873 param->fp_verbose |= VERBOSE_COMP_START;
5874 param->fp_max_depth = 0;
5877 case LFS_MIRROR_INDEX_OPT: {
5878 unsigned long int mirror_index;
5880 if (optarg[0] == '+') {
5881 param->fp_mirror_index_sign = -1;
5883 } else if (optarg[0] == '-') {
5884 param->fp_mirror_index_sign = 1;
5889 mirror_index = strtoul(optarg, &end, 0);
5890 if (errno != 0 || *end != '\0' ||
5891 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5892 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5894 "%s %s: invalid mirror index '%s'\n",
5895 progname, argv[0], optarg);
5899 param->fp_mirror_index = (__u16)mirror_index;
5901 if (param->fp_mirror_id != 0) {
5903 "%s %s: can't specify both mirror index and mirror ID\n",
5907 param->fp_check_mirror_index = 1;
5908 param->fp_exclude_mirror_index = !!neg_opt;
5911 case LFS_MIRROR_ID_OPT: {
5912 unsigned long int mirror_id;
5914 if (optarg[0] == '+') {
5915 param->fp_mirror_id_sign = -1;
5917 } else if (optarg[0] == '-') {
5918 param->fp_mirror_id_sign = 1;
5923 mirror_id = strtoul(optarg, &end, 0);
5924 if (errno != 0 || *end != '\0' ||
5925 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5926 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5928 "%s %s: invalid mirror ID '%s'\n",
5929 progname, argv[0], optarg);
5933 param->fp_mirror_id = (__u16)mirror_id;
5935 if (param->fp_mirror_index != 0) {
5937 "%s %s: can't specify both mirror index and mirror ID\n",
5941 param->fp_check_mirror_id = 1;
5942 param->fp_exclude_mirror_id = !!neg_opt;
5946 param->fp_max_depth = 0;
5949 param->fp_get_default_lmv = 1;
5954 if (tmp[0] == '+') {
5955 param->fp_comp_end_sign = -1;
5957 } else if (tmp[0] == '-') {
5958 param->fp_comp_end_sign = 1;
5962 if (arg_is_eof(tmp)) {
5963 param->fp_comp_end = LUSTRE_EOF;
5964 param->fp_comp_end_units = 1;
5967 rc = llapi_parse_size(tmp,
5968 ¶m->fp_comp_end,
5969 ¶m->fp_comp_end_units, 0);
5970 /* assume units of KB if too small */
5971 if (param->fp_comp_end < 4096)
5972 param->fp_comp_end *= 1024;
5976 "error: %s bad component end '%s'.\n",
5980 param->fp_check_comp_end = 1;
5982 param->fp_verbose |= VERBOSE_COMP_END;
5983 param->fp_max_depth = 0;
5987 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5988 param->fp_verbose |= VERBOSE_DFID;
5989 param->fp_max_depth = 0;
5993 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5994 param->fp_verbose |= VERBOSE_GENERATION;
5995 param->fp_max_depth = 0;
5999 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6000 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6001 param->fp_max_depth = 0;
6006 param->fp_comp_id = strtoul(optarg, &end, 0);
6007 if (*end != '\0' || param->fp_comp_id == 0 ||
6008 param->fp_comp_id > LCME_ID_MAX) {
6010 "error: %s bad component id '%s'\n",
6014 param->fp_check_comp_id = 1;
6016 param->fp_max_depth = 0;
6017 param->fp_verbose |= VERBOSE_COMP_ID;
6021 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6022 param->fp_verbose |= VERBOSE_PATTERN;
6023 param->fp_max_depth = 0;
6026 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6029 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6032 if (!(param->fp_verbose & VERBOSE_DETAIL))
6033 param->fp_max_depth = 0;
6034 param->fp_verbose |= VERBOSE_MDTINDEX;
6037 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6038 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6039 param->fp_max_depth = 0;
6043 if (param->fp_obd_uuid) {
6045 "error: %s: only one obduuid allowed",
6049 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6052 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6053 param->fp_verbose |= VERBOSE_POOL;
6054 param->fp_max_depth = 0;
6061 param->fp_recursive = 1;
6067 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6068 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6069 param->fp_max_depth = 0;
6073 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6079 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6080 param->fp_verbose |= VERBOSE_EXT_SIZE;
6081 param->fp_max_depth = 0;
6085 fprintf(stderr, "%s: unrecognized option '%s'\n",
6086 progname, argv[optind - 1]);
6092 if (pathstart == -1) {
6093 fprintf(stderr, "error: %s: no filename|pathname\n",
6096 } else if (pathend == -1) {
6104 if (param->fp_recursive)
6105 param->fp_max_depth = -1;
6106 else if (param->fp_verbose & VERBOSE_DETAIL)
6107 param->fp_max_depth = 1;
6109 if (!param->fp_verbose)
6110 param->fp_verbose = VERBOSE_DEFAULT;
6111 if (param->fp_quiet)
6112 param->fp_verbose = VERBOSE_OBJID;
6115 rc = llapi_getstripe(argv[pathstart], param);
6116 } while (++pathstart < pathend && !rc);
6119 fprintf(stderr, "error: %s failed for %s.\n",
6120 argv[0], argv[optind - 1]);
6124 static int lfs_tgts(int argc, char **argv)
6126 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6127 struct find_param param;
6128 int index = 0, rc = 0;
6133 if (argc == 2 && !realpath(argv[1], path)) {
6135 fprintf(stderr, "error: invalid path '%s': %s\n",
6136 argv[1], strerror(-rc));
6140 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6141 /* Check if we have a mount point */
6142 if (mntdir[0] == '\0')
6145 memset(¶m, 0, sizeof(param));
6146 if (!strcmp(argv[0], "mdts"))
6147 param.fp_get_lmv = 1;
6149 rc = llapi_ostlist(mntdir, ¶m);
6151 fprintf(stderr, "error: %s: failed on %s\n",
6154 if (path[0] != '\0')
6156 memset(mntdir, 0, PATH_MAX);
6162 static int lfs_getstripe(int argc, char **argv)
6164 struct find_param param = { 0 };
6166 param.fp_max_depth = 1;
6167 return lfs_getstripe_internal(argc, argv, ¶m);
6171 static int lfs_getdirstripe(int argc, char **argv)
6173 struct find_param param = { 0 };
6174 struct option long_opts[] = {
6175 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6176 { .val = 'D', .name = "default", .has_arg = no_argument },
6177 { .val = 'h', .name = "help", .has_arg = no_argument },
6178 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6179 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6180 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6181 { .val = 'O', .name = "obd", .has_arg = required_argument },
6182 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6183 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6184 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6185 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6186 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6187 { .val = LFS_INHERIT_RR_OPT,
6188 .name = "max-inherit-rr", .has_arg = no_argument },
6192 param.fp_get_lmv = 1;
6194 while ((c = getopt_long(argc, argv,
6195 "cDhHimO:rtTvXy", long_opts, NULL)) != -1) {
6199 param.fp_verbose |= VERBOSE_COUNT;
6202 param.fp_get_default_lmv = 1;
6204 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6207 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6211 param.fp_verbose |= VERBOSE_HASH_TYPE;
6213 case 'i': /* fallthrough */
6215 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6218 if (param.fp_obd_uuid) {
6220 "%s: only one obduuid allowed",
6224 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6227 param.fp_recursive = 1;
6230 param.fp_verbose |= VERBOSE_DETAIL;
6233 param.fp_verbose |= VERBOSE_INHERIT;
6235 case LFS_INHERIT_RR_OPT:
6236 param.fp_verbose |= VERBOSE_INHERIT_RR;
6242 fprintf(stderr, "%s: unrecognized option '%s'\n",
6243 progname, argv[optind - 1]);
6253 if (param.fp_recursive)
6254 param.fp_max_depth = -1;
6256 if (!param.fp_verbose)
6257 param.fp_verbose = VERBOSE_DEFAULT;
6260 rc = llapi_getstripe(argv[optind], ¶m);
6261 } while (++optind < argc && !rc);
6264 fprintf(stderr, "error: %s failed for %s.\n",
6265 argv[0], argv[optind - 1]);
6270 MNTDF_INODES = 0x0001,
6271 MNTDF_COOKED = 0x0002,
6272 MNTDF_LAZY = 0x0004,
6273 MNTDF_VERBOSE = 0x0008,
6274 MNTDF_SHOW = 0x0010,
6275 MNTDF_DECIMAL = 0x0020,
6278 #define COOK(value, base) \
6281 while (value > base) { \
6289 #define CDF "%11llu"
6290 #define HDF "%8.1f%c"
6294 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6296 double avail, used, ratio = 0;
6299 avail = st->os_ffree;
6300 used = st->os_files - st->os_ffree;
6302 avail = st->os_bavail;
6303 used = st->os_blocks - st->os_bfree;
6305 if (avail + used > 0)
6306 ratio = used / (used + avail) * 100;
6308 /* Round up to match df(1) usage percentage */
6309 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6313 * This is to identify various problem states for "lfs df" if .osn_err = true,
6314 * so only show flags reflecting those states by default. Informational states
6315 * are only shown with "-v" and use lower-case names to distinguish them.
6316 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6318 static struct obd_statfs_state_names {
6319 enum obd_statfs_state osn_state;
6320 const char osn_name;
6323 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6324 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6325 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6326 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6327 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6328 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6329 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6330 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6331 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6334 static int showdf(char *mntdir, struct obd_statfs *stat,
6335 char *uuid, enum mntdf_flags flags,
6336 char *type, int index, int rc)
6338 long long avail, used, total;
6340 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6341 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6342 char tbuf[3 * sizeof(__u64)];
6343 char ubuf[3 * sizeof(__u64)];
6344 char abuf[3 * sizeof(__u64)];
6345 char rbuf[3 * sizeof(__u64)];
6352 if (flags & MNTDF_INODES) {
6353 avail = stat->os_ffree;
6354 used = stat->os_files - stat->os_ffree;
6355 total = stat->os_files;
6357 int shift = flags & MNTDF_COOKED ? 0 : 10;
6359 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6360 used = ((stat->os_blocks - stat->os_bfree) *
6361 stat->os_bsize) >> shift;
6362 total = (stat->os_blocks * stat->os_bsize) >> shift;
6365 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6367 if (flags & MNTDF_COOKED) {
6368 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6372 cook_val = (double)total;
6373 i = COOK(cook_val, base);
6375 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6378 snprintf(tbuf, sizeof(tbuf), CDF, total);
6380 cook_val = (double)used;
6381 i = COOK(cook_val, base);
6383 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6386 snprintf(ubuf, sizeof(ubuf), CDF, used);
6388 cook_val = (double)avail;
6389 i = COOK(cook_val, base);
6391 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6394 snprintf(abuf, sizeof(abuf), CDF, avail);
6396 snprintf(tbuf, sizeof(tbuf), CDF, total);
6397 snprintf(ubuf, sizeof(tbuf), CDF, used);
6398 snprintf(abuf, sizeof(tbuf), CDF, avail);
6401 sprintf(rbuf, RDF, ratio);
6402 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6403 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6405 printf("[%s:%d]", type, index);
6407 if (stat->os_state) {
6411 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6412 if (oss_names[i].osn_state & stat->os_state &&
6413 (oss_names[i].osn_err ||
6414 flags & MNTDF_VERBOSE))
6415 printf("%c", oss_names[i].osn_name);
6422 printf(UUF": inactive device\n", uuid);
6425 printf(UUF": %s\n", uuid, strerror(-rc));
6432 struct ll_stat_type {
6437 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6439 struct ll_statfs_data {
6441 struct obd_statfs sd_st;
6444 struct ll_statfs_buf {
6446 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6449 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6450 int ops, struct ll_statfs_buf *lsb)
6452 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6453 struct obd_uuid uuid_buf;
6454 char *poolname = NULL;
6455 struct ll_stat_type types[] = {
6456 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6457 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6458 { .st_name = NULL } };
6459 struct ll_stat_type *tp;
6460 __u64 ost_files = 0;
6461 __u64 ost_ffree = 0;
6469 poolname = strchr(pool, '.');
6471 if (strncmp(fsname, pool, strlen(fsname))) {
6472 fprintf(stderr, "filesystem name incorrect\n");
6480 fd = open(mntdir, O_RDONLY);
6483 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6488 if (flags & MNTDF_SHOW) {
6489 if (flags & MNTDF_INODES)
6490 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6491 "UUID", "Inodes", "IUsed", "IFree",
6492 "IUse%", "Mounted on");
6494 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6496 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6497 "Used", "Available", "Use%", "Mounted on");
6500 for (tp = types; tp->st_name != NULL; tp++) {
6501 bool have_ost = false;
6503 if (!(tp->st_op & ops))
6506 for (index = 0; ; index++) {
6507 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6508 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6509 type = flags & MNTDF_LAZY ?
6510 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6511 rc2 = llapi_obd_fstatfs(fd, type, index,
6512 &stat_buf, &uuid_buf);
6517 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6518 if (!(flags & MNTDF_VERBOSE))
6520 } else if (rc2 < 0 && rc == 0) {
6525 * If we have OSTs then don't report MDT block counts.
6526 * For MDT-only filesystems the expectation is that all
6527 * layouts have a DoM component. For filesystems with
6528 * OSTs, files are not necessarily going to store data
6529 * on MDTs, and MDT space is limited to a fraction of
6530 * OST space, so don't include it in the summary.
6532 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6539 if (poolname && tp->st_op == LL_STATFS_LOV &&
6540 llapi_search_ost(fsname, poolname,
6541 obd_uuid2str(&uuid_buf)) != 1)
6545 * the llapi_obd_fstatfs() call may have returned with
6546 * an error, but if it filled in uuid_buf we will at
6547 * lease use that to print out a message for that OBD.
6548 * If we didn't get anything in the uuid_buf, then fill
6549 * it in so that we can print an error message.
6551 if (uuid_buf.uuid[0] == '\0')
6552 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6553 "%s%04x", tp->st_name, index);
6555 lsb->sb_buf[lsb->sb_count].sd_index = index;
6556 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6559 if (flags & MNTDF_SHOW)
6560 showdf(mntdir, &stat_buf,
6561 obd_uuid2str(&uuid_buf), flags,
6562 tp->st_name, index, rc2);
6567 if (tp->st_op == LL_STATFS_LMV) {
6568 sum.os_ffree += stat_buf.os_ffree;
6569 sum.os_files += stat_buf.os_files;
6570 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6571 ost_files += stat_buf.os_files;
6572 ost_ffree += stat_buf.os_ffree;
6574 sum.os_blocks += stat_buf.os_blocks *
6576 sum.os_bfree += stat_buf.os_bfree *
6578 sum.os_bavail += stat_buf.os_bavail *
6586 * If we have _some_ OSTs, but don't have as many free objects on the
6587 * OST as inodes on the MDTs, reduce the reported number of inodes
6588 * to compensate, so that the "inodes in use" number is correct.
6589 * This should be kept in sync with ll_statfs_internal().
6591 if (ost_files && ost_ffree < sum.os_ffree) {
6592 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6593 sum.os_ffree = ost_ffree;
6595 if (flags & MNTDF_SHOW) {
6597 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6605 LAYOUT_INHERIT_UNSET = -2,
6609 static int lfs_setdirstripe(int argc, char **argv)
6612 struct lfs_setstripe_args lsa = { 0 };
6613 struct llapi_stripe_param *param = NULL;
6614 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6617 char *mode_opt = NULL;
6618 bool default_stripe = false;
6619 bool delete = false;
6620 bool foreign_mode = false;
6621 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6622 mode_t previous_mode = 0;
6624 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6625 int max_inherit = LAYOUT_INHERIT_UNSET;
6626 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6627 struct option long_opts[] = {
6628 { .val = 'c', .name = "count", .has_arg = required_argument },
6629 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6630 { .val = 'd', .name = "delete", .has_arg = no_argument },
6631 { .val = 'D', .name = "default", .has_arg = no_argument },
6632 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6633 { .val = LFS_LAYOUT_FLAGS_OPT,
6634 .name = "flags", .has_arg = required_argument },
6635 { .val = LFS_LAYOUT_FOREIGN_OPT,
6636 .name = "foreign", .has_arg = optional_argument},
6637 { .val = 'h', .name = "help", .has_arg = no_argument },
6638 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6639 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6640 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6641 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6643 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6644 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6645 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6647 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6648 { .val = 'i', .name = "index", .has_arg = required_argument },
6650 { .val = 'o', .name = "mode", .has_arg = required_argument },
6651 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6652 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6654 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6655 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6656 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
6657 { .val = LFS_INHERIT_RR_OPT,
6658 .name = "max-inherit-rr", .has_arg = required_argument},
6659 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6663 setstripe_args_init(&lsa);
6665 while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
6666 long_opts, NULL)) >= 0) {
6674 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6675 if (errno != 0 || *end != '\0' ||
6676 lsa.lsa_stripe_count < -1 ||
6677 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6679 "%s: invalid stripe count '%s'\n",
6686 default_stripe = true;
6689 default_stripe = true;
6691 case LFS_LAYOUT_FOREIGN_OPT:
6693 /* check pure numeric */
6694 type = strtoul(optarg, &end, 0);
6697 type = check_foreign_type_name(optarg);
6698 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6700 "%s %s: unknown foreign type '%s'\n",
6705 } else if (type >= UINT32_MAX) {
6707 "%s %s: invalid foreign type '%s'\n",
6708 progname, argv[0], optarg);
6712 foreign_mode = true;
6714 case LFS_LAYOUT_FLAGS_OPT:
6716 flags = strtoul(optarg, &end, 16);
6717 if (errno != 0 || *end != '\0' ||
6718 flags >= UINT32_MAX) {
6720 "%s %s: invalid hex flags '%s'\n",
6721 progname, argv[0], optarg);
6724 if (!foreign_mode) {
6726 "%s %s: hex flags must be specified with --foreign option\n",
6731 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6734 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6738 lsa.lsa_pattern = check_hashtype(optarg);
6739 if (lsa.lsa_pattern == 0) {
6741 "%s %s: bad stripe hash type '%s'\n",
6742 progname, argv[0], optarg);
6747 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6750 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6751 if (strcmp(argv[optind - 1], "--index") == 0)
6753 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6756 lsa.lsa_nr_tgts = parse_targets(mdts,
6757 sizeof(mdts) / sizeof(__u32),
6758 lsa.lsa_nr_tgts, optarg, NULL);
6759 if (lsa.lsa_nr_tgts < 0) {
6761 "%s %s: invalid MDT target(s) '%s'\n",
6762 progname, argv[0], optarg);
6766 lsa.lsa_tgts = mdts;
6767 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6768 lsa.lsa_stripe_off = mdts[0];
6770 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6773 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6783 max_inherit = strtol(optarg, &end, 10);
6784 if (errno != 0 || *end != '\0' || max_inherit < -2) {
6786 "%s %s: invalid max-inherit '%s'\n",
6787 progname, argv[0], optarg);
6790 if (max_inherit == 0) {
6791 max_inherit = LMV_INHERIT_NONE;
6792 } else if (max_inherit == -1) {
6793 max_inherit = LMV_INHERIT_UNLIMITED;
6794 } else if (max_inherit > LMV_INHERIT_MAX) {
6796 "%s %s: max-inherit %d exceeds maximum %u\n",
6797 progname, argv[0], max_inherit,
6802 case LFS_INHERIT_RR_OPT:
6803 if (!default_stripe) {
6805 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6810 max_inherit_rr = strtol(optarg, &end, 10);
6811 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6813 "%s %s: invalid max-inherit-rr '%s'\n",
6814 progname, argv[0], optarg);
6817 if (max_inherit_rr == 0) {
6818 max_inherit_rr = LMV_INHERIT_RR_NONE;
6819 } else if (max_inherit_rr == -1) {
6820 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6821 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6823 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6824 progname, argv[0], max_inherit_rr,
6825 LMV_INHERIT_RR_MAX);
6830 fprintf(stderr, "%s: unrecognized option '%s'\n",
6831 progname, argv[optind - 1]);
6838 if (optind == argc) {
6839 fprintf(stderr, "%s %s: DIR must be specified\n",
6844 if (xattr && !foreign_mode) {
6846 * only print a warning as this is armless and will be
6850 "%s %s: xattr has been specified for non-foreign layout\n",
6852 } else if (foreign_mode && !xattr) {
6854 "%s %s: xattr must be provided in foreign mode\n",
6859 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6860 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6862 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6867 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6868 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6869 /* if no parameters set, create directory on least-used MDTs */
6870 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
6871 lsa.lsa_stripe_count = 1;
6875 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6876 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6878 "%s %s: cannot specify -d with -c or -i options\n",
6884 mode = strtoul(mode_opt, &end, 8);
6887 "%s %s: bad MODE '%s'\n",
6888 progname, argv[0], mode_opt);
6891 previous_mode = umask(0);
6894 /* check max-inherit and warn user in some cases */
6895 if (default_stripe &&
6896 (lsa.lsa_stripe_count < 0 || lsa.lsa_stripe_count > 1)) {
6897 if (max_inherit == LMV_INHERIT_UNLIMITED)
6899 "%s %s: unrecommended max-inherit=-1 when default stripe-count=%lld\n",
6900 progname, argv[0], lsa.lsa_stripe_count);
6901 else if (max_inherit > LMV_INHERIT_DEFAULT_STRIPED + 2 &&
6902 max_inherit != LMV_INHERIT_NONE)
6904 "%s %s: unrecommended max-inherit=%d when default stripe-count=%lld\n",
6905 progname, argv[0], max_inherit,
6906 lsa.lsa_stripe_count);
6909 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6910 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6911 lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
6913 "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
6914 progname, argv[0], lsa.lsa_stripe_off);
6918 /* foreign LMV/dir case */
6920 if (argc > optind + 1) {
6922 "%s %s: cannot specify multiple foreign dirs\n",
6927 dname = argv[optind];
6928 result = llapi_dir_create_foreign(dname, mode, type, flags,
6932 "%s mkdir: can't create foreign dir '%s': %s\n",
6933 progname, dname, strerror(-result));
6938 * initialize stripe parameters, in case param is converted to specific,
6939 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6941 param = calloc(1, offsetof(typeof(*param),
6942 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6943 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6946 "%s %s: cannot allocate memory for parameters: %s\n",
6947 progname, argv[0], strerror(ENOMEM));
6951 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6952 if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
6953 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6954 lsa.lsa_stripe_count == 0))
6955 lsa.lsa_stripe_count = 1;
6956 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6957 param->lsp_stripe_count = lsa.lsa_stripe_count;
6958 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6959 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
6961 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6962 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6963 param->lsp_stripe_pattern = lsa.lsa_pattern;
6965 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6966 param->lsp_pool = lsa.lsa_pool_name;
6967 param->lsp_is_specific = false;
6968 if (max_inherit == LAYOUT_INHERIT_UNSET) {
6969 if (lsa.lsa_stripe_count == 0 || lsa.lsa_stripe_count == 1)
6970 max_inherit = LMV_INHERIT_DEFAULT_PLAIN;
6972 max_inherit = LMV_INHERIT_DEFAULT_STRIPED;
6974 param->lsp_max_inherit = max_inherit;
6975 if (default_stripe) {
6977 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
6978 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
6979 param->lsp_max_inherit_rr = max_inherit_rr;
6981 if (strcmp(argv[0], "mkdir") == 0)
6982 param->lsp_is_create = true;
6983 if (lsa.lsa_nr_tgts > 1) {
6984 if (lsa.lsa_stripe_count > 0 &&
6985 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6986 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6988 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6989 argv[0], lsa.lsa_stripe_count,
6995 param->lsp_is_specific = true;
6996 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6997 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
7000 dname = argv[optind];
7002 if (default_stripe) {
7003 result = llapi_dir_set_default_lmv(dname, param);
7006 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7007 progname, dname, strerror(-result));
7011 result = llapi_dir_create(dname, mode, param);
7014 "%s setdirstripe: cannot create dir '%s': %s\n",
7015 progname, dname, strerror(-result));
7016 } while (!result && (dname = argv[++optind]));
7019 umask(previous_mode);
7026 static int lfs_rmentry(int argc, char **argv)
7033 fprintf(stderr, "error: %s: missing dirname\n",
7039 dname = argv[index];
7041 result = llapi_direntry_remove(dname);
7044 "error: %s: remove dir entry '%s' failed\n",
7048 dname = argv[++index];
7053 static int lfs_unlink_foreign(int argc, char **argv)
7060 fprintf(stderr, "error: %s: missing pathname\n",
7067 while (name != NULL) {
7068 result = llapi_unlink_foreign(name);
7071 "error: %s: unlink foreign entry '%s' failed\n",
7075 name = argv[++index];
7080 static int lfs_mv(int argc, char **argv)
7082 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7083 struct find_param param = {
7090 struct option long_opts[] = {
7091 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7092 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7093 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7096 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7098 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7101 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7105 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7106 if (errno != 0 || *end != '\0' ||
7107 lmu.lum_stripe_offset >= UINT32_MAX) {
7108 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7114 param.fp_verbose = VERBOSE_DETAIL;
7117 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7118 progname, argv[optind - 1]);
7123 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7124 fprintf(stderr, "%s mv: MDT index must be specified\n",
7129 if (optind >= argc) {
7130 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7134 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7136 /* initialize migrate mdt parameters */
7137 param.fp_lmv_md = &lmu;
7138 param.fp_migrate = 1;
7139 rc = llapi_migrate_mdt(argv[optind], ¶m);
7141 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7142 progname, argv[optind], lmu.lum_stripe_offset,
7147 static int lfs_osts(int argc, char **argv)
7149 return lfs_tgts(argc, argv);
7152 static int lfs_mdts(int argc, char **argv)
7154 return lfs_tgts(argc, argv);
7157 static int lfs_df(int argc, char **argv)
7159 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7160 enum mntdf_flags flags = MNTDF_SHOW;
7161 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7162 int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7163 char fsname[PATH_MAX] = "", *pool_name = NULL;
7164 struct option long_opts[] = {
7165 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7166 { .val = 'H', .name = "si", .has_arg = no_argument },
7167 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7168 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7169 { .val = 'p', .name = "pool", .has_arg = required_argument },
7170 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7173 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7176 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7179 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7182 flags |= MNTDF_INODES;
7185 flags |= MNTDF_LAZY;
7191 flags |= MNTDF_VERBOSE;
7194 fprintf(stderr, "%s: unrecognized option '%s'\n",
7195 progname, argv[optind - 1]);
7200 /* Handle case where path is not specified */
7201 if (optind == argc) {
7202 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7203 /* Check if we have a mount point */
7204 if (mntdir[0] == '\0')
7207 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7208 if (rc || path[0] != '\0')
7211 fsname[0] = '\0'; /* avoid matching in next loop */
7212 mntdir[0] = '\0'; /* avoid matching in next loop */
7213 path[0] = '\0'; /* clean for next loop */
7218 /* Loop through all the remaining arguments. These are Lustre FS
7221 for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7224 fsname[0] = '\0'; /* start clean */
7225 mntdir[0] = '\0'; /* start clean */
7226 path[0] = '\0'; /* start clean */
7228 /* path does not exists at all */
7229 if (!realpath(argv[arg_idx], path)) {
7231 fprintf(stderr, "error: invalid path '%s': %s\n",
7232 argv[arg_idx], strerror(-rc));
7233 /* save first seen error */
7240 /* path exists but may not be a Lustre filesystem */
7241 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7242 /* Check if we have a mount point */
7243 if (mntdir[0] == '\0')
7246 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7247 if (rc || path[0] != '\0') {
7250 /* save first seen error */
7258 llapi_printf(LLAPI_MSG_ERROR,
7259 "%s:%s Not a Lustre filesystem\n",
7260 argv[0], argv[arg_idx]);
7261 /* save first seen error */
7270 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7271 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7275 if (opt_fsname == opt_instance) { /* both true or both false */
7276 rc = llapi_getname(mntdir, buf, buflen);
7277 } else if (opt_fsname) {
7279 * llapi_search_mounts() fills @buf with fsname, but that is not
7280 * called if explicit paths are specified on the command-line
7283 rc = llapi_get_fsname(mntdir, buf, buflen);
7284 } else /* if (opt_instance) */ {
7285 rc = llapi_get_instance(mntdir, buf, buflen);
7289 fprintf(stderr, "cannot get instance for '%s': %s\n",
7290 mntdir, strerror(-rc));
7295 printf("%s %s\n", buf, mntdir);
7297 printf("%s\n", buf);
7302 static int lfs_getname(int argc, char **argv)
7304 struct option long_opts[] = {
7305 { .val = 'h', .name = "help", .has_arg = no_argument },
7306 { .val = 'i', .name = "instance", .has_arg = no_argument },
7307 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7309 bool opt_instance = false, opt_fsname = false;
7310 char fsname[PATH_MAX] = "";
7313 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7316 opt_instance = true;
7322 fprintf(stderr, "%s: unrecognized option '%s'\n",
7323 progname, argv[optind - 1]);
7330 if (optind == argc) { /* no paths specified, get all paths. */
7331 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7334 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7335 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7336 opt_instance, opt_fsname, true);
7339 path[0] = fsname[0] = mntdir[0] = '\0';
7341 } else { /* paths specified, only attempt to search these. */
7344 /* if only one path is given, print only requested info */
7345 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7347 for (; optind < argc; optind++) {
7348 rc2 = print_instance(argv[optind], fsname,
7349 sizeof(fsname), opt_instance,
7350 opt_fsname, opt_mntdir);
7360 static int lfs_check(int argc, char **argv)
7362 char mntdir[PATH_MAX] = {'\0'};
7371 fprintf(stderr, "%s check: server type must be specified\n",
7376 obd_types[0] = obd_type1;
7377 obd_types[1] = obd_type2;
7378 obd_types[2] = obd_type3;
7380 if (strcmp(argv[1], "osts") == 0) {
7381 strcpy(obd_types[0], "osc");
7382 } else if (strcmp(argv[1], "mdts") == 0 ||
7383 strcmp(argv[1], "mds") == 0) {
7384 strcpy(obd_types[0], "mdc");
7385 } else if (strcmp(argv[1], "mgts") == 0) {
7386 strcpy(obd_types[0], "mgc");
7387 } else if (strcmp(argv[1], "all") == 0 ||
7388 strcmp(argv[1], "servers") == 0) {
7390 strcpy(obd_types[0], "osc");
7391 strcpy(obd_types[1], "mdc");
7392 strcpy(obd_types[2], "mgc");
7394 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7399 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
7400 if (rc < 0 || mntdir[0] == '\0') {
7402 "%s check: cannot find mounted Lustre filesystem: %s\n",
7403 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7407 rc = llapi_target_check(num_types, obd_types, mntdir);
7409 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7410 progname, argv[1], strerror(-rc));
7415 #ifdef HAVE_SYS_QUOTA_H
7416 #define ADD_OVERFLOW(a, b) \
7417 ((((a) + (b)) < (a)) ? \
7418 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7420 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7421 * returns the value or ULONG_MAX on integer overflow or incorrect format
7423 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7424 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7425 * 3. empty integer value is interpreted as 0
7427 static unsigned long str2sec(const char *timestr)
7429 const char spec[] = "smhdw";
7430 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7431 unsigned long val = 0;
7434 if (strpbrk(timestr, spec) == NULL) {
7436 * no specifiers inside the time string,
7437 * should treat it as an integer value
7439 val = strtoul(timestr, &tail, 10);
7440 return *tail ? ULONG_MAX : val;
7443 /* format string is XXwXXdXXhXXmXXs */
7449 v = strtoul(timestr, &tail, 10);
7450 if (v == ULONG_MAX || *tail == '\0')
7452 * value too large (ULONG_MAX or more)
7453 * or missing specifier
7457 ptr = strchr(spec, *tail);
7459 /* unknown specifier */
7464 /* check if product will overflow the type */
7465 if (!(v < ULONG_MAX / mult[ind]))
7468 ADD_OVERFLOW(val, mult[ind] * v);
7469 if (val == ULONG_MAX)
7481 #define ARG2ULL(nr, str, def_units) \
7483 unsigned long long limit, units = def_units; \
7486 rc = llapi_parse_size(str, &limit, &units, 1); \
7488 fprintf(stderr, "%s: invalid limit '%s'\n", \
7495 static inline int has_times_option(int argc, char **argv)
7499 for (i = 1; i < argc; i++)
7500 if (!strcmp(argv[i], "-t"))
7506 static inline int lfs_verify_poolarg(char *pool)
7508 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7510 "Pool name '%.*s' is longer than %d\n",
7511 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7517 /* special grace time, only notify the user when its quota is over soft limit
7518 * but doesn't block new writes until the hard limit is reached.
7520 #define NOTIFY_GRACE "notify"
7521 #define NOTIFY_GRACE_TIME LQUOTA_GRACE_MASK
7524 static inline __u64 lustre_stoqb(size_t space)
7526 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7529 #define lustre_stoqb toqb
7532 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7535 char *mnt, *obd_type = (char *)qctl->obd_type;
7536 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7537 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7538 struct option long_opts[] = {
7539 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7540 { .val = 'g', .name = "group", .has_arg = no_argument },
7541 { .val = 'h', .name = "help", .has_arg = no_argument },
7542 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7543 { .val = 'p', .name = "projid", .has_arg = no_argument },
7544 { .val = 't', .name = "times", .has_arg = no_argument },
7545 { .val = 'u', .name = "user", .has_arg = no_argument },
7546 { .val = LFS_POOL_OPT,
7547 .name = "pool", .has_arg = required_argument },
7551 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7552 qctl->qc_type = ALLQUOTA;
7554 while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7555 long_opts, NULL)) != -1) {
7566 if (qctl->qc_type != ALLQUOTA) {
7568 "%s: -u/g/p cannot be used more than once\n",
7572 qctl->qc_type = qtype;
7575 if (strncmp(optarg, NOTIFY_GRACE,
7576 strlen(NOTIFY_GRACE)) == 0) {
7577 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7579 dqi->dqi_bgrace = str2sec(optarg);
7580 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7582 "%s: bad block-grace: %s\n",
7587 dqb->dqb_valid |= QIF_BTIME;
7590 if (strncmp(optarg, NOTIFY_GRACE,
7591 strlen(NOTIFY_GRACE)) == 0) {
7592 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7594 dqi->dqi_igrace = str2sec(optarg);
7595 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7597 "%s: bad inode-grace: %s\n",
7602 dqb->dqb_valid |= QIF_ITIME;
7604 case 't': /* Yes, of course! */
7607 if (lfs_verify_poolarg(optarg))
7609 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7610 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7612 /* getopt prints error message for us when opterr != 0 */
7614 fprintf(stderr, "%s: unrecognized option '%s'\n",
7615 progname, argv[optind - 1]);
7622 if (qctl->qc_type == ALLQUOTA) {
7623 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7628 if (optind != argc - 1) {
7629 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7630 progname, argv[optind + 1]);
7635 rc = llapi_quotactl(mnt, qctl);
7638 fprintf(stderr, "%s %s ", obd_type,
7639 obd_uuid2str(&qctl->obd_uuid));
7640 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7647 #define BSLIMIT (1 << 0)
7648 #define BHLIMIT (1 << 1)
7649 #define ISLIMIT (1 << 2)
7650 #define IHLIMIT (1 << 3)
7652 int lfs_setquota(int argc, char **argv)
7655 struct if_quotactl *qctl;
7656 char *mnt, *obd_type;
7657 struct obd_dqblk *dqb;
7658 struct option long_opts[] = {
7659 { .val = 'b', .name = "block-softlimit",
7660 .has_arg = required_argument },
7661 { .val = 'B', .name = "block-hardlimit",
7662 .has_arg = required_argument },
7663 { .val = 'd', .name = "default", .has_arg = no_argument },
7664 { .val = 'g', .name = "group", .has_arg = required_argument },
7665 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7666 { .val = 'h', .name = "help", .has_arg = no_argument },
7667 { .val = 'i', .name = "inode-softlimit",
7668 .has_arg = required_argument },
7669 { .val = 'I', .name = "inode-hardlimit",
7670 .has_arg = required_argument },
7671 { .val = 'p', .name = "projid", .has_arg = required_argument },
7672 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7673 { .val = 'u', .name = "user", .has_arg = required_argument },
7674 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7675 { .val = LFS_POOL_OPT,
7676 .name = "pool", .has_arg = required_argument },
7678 unsigned int limit_mask = 0;
7679 bool use_default = false;
7680 int qtype, qctl_len;
7682 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7683 qctl = malloc(qctl_len);
7687 memset(qctl, 0, qctl_len);
7688 obd_type = (char *)qctl->obd_type;
7689 dqb = &qctl->qc_dqblk;
7691 if (has_times_option(argc, argv)) {
7692 rc = lfs_setquota_times(argc, argv, qctl);
7696 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7697 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7698 * so it can be used as a marker that qc_type
7699 * isn't reinitialized from command line
7702 while ((c = getopt_long(argc, argv, "b:B:dg:Ghi:I:p:Pu:U",
7703 long_opts, NULL)) != -1) {
7706 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7709 goto quota_type_def;
7712 rc = name2uid(&qctl->qc_id, optarg);
7715 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7718 goto quota_type_def;
7721 rc = name2gid(&qctl->qc_id, optarg);
7724 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7727 goto quota_type_def;
7730 rc = name2projid(&qctl->qc_id, optarg);
7733 if (str2quotaid(&qctl->qc_id, optarg)) {
7735 "%s setquota: invalid id '%s'\n",
7742 if (qctl->qc_id == 0) {
7744 "%s setquota: can't set quota for root usr/group/project.\n",
7751 if (qctl->qc_type != ALLQUOTA) {
7753 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7758 qctl->qc_type = qtype;
7761 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7765 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7766 dqb->dqb_bsoftlimit >>= 10;
7767 limit_mask |= BSLIMIT;
7768 if (dqb->dqb_bsoftlimit &&
7769 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7771 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7773 (unsigned long long)dqb->dqb_bsoftlimit,
7777 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7778 dqb->dqb_bhardlimit >>= 10;
7779 limit_mask |= BHLIMIT;
7780 if (dqb->dqb_bhardlimit &&
7781 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7783 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7784 "See '%s help setquota' or Lustre manual for details\n",
7786 (unsigned long long)dqb->dqb_bhardlimit,
7790 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7791 limit_mask |= ISLIMIT;
7792 if (dqb->dqb_isoftlimit &&
7793 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7795 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7797 (unsigned long long)dqb->dqb_isoftlimit,
7801 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7802 limit_mask |= IHLIMIT;
7803 if (dqb->dqb_ihardlimit &&
7804 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7806 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7808 (unsigned long long)dqb->dqb_ihardlimit,
7812 if (lfs_verify_poolarg(optarg)) {
7816 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7817 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
7818 LUSTRE_Q_SETDEFAULT_POOL :
7819 LUSTRE_Q_SETQUOTAPOOL;
7823 "%s setquota: unrecognized option '%s'\n",
7824 progname, argv[optind - 1]);
7832 if (qctl->qc_type == ALLQUOTA) {
7834 "%s setquota: either -u or -g must be specified\n",
7840 if (!use_default && limit_mask == 0) {
7842 "%s setquota: at least one limit must be specified\n",
7848 if (use_default && limit_mask != 0) {
7850 "%s setquota: limits should not be specified when using default quota\n",
7856 if (use_default && qctl->qc_id == 0) {
7858 "%s setquota: can not set default quota for root user/group/project\n",
7864 if (optind != argc - 1) {
7866 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7867 progname, argv[optind]);
7875 dqb->dqb_bhardlimit = 0;
7876 dqb->dqb_bsoftlimit = 0;
7877 dqb->dqb_ihardlimit = 0;
7878 dqb->dqb_isoftlimit = 0;
7881 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7882 /* do not set inode limits for Pool Quotas */
7883 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
7884 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
7885 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7886 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7887 /* sigh, we can't just set blimits/ilimits */
7888 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7889 .qc_type = qctl->qc_type,
7890 .qc_id = qctl->qc_id};
7892 rc = llapi_quotactl(mnt, &tmp_qctl);
7896 if (!(limit_mask & BHLIMIT))
7897 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7898 if (!(limit_mask & BSLIMIT))
7899 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7900 if (!(limit_mask & IHLIMIT))
7901 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7902 if (!(limit_mask & ISLIMIT))
7903 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7905 /* Keep grace times if we have got no softlimit arguments */
7906 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7907 dqb->dqb_valid |= QIF_BTIME;
7908 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7911 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7912 dqb->dqb_valid |= QIF_ITIME;
7913 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7917 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7918 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7920 rc = llapi_quotactl(mnt, qctl);
7924 "%s setquota: cannot quotactl '%s' '%s': %s\n",
7926 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7929 "%s setquota: quotactl failed: %s\n",
7930 progname, strerror(-rc));
7934 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7940 /* Converts seconds value into format string
7941 * result is returned in buf
7943 * 1. result is in descenting order: 1w2d3h4m5s
7944 * 2. zero fields are not filled (except for p. 3): 5d1s
7945 * 3. zero seconds value is presented as "0s"
7947 static char *__sec2str(time_t seconds, char *buf)
7949 const char spec[] = "smhdw";
7950 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7955 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7956 c = seconds / mult[i];
7958 if (c > 0 || (i == 0 && buf == tail))
7959 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7968 static void sec2str(time_t seconds, char *buf, int rc)
7975 tail = __sec2str(seconds, tail);
7977 if (rc && tail - buf < 39) {
7983 static void diff2str(time_t seconds, char *buf, time_t now)
7988 if (seconds <= now) {
7989 strcpy(buf, "none");
7992 __sec2str(seconds - now, buf);
7995 static void print_quota_title(char *name, struct if_quotactl *qctl,
7996 bool human_readable, bool show_default)
7999 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
8000 printf("%15s %8s%8s%8s %8s%8s%8s\n",
8001 "Filesystem", "bquota", "blimit", "bgrace",
8002 "iquota", "ilimit", "igrace");
8004 printf("Disk quotas for %s %s (%cid %u):\n",
8005 qtype_name(qctl->qc_type), name,
8006 *qtype_name(qctl->qc_type), qctl->qc_id);
8007 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
8008 "Filesystem", human_readable ? "used" : "kbytes",
8009 "quota", "limit", "grace",
8010 "files", "quota", "limit", "grace");
8014 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8017 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8020 snprintf(buf, buflen, "%5.4gP",
8021 (double)num / ((__u64)1 << 40));
8023 snprintf(buf, buflen, "%5.4gT",
8024 (double)num / (1 << 30));
8026 snprintf(buf, buflen, "%5.4gG",
8027 (double)num / (1 << 20));
8029 snprintf(buf, buflen, "%5.4gM",
8030 (double)num / (1 << 10));
8032 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8036 #ifdef HAVE_NATIVE_CLIENT
8037 /* In the current Lustre implementation, the grace time is either the time
8038 * or the timestamp to be used after some quota ID exceeds the soft limt,
8039 * 48 bits should be enough, its high 16 bits can be used as quota flags.
8041 #define LQUOTA_GRACE_BITS 48
8042 #define LQUOTA_GRACE_MASK ((1ULL << LQUOTA_GRACE_BITS) - 1)
8043 #define LQUOTA_GRACE_MAX LQUOTA_GRACE_MASK
8044 #define LQUOTA_GRACE(t) (t & LQUOTA_GRACE_MASK)
8045 #define LQUOTA_FLAG(t) (t >> LQUOTA_GRACE_BITS)
8046 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8049 #define STRBUF_LEN 24
8050 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8051 int rc, bool h, bool show_default)
8057 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8058 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8059 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8060 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8061 int bover = 0, iover = 0;
8062 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8063 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8065 char strbuf[STRBUF_LEN];
8067 if (dqb->dqb_bhardlimit &&
8068 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8070 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8071 if (dqb->dqb_btime > now)
8077 if (dqb->dqb_ihardlimit &&
8078 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8080 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8081 if (dqb->dqb_itime > now)
8087 if (strlen(mnt) > 15)
8088 printf("%s\n%15s", mnt, "");
8090 printf("%15s", mnt);
8093 diff2str(dqb->dqb_btime, timebuf, now);
8094 else if (show_default)
8095 snprintf(timebuf, sizeof(timebuf), "%llu",
8096 (unsigned long long)dqb->dqb_btime);
8098 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8099 strbuf, sizeof(strbuf), h);
8100 if (rc == -EREMOTEIO)
8101 sprintf(numbuf[0], "%s*", strbuf);
8103 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8104 "%s" : "[%s]", strbuf);
8106 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8107 if (type == QC_GENERAL)
8108 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8109 "%s" : "[%s]", strbuf);
8111 sprintf(numbuf[1], "%s", "-");
8113 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8114 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8115 "%s" : "[%s]", strbuf);
8118 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8120 printf(" %7s%c %6s %7s %7s",
8121 numbuf[0], bover ? '*' : ' ', numbuf[1],
8122 numbuf[2], bover > 1 ? timebuf : "-");
8125 diff2str(dqb->dqb_itime, timebuf, now);
8126 else if (show_default)
8127 snprintf(timebuf, sizeof(timebuf), "%llu",
8128 (unsigned long long)dqb->dqb_itime);
8130 snprintf(numbuf[0], sizeof(numbuf),
8131 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8132 (uintmax_t)dqb->dqb_curinodes);
8134 if (type == QC_GENERAL)
8135 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8137 (uintmax_t)dqb->dqb_isoftlimit);
8139 sprintf(numbuf[1], "%s", "-");
8141 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8142 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8145 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8146 else if (type != QC_OSTIDX)
8147 printf(" %7s%c %6s %7s %7s",
8148 numbuf[0], iover ? '*' : ' ', numbuf[1],
8149 numbuf[2], iover > 1 ? timebuf : "-");
8151 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8153 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8154 qctl->qc_cmd == Q_GETOINFO) {
8158 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8159 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8161 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8162 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8163 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8165 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8167 printf("Block grace time: %s; Inode grace time: %s\n",
8168 bgtimebuf, igtimebuf);
8172 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8176 /* format is "lustre-OST0001" */
8177 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8179 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8184 *idx = strtoul(dash, &endp, 16);
8185 if (*idx > 0xffff) {
8186 fprintf(stderr, "wrong index %s\n", tgtname);
8193 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8194 bool h, __u64 *total)
8196 int rc = 0, rc1 = 0, count = 0, i = 0;
8197 char **list = NULL, *buffer = NULL;
8198 __u32 valid = qctl->qc_valid;
8200 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8203 /* Is it correct for the case OST0000, OST0002, OST0003 -
8204 * we will ask OST0001 that is absent and won't ask OST0003? */
8205 rc = llapi_get_obd_count(mnt, &count, is_mdt);
8207 fprintf(stderr, "can not get %s count: %s\n",
8208 is_mdt ? "mdt" : "ost", strerror(-rc));
8212 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8213 char fname[PATH_MAX];
8214 char fsname[LUSTRE_MAXFSNAME + 1];
8215 int bufsize = sizeof(struct obd_uuid) * count;
8217 rc = llapi_search_fsname(mnt, fsname);
8219 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8223 buffer = malloc(bufsize + sizeof(*list) * count);
8226 list = (char **)(buffer + bufsize);
8227 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8228 count = llapi_get_poolmembers(fname, list, count,
8234 for (i = 0; i < count; i++) {
8235 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8238 if (tgt_name2index(list[i], &index))
8240 qctl->qc_idx = index;
8245 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8246 rc = llapi_quotactl(mnt, qctl);
8248 /* It is remote client case. */
8249 if (rc == -EOPNOTSUPP) {
8256 fprintf(stderr, "quotactl %s%d failed.\n",
8257 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8261 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8262 qctl->qc_valid, 0, h, false);
8263 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8264 qctl->qc_dqblk.dqb_bhardlimit;
8269 qctl->qc_valid = valid;
8273 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8274 int verbose, int quiet, bool human_readable,
8277 int rc1 = 0, rc2 = 0, rc3 = 0;
8278 char *obd_type = (char *)qctl->obd_type;
8279 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8280 __u64 total_ialloc = 0, total_balloc = 0;
8281 bool use_default_for_blk = false;
8282 bool use_default_for_file = false;
8285 rc1 = llapi_quotactl(mnt, qctl);
8289 fprintf(stderr, "%s quotas are not enabled.\n",
8290 qtype_name(qctl->qc_type));
8293 fprintf(stderr, "Permission denied.\n");
8296 /* We already got error message. */
8299 fprintf(stderr, "Unexpected quotactl error: %s\n",
8304 if (!show_default && qctl->qc_id == 0) {
8305 qctl->qc_dqblk.dqb_bhardlimit = 0;
8306 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8307 qctl->qc_dqblk.dqb_ihardlimit = 0;
8308 qctl->qc_dqblk.dqb_isoftlimit = 0;
8309 qctl->qc_dqblk.dqb_btime = 0;
8310 qctl->qc_dqblk.dqb_itime = 0;
8311 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8314 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8315 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8316 use_default_for_blk = true;
8317 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8320 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8321 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8322 use_default_for_file = true;
8323 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8326 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8327 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8328 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8329 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8330 print_quota_title(name, qctl, human_readable, show_default);
8332 if (rc1 && *obd_type)
8333 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8335 if (qctl->qc_valid != QC_GENERAL)
8338 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8339 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8340 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8341 (QIF_LIMITS|QIF_USAGE));
8343 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8345 if (!show_default && verbose &&
8346 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8347 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8348 char strbuf[STRBUF_LEN];
8350 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8352 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8354 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8356 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8357 (uintmax_t)total_ialloc, strbuf);
8360 if (use_default_for_blk)
8361 printf("%cid %u is using default block quota setting\n",
8362 *qtype_name(qctl->qc_type), qctl->qc_id);
8364 if (use_default_for_file)
8365 printf("%cid %u is using default file quota setting\n",
8366 *qtype_name(qctl->qc_type), qctl->qc_id);
8368 if (rc1 || rc2 || rc3 || inacc)
8369 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8383 static int lfs_project(int argc, char **argv)
8385 int ret = 0, err = 0, c, i;
8386 struct project_handle_control phc = { 0 };
8387 enum lfs_project_ops_t op;
8390 phc.assign_projid = false;
8391 /* default action */
8392 op = LFS_PROJECT_LIST;
8394 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8397 if (op != LFS_PROJECT_LIST) {
8399 "%s: cannot specify '-c' '-C' '-s' together\n",
8404 op = LFS_PROJECT_CHECK;
8407 if (op != LFS_PROJECT_LIST) {
8409 "%s: cannot specify '-c' '-C' '-s' together\n",
8414 op = LFS_PROJECT_CLEAR;
8417 if (op != LFS_PROJECT_LIST) {
8419 "%s: cannot specify '-c' '-C' '-s' together\n",
8424 phc.set_inherit = true;
8425 op = LFS_PROJECT_SET;
8431 phc.keep_projid = true;
8434 phc.recursive = true;
8437 if (str2quotaid(&phc.projid, optarg)) {
8439 "Invalid project ID: %s\n",
8444 phc.assign_projid = true;
8448 phc.newline = false;
8451 fprintf(stderr, "%s: invalid option '%c'\n",
8457 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8458 op = LFS_PROJECT_SET;
8459 phc.set_projid = true;
8460 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8461 phc.set_projid = true;
8465 case LFS_PROJECT_CHECK:
8466 if (phc.keep_projid) {
8468 "%s: '-k' is useless together with '-c'\n",
8473 case LFS_PROJECT_CLEAR:
8476 "%s: '-0' is useless together with '-C'\n",
8480 if (phc.assign_projid) {
8482 "%s: '-p' is useless together with '-C'\n",
8487 case LFS_PROJECT_SET:
8490 "%s: '-0' is useless together with '-s'\n",
8494 if (phc.keep_projid) {
8496 "%s: '-k' is useless together with '-s'\n",
8504 "%s: '-0' is useless for list operations\n",
8514 fprintf(stderr, "%s: missing file or directory target(s)\n",
8519 for (i = 0; i < argc; i++) {
8521 case LFS_PROJECT_CHECK:
8522 err = lfs_project_check(argv[i], &phc);
8524 case LFS_PROJECT_LIST:
8525 err = lfs_project_list(argv[i], &phc);
8527 case LFS_PROJECT_CLEAR:
8528 err = lfs_project_clear(argv[i], &phc);
8530 case LFS_PROJECT_SET:
8531 err = lfs_project_set(argv[i], &phc);
8543 static int lfs_quota(int argc, char **argv)
8546 char *mnt, *name = NULL;
8547 struct if_quotactl *qctl;
8549 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8550 __u32 valid = QC_GENERAL, idx = 0;
8551 bool human_readable = false;
8552 bool show_default = false;
8554 bool show_pools = false;
8555 struct option long_opts[] = {
8556 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
8558 char **poollist = NULL;
8562 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8566 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8567 qctl->qc_type = ALLQUOTA;
8568 obd_uuid = (char *)qctl->obd_uuid.uuid;
8570 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8571 long_opts, NULL)) != -1) {
8574 show_default = true;
8579 show_default = true;
8584 show_default = true;
8588 if (qctl->qc_type != ALLQUOTA) {
8590 "%s quota: only one of -u, -g, or -p may be specified\n",
8595 qctl->qc_type = qtype;
8598 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8601 valid = qctl->qc_valid = QC_UUID;
8602 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8605 valid = qctl->qc_valid = QC_MDTIDX;
8606 idx = qctl->qc_idx = atoi(optarg);
8607 if (idx == 0 && *optarg != '0') {
8609 "%s quota: invalid MDT index '%s'\n",
8616 valid = qctl->qc_valid = QC_OSTIDX;
8617 idx = qctl->qc_idx = atoi(optarg);
8618 if (idx == 0 && *optarg != '0') {
8620 "%s quota: invalid OST index '%s'\n",
8633 human_readable = true;
8636 if ((!optarg) && (argv[optind] != NULL) &&
8637 (argv[optind][0] != '-') &&
8638 (argv[optind][0] != '/')) {
8639 optarg = argv[optind++];
8640 if (lfs_verify_poolarg(optarg)) {
8644 strncpy(qctl->qc_poolname, optarg,
8646 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
8647 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
8649 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8653 /* optarg is NULL */
8655 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8658 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8659 progname, argv[optind - 1]);
8665 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8666 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8667 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8668 qctl->qc_type == ALLQUOTA &&
8669 optind == argc - 1 && !show_default) {
8672 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8673 qctl->qc_type = qtype;
8674 qctl->qc_valid = valid;
8675 if (qtype == USRQUOTA) {
8676 qctl->qc_id = geteuid();
8677 rc = uid2name(&name, qctl->qc_id);
8679 qctl->qc_id = getegid();
8680 rc = gid2name(&name, qctl->qc_id);
8681 memset(&qctl->qc_dqblk, 0,
8682 sizeof(qctl->qc_dqblk));
8687 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8688 human_readable, show_default);
8693 /* lfs quota -u username /path/to/lustre/mount */
8694 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8695 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8696 /* options should be followed by u/g-name and mntpoint */
8697 if ((!show_default && optind + 2 != argc) ||
8698 (show_default && optind + 1 != argc) ||
8699 qctl->qc_type == ALLQUOTA) {
8701 "%s quota: name and mount point must be specified\n",
8707 if (!show_default) {
8708 name = argv[optind++];
8709 switch (qctl->qc_type) {
8711 rc = name2uid(&qctl->qc_id, name);
8714 rc = name2gid(&qctl->qc_id, name);
8717 rc = name2projid(&qctl->qc_id, name);
8724 qctl->qc_valid = QC_GENERAL;
8725 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8726 LUSTRE_Q_GETDEFAULT_POOL :
8727 LUSTRE_Q_GETDEFAULT;
8732 if (str2quotaid(&qctl->qc_id, name)) {
8733 fprintf(stderr, "%s quota: invalid id '%s'\n",
8739 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8740 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8751 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
8755 for (i = 0; i < poolcount; i++) {
8756 p = memchr(poollist[i], '.', MAXNAMLEN);
8758 fprintf(stderr, "bad string format %.*s\n",
8759 MAXNAMLEN, poollist[i]);
8764 printf("Quotas for pool: %s\n", p);
8765 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
8766 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8767 human_readable, show_default);
8774 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8775 human_readable, show_default);
8781 #endif /* HAVE_SYS_QUOTA_H! */
8783 static int flushctx_ioctl(char *mp)
8787 fd = open(mp, O_RDONLY);
8789 fprintf(stderr, "flushctx: error open %s: %s\n",
8790 mp, strerror(errno));
8794 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8796 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8797 mp, strerror(errno));
8803 static int lfs_flushctx(int argc, char **argv)
8805 int kdestroy = 0, reap = 0, c;
8806 char mntdir[PATH_MAX] = {'\0'};
8810 while ((c = getopt(argc, argv, "kr")) != -1) {
8820 "error: %s: option '-%c' unrecognized\n",
8827 rc = system("kdestroy > /dev/null");
8829 rc = WEXITSTATUS(rc);
8831 "error destroying tickets: %d, continuing\n",
8836 if (optind >= argc) {
8837 /* flush for all mounted lustre fs. */
8838 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8839 /* Check if we have a mount point */
8840 if (mntdir[0] == '\0')
8843 if (flushctx_ioctl(mntdir))
8846 mntdir[0] = '\0'; /* avoid matching in next loop */
8849 /* flush fs as specified */
8850 while (optind < argc) {
8851 if (flushctx_ioctl(argv[optind++]))
8857 rc = system("keyctl reap > /dev/null");
8859 rc = WEXITSTATUS(rc);
8860 fprintf(stderr, "error reaping keyring: %d\n", rc);
8867 static int lfs_changelog(int argc, char **argv)
8869 void *changelog_priv;
8870 struct changelog_rec *rec;
8871 long long startrec = 0, endrec = 0;
8873 struct option long_opts[] = {
8874 { .val = 'f', .name = "follow", .has_arg = no_argument },
8876 char short_opts[] = "f";
8879 while ((rc = getopt_long(argc, argv, short_opts,
8880 long_opts, NULL)) != -1) {
8887 "%s changelog: unrecognized option '%s'\n",
8888 progname, argv[optind - 1]);
8892 if (optind >= argc) {
8893 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8898 mdd = argv[optind++];
8899 if (argc > optind) {
8901 startrec = strtoll(argv[optind++], NULL, 10);
8902 if (errno != 0 || startrec < 0) {
8904 "%s changelog: bad startrec\n",
8910 if (argc > optind) {
8912 endrec = strtoll(argv[optind++], NULL, 10);
8913 if (errno != 0 || endrec < 0) {
8915 "%s changelog: bad endrec\n",
8921 rc = llapi_changelog_start(&changelog_priv,
8922 CHANGELOG_FLAG_BLOCK |
8923 CHANGELOG_FLAG_JOBID |
8924 CHANGELOG_FLAG_EXTRA_FLAGS |
8925 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8928 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8929 progname, strerror(errno = -rc));
8933 rc = llapi_changelog_set_xflags(changelog_priv,
8934 CHANGELOG_EXTRA_FLAG_UIDGID |
8935 CHANGELOG_EXTRA_FLAG_NID |
8936 CHANGELOG_EXTRA_FLAG_OMODE |
8937 CHANGELOG_EXTRA_FLAG_XATTR);
8940 "%s changelog: cannot set xflags for changelog: %s\n",
8941 progname, strerror(errno = -rc));
8945 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8949 if (endrec && rec->cr_index > endrec) {
8950 llapi_changelog_free(&rec);
8953 if (rec->cr_index < startrec) {
8954 llapi_changelog_free(&rec);
8958 secs = rec->cr_time >> 30;
8959 gmtime_r(&secs, &ts);
8960 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8961 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8962 changelog_type2str(rec->cr_type),
8963 ts.tm_hour, ts.tm_min, ts.tm_sec,
8964 (int)(rec->cr_time & ((1 << 30) - 1)),
8965 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8966 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8968 if (rec->cr_flags & CLF_JOBID) {
8969 struct changelog_ext_jobid *jid =
8970 changelog_rec_jobid(rec);
8972 if (jid->cr_jobid[0] != '\0')
8973 printf(" j=%s", jid->cr_jobid);
8976 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8977 struct changelog_ext_extra_flags *ef =
8978 changelog_rec_extra_flags(rec);
8980 printf(" ef=0x%llx",
8981 (unsigned long long)ef->cr_extra_flags);
8983 if (ef->cr_extra_flags & CLFE_UIDGID) {
8984 struct changelog_ext_uidgid *uidgid =
8985 changelog_rec_uidgid(rec);
8987 printf(" u=%llu:%llu",
8988 (unsigned long long)uidgid->cr_uid,
8989 (unsigned long long)uidgid->cr_gid);
8991 if (ef->cr_extra_flags & CLFE_NID) {
8992 struct changelog_ext_nid *nid =
8993 changelog_rec_nid(rec);
8996 libcfs_nid2str(nid->cr_nid));
8999 if (ef->cr_extra_flags & CLFE_OPEN) {
9000 struct changelog_ext_openmode *omd =
9001 changelog_rec_openmode(rec);
9002 char mode[] = "---";
9004 /* exec mode must be exclusive */
9005 if (omd->cr_openflags & MDS_FMODE_EXEC) {
9008 if (omd->cr_openflags & MDS_FMODE_READ)
9010 if (omd->cr_openflags &
9017 if (strcmp(mode, "---") != 0)
9018 printf(" m=%s", mode);
9021 if (ef->cr_extra_flags & CLFE_XATTR) {
9022 struct changelog_ext_xattr *xattr =
9023 changelog_rec_xattr(rec);
9025 if (xattr->cr_xattr[0] != '\0')
9026 printf(" x=%s", xattr->cr_xattr);
9030 if (!fid_is_zero(&rec->cr_pfid))
9031 printf(" p="DFID, PFID(&rec->cr_pfid));
9032 if (rec->cr_namelen)
9033 printf(" %.*s", rec->cr_namelen,
9034 changelog_rec_name(rec));
9036 if (rec->cr_flags & CLF_RENAME) {
9037 struct changelog_ext_rename *rnm =
9038 changelog_rec_rename(rec);
9040 if (!fid_is_zero(&rnm->cr_sfid))
9041 printf(" s="DFID" sp="DFID" %.*s",
9042 PFID(&rnm->cr_sfid),
9043 PFID(&rnm->cr_spfid),
9044 (int)changelog_rec_snamelen(rec),
9045 changelog_rec_sname(rec));
9049 llapi_changelog_free(&rec);
9052 llapi_changelog_fini(&changelog_priv);
9055 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9056 progname, strerror(errno = -rc));
9058 return (rc == 1 ? 0 : rc);
9061 static int lfs_changelog_clear(int argc, char **argv)
9070 endrec = strtoll(argv[3], NULL, 10);
9071 if (errno != 0 || endrec < 0) {
9073 "%s: bad endrec '%s'\n",
9078 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9081 fprintf(stderr, "%s: record out of range: %llu\n",
9083 else if (rc == -ENOENT)
9084 fprintf(stderr, "%s: no changelog user: %s\n",
9087 fprintf(stderr, "%s error: %s\n", argv[0],
9096 static void rstripc(char *str, int c)
9098 char *end = str + strlen(str);
9100 for (; str < end && end[-1] == c; --end)
9104 static int lfs_fid2path(int argc, char **argv)
9106 struct option long_opts[] = {
9107 { .val = 'c', .name = "cur", .has_arg = no_argument },
9108 { .val = 'c', .name = "current", .has_arg = no_argument },
9109 { .val = 'c', .name = "print-link", .has_arg = no_argument },
9110 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
9111 { .val = 'l', .name = "link", .has_arg = required_argument },
9113 char short_opts[] = "cfl:pr:";
9114 bool print_link = false;
9115 bool print_fid = false;
9117 char mnt_dir[PATH_MAX] = "";
9119 char *path_or_fsname;
9120 long long recno = -1;
9122 char *endptr = NULL;
9127 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9137 linkno = strtol(optarg, &endptr, 10);
9138 if (errno != 0 || *endptr != '\0' || linkno < 0) {
9140 "%s fid2path: invalid linkno '%s'\n",
9146 /* recno is something to do with changelogs
9147 * that was never implemented. We just pass it
9148 * through for the MDT to ignore.
9151 recno = strtoll(optarg, &endptr, 10);
9152 if (errno != 0 || *endptr != '\0' || recno < 0) {
9154 "%s fid2path: invalid recno '%s'\n",
9161 "%s fid2path: unrecognized option '%s'\n",
9162 progname, argv[optind - 1]);
9167 if (argc - optind < 2) {
9169 "Usage: %s fid2path FSNAME|ROOT FID...\n",
9174 path_or_fsname = argv[optind];
9176 if (*path_or_fsname == '/') {
9177 print_mnt_dir = true;
9178 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9180 print_mnt_dir = false;
9181 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9186 "%s fid2path: cannot resolve mount point for '%s': %s\n",
9187 progname, path_or_fsname, strerror(-rc));
9191 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9194 "%s fid2path: cannot open mount point for '%s': %s\n",
9195 progname, path_or_fsname, strerror(-rc));
9199 /* Strip trailing slashes from mnt_dir. */
9200 rstripc(mnt_dir + 1, '/');
9202 for (i = optind + 1; i < argc; i++) {
9203 const char *fid_str = argv[i];
9207 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9210 "%s fid2path: invalid FID '%s'\n",
9218 int linktmp = (linkno >= 0) ? linkno : 0;
9220 int oldtmp = linktmp;
9221 long long rectmp = recno;
9222 char path_buf[PATH_MAX];
9224 rc2 = llapi_fid2path_at(mnt_fd, &fid,
9225 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9228 "%s fid2path: cannot find %s %s: %s\n",
9229 progname, path_or_fsname, fid_str,
9237 printf("%s ", fid_str);
9240 printf("%d ", linktmp);
9242 /* You may think this looks wrong or weird (and it is!)
9243 * but we are actually trying to preserve the old quirky
9244 * behaviors (enforced by our old quirky tests!) that
9245 * make lfs so much fun to work on:
9247 * lustre 0x200000007:0x1:0x0 => "/"
9248 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9250 * Note that llapi_fid2path() returns "" for the root
9254 print_mnt_dir ? mnt_dir : "",
9255 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9259 /* specified linkno */
9262 if (oldtmp == linktmp)
9274 static int lfs_path2fid(int argc, char **argv)
9276 struct option long_opts[] = {
9277 { .val = 'p', .name = "parents", .has_arg = no_argument },
9280 const char short_opts[] = "p";
9281 const char *sep = "";
9284 bool show_parents = false;
9286 while ((rc = getopt_long(argc, argv, short_opts,
9287 long_opts, NULL)) != -1) {
9290 show_parents = true;
9294 "%s path2fid: unrecognized option '%s'\n",
9295 progname, argv[optind - 1]);
9300 if (optind > argc - 1) {
9301 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9304 } else if (optind < argc - 1) {
9309 for (path = argv + optind; *path != NULL; path++) {
9312 if (!show_parents) {
9313 err = llapi_path2fid(*path, &fid);
9315 printf("%s%s"DFID"\n",
9316 *sep != '\0' ? *path : "", sep,
9319 char name[NAME_MAX + 1];
9320 unsigned int linkno = 0;
9322 while ((err = llapi_path2parent(*path, linkno, &fid,
9323 name, sizeof(name))) == 0) {
9324 if (*sep != '\0' && linkno == 0)
9325 printf("%s%s", *path, sep);
9327 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9332 /* err == -ENODATA is end-of-loop */
9333 if (linkno > 0 && err == -ENODATA) {
9341 "%s path2fid: cannot get %sfid for '%s': %s\n",
9342 progname, show_parents ? "parent " : "", *path,
9354 #define MAX_ERRNO 4095
9355 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9357 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9361 rc = llapi_rmfid(device, fa);
9363 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9364 progname, strerror(-rc));
9368 for (k = 0; k < fa->fa_nr; k++) {
9369 rc2 = (__s32)fa->fa_fids[k].f_ver;
9370 if (!IS_ERR_VALUE(rc2))
9376 fa->fa_fids[k].f_ver = 0;
9377 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9378 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9384 static int lfs_rmfid(int argc, char **argv)
9386 char *fidstr, *device;
9387 int rc = 0, rc2, nr;
9388 struct fid_array *fa;
9390 if (optind > argc - 1) {
9391 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9395 device = argv[optind++];
9398 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9404 while (optind < argc) {
9407 fidstr = argv[optind++];
9408 while (*fidstr == '[')
9410 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9412 fprintf(stderr, "unrecognized FID: %s\n",
9417 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9418 /* start another batch */
9419 rc2 = lfs_rmfid_and_show_errors(device, fa);
9426 rc2 = lfs_rmfid_and_show_errors(device, fa);
9434 static int lfs_data_version(int argc, char **argv)
9436 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9444 fprintf(stderr, "%s: FILE must be specified\n",
9449 while ((c = getopt(argc, argv, "hnrw")) != -1) {
9452 data_version_flags = 0;
9455 data_version_flags |= LL_DV_RD_FLUSH;
9458 data_version_flags |= LL_DV_WR_FLUSH;
9462 "%s data_version: unrecognized option '%s'\n",
9463 progname, argv[optind - 1]);
9469 if (optind == argc) {
9470 fprintf(stderr, "%s data_version: FILE must be specified\n",
9475 path = argv[optind];
9476 fd = open(path, O_RDONLY);
9479 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9480 progname, path, strerror(-rc));
9484 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9487 "%s data_version: cannot get version for '%s': %s\n",
9488 progname, path, strerror(-rc));
9490 printf("%ju" "\n", (uintmax_t)data_version);
9496 static int lfs_hsm_state(int argc, char **argv)
9501 struct hsm_user_state hus;
9509 rc = llapi_hsm_state_get(path, &hus);
9511 fprintf(stderr, "can't get hsm state for %s: %s\n",
9512 path, strerror(errno = -rc));
9516 /* Display path name and status flags */
9517 printf("%s: (0x%08x)", path, hus.hus_states);
9519 if (hus.hus_states & HS_RELEASED)
9520 printf(" released");
9521 if (hus.hus_states & HS_EXISTS)
9523 if (hus.hus_states & HS_DIRTY)
9525 if (hus.hus_states & HS_ARCHIVED)
9526 printf(" archived");
9527 /* Display user-settable flags */
9528 if (hus.hus_states & HS_NORELEASE)
9529 printf(" never_release");
9530 if (hus.hus_states & HS_NOARCHIVE)
9531 printf(" never_archive");
9532 if (hus.hus_states & HS_LOST)
9533 printf(" lost_from_hsm");
9535 if (hus.hus_archive_id != 0)
9536 printf(", archive_id:%d", hus.hus_archive_id);
9539 } while (++i < argc);
9544 #define LFS_HSM_SET 0
9545 #define LFS_HSM_CLEAR 1
9548 * Generic function to set or clear HSM flags.
9549 * Used by hsm_set and hsm_clear.
9551 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9553 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9555 struct option long_opts[] = {
9556 { .val = 'A', .name = "archived", .has_arg = no_argument },
9557 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
9558 { .val = 'd', .name = "dirty", .has_arg = no_argument },
9559 { .val = 'e', .name = "exists", .has_arg = no_argument },
9560 { .val = 'h', .name = "help", .has_arg = no_argument },
9561 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
9562 { .val = 'l', .name = "lost", .has_arg = no_argument },
9563 { .val = 'r', .name = "norelease", .has_arg = no_argument },
9568 __u32 archive_id = 0;
9574 while ((c = getopt_long(argc, argv, "aAdehi:lr",
9575 long_opts, NULL)) != -1) {
9581 mask |= HS_NOARCHIVE;
9584 mask |= HS_ARCHIVED;
9587 mask |= HS_NORELEASE;
9597 archive_id = strtol(optarg, &end, 10);
9598 if (errno != 0 || *end != '\0' || archive_id < 0) {
9600 "%s: invalid archive_id: '%s'\n",
9606 fprintf(stderr, "%s: unrecognized option '%s'\n",
9607 progname, argv[optind - 1]);
9614 /* User should have specified a flag */
9618 while (optind < argc) {
9619 path = argv[optind];
9621 /* If mode == 0, this means we apply the mask. */
9622 if (mode == LFS_HSM_SET)
9623 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9625 rc = llapi_hsm_state_set(path, 0, mask, 0);
9628 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9629 path, strerror(errno = -rc));
9638 static int lfs_hsm_action(int argc, char **argv)
9643 struct hsm_current_action hca;
9644 struct hsm_extent he;
9645 enum hsm_user_action hua;
9646 enum hsm_progress_states hps;
9654 rc = llapi_hsm_current_action(path, &hca);
9656 fprintf(stderr, "can't get hsm action for %s: %s\n",
9657 path, strerror(errno = -rc));
9660 he = hca.hca_location;
9661 hua = hca.hca_action;
9662 hps = hca.hca_state;
9664 printf("%s: %s", path, hsm_user_action2name(hua));
9666 /* Skip file without action */
9667 if (hca.hca_action == HUA_NONE) {
9672 printf(" %s ", hsm_progress_state2name(hps));
9674 if ((hps == HPS_RUNNING) &&
9675 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9676 printf("(%llu bytes moved)\n",
9677 (unsigned long long)he.length);
9678 else if ((he.offset + he.length) == LUSTRE_EOF)
9679 printf("(from %llu to EOF)\n",
9680 (unsigned long long)he.offset);
9682 printf("(from %llu to %llu)\n",
9683 (unsigned long long)he.offset,
9684 (unsigned long long)(he.offset + he.length));
9686 } while (++i < argc);
9691 static int lfs_hsm_set(int argc, char **argv)
9693 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9696 static int lfs_hsm_clear(int argc, char **argv)
9698 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9702 * Check file state and return its fid, to be used by lfs_hsm_request().
9704 * \param[in] file Path to file to check
9705 * \param[in,out] fid Pointer to allocated lu_fid struct.
9706 * \param[in,out] last_dev Pointer to last device id used.
9708 * \return 0 on success.
9710 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9716 rc = lstat(file, &st);
9718 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9722 * Checking for regular file as archiving as posix copytool
9723 * rejects archiving files other than regular files
9725 if (!S_ISREG(st.st_mode)) {
9726 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9729 /* A request should be ... */
9730 if (*last_dev != st.st_dev && *last_dev != 0) {
9732 "All files should be on the same filesystem: %s\n",
9736 *last_dev = st.st_dev;
9738 rc = llapi_path2fid(file, fid);
9740 fprintf(stderr, "Cannot read FID of %s: %s\n",
9741 file, strerror(-rc));
9747 /* Fill an HSM HUR item with a given file name.
9749 * If mntpath is set, then the filename is actually a FID, and no
9750 * lookup on the filesystem will be performed.
9752 * \param[in] hur the user request to fill
9753 * \param[in] idx index of the item inside the HUR to fill
9754 * \param[in] mntpath mountpoint of Lustre
9755 * \param[in] fname filename (if mtnpath is NULL)
9756 * or FID (if mntpath is set)
9757 * \param[in] last_dev pointer to last device id used
9759 * \retval 0 on success
9760 * \retval CMD_HELP or a negative errno on error
9762 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9763 const char *mntpath, const char *fname,
9766 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9769 hui->hui_extent.length = -1;
9772 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9774 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9777 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9781 hur->hur_request.hr_itemcount++;
9786 static int lfs_hsm_request(int argc, char **argv, int action)
9788 struct option long_opts[] = {
9789 { .val = 'a', .name = "archive", .has_arg = required_argument },
9790 { .val = 'D', .name = "data", .has_arg = required_argument },
9791 { .val = 'h', .name = "help", .has_arg = no_argument },
9792 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9793 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9796 struct hsm_user_request *hur, *oldhur;
9801 char *filelist = NULL;
9802 char fullpath[PATH_MAX];
9803 char *opaque = NULL;
9807 int nbfile_alloc = 0;
9808 char *some_file = NULL;
9809 char *mntpath = NULL;
9815 while ((c = getopt_long(argc, argv, "a:D:hl:m:",
9816 long_opts, NULL)) != -1) {
9825 if (action != HUA_ARCHIVE &&
9826 action != HUA_REMOVE) {
9828 "error: -a is supported only when archiving or removing\n");
9831 archive_id = atoi(optarg);
9836 some_file = strdup(optarg);
9840 fprintf(stderr, "%s: unrecognized option '%s'\n",
9841 progname, argv[optind - 1]);
9848 /* All remaining args are files, so we have at least nbfile */
9849 nbfile = argc - optind;
9851 if ((nbfile == 0) && (!filelist))
9855 opaque_len = strlen(opaque);
9858 * Alloc the request structure with enough place to store all files
9859 * from command line.
9861 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9863 fprintf(stderr, "Cannot create the request: %s\n",
9867 nbfile_alloc = nbfile;
9869 hur->hur_request.hr_action = action;
9870 hur->hur_request.hr_archive_id = archive_id;
9871 hur->hur_request.hr_flags = 0;
9873 /* All remaining args are files, add them */
9874 if (nbfile != 0 && some_file == NULL)
9875 some_file = strdup(argv[optind]);
9877 for (i = 0; i < nbfile; i++) {
9878 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9884 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9886 /* If a filelist was specified, read the filelist from it. */
9888 fp = fopen(filelist, "r");
9890 fprintf(stderr, "Cannot read the file list %s: %s\n",
9891 filelist, strerror(errno));
9896 while ((rc = getline(&line, &len, fp)) != -1) {
9898 * If allocated buffer was too small, get something
9901 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9904 nbfile_alloc = nbfile_alloc * 2 + 1;
9906 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9910 "hsm: cannot allocate the request: %s\n",
9917 size = hur_len(oldhur);
9920 "hsm: cannot allocate %u files + %u bytes data\n",
9921 oldhur->hur_request.hr_itemcount,
9922 oldhur->hur_request.hr_data_len);
9929 memcpy(hur, oldhur, size);
9934 if (line[strlen(line) - 1] == '\n')
9935 line[strlen(line) - 1] = '\0';
9937 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9938 mntpath, line, &last_dev);
9954 /* If a --data was used, add it to the request */
9955 hur->hur_request.hr_data_len = opaque_len;
9957 memcpy(hur_data(hur), opaque, opaque_len);
9959 /* Send the HSM request */
9960 if (realpath(some_file, fullpath) == NULL) {
9961 fprintf(stderr, "Could not find path '%s': %s\n",
9962 some_file, strerror(errno));
9964 rc = llapi_hsm_request(fullpath, hur);
9966 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9967 some_file, strerror(-rc));
9977 static int lfs_hsm_archive(int argc, char **argv)
9979 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9982 static int lfs_hsm_restore(int argc, char **argv)
9984 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9987 static int lfs_hsm_release(int argc, char **argv)
9989 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9992 static int lfs_hsm_remove(int argc, char **argv)
9994 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9997 static int lfs_hsm_cancel(int argc, char **argv)
9999 return lfs_hsm_request(argc, argv, HUA_CANCEL);
10002 static int lfs_swap_layouts(int argc, char **argv)
10007 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
10008 SWAP_LAYOUTS_KEEP_MTIME |
10009 SWAP_LAYOUTS_KEEP_ATIME);
10012 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
10014 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
10016 int lfs_get_mode(const char *string)
10018 enum lock_mode_user mode;
10020 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10021 if (lock_mode_names[mode] == NULL)
10023 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10030 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10032 enum lu_ladvise_type advice;
10035 advice < ARRAY_SIZE(ladvise_names); advice++) {
10036 if (ladvise_names[advice] == NULL)
10038 if (strcmp(string, ladvise_names[advice]) == 0)
10042 return LU_LADVISE_INVALID;
10045 static int lfs_ladvise(int argc, char **argv)
10047 struct option long_opts[] = {
10048 { .val = 'a', .name = "advice", .has_arg = required_argument },
10049 { .val = 'b', .name = "background", .has_arg = no_argument },
10050 { .val = 'e', .name = "end", .has_arg = required_argument },
10051 { .val = 'h', .name = "help", .has_arg = no_argument },
10052 { .val = 'l', .name = "length", .has_arg = required_argument },
10053 { .val = 'm', .name = "mode", .has_arg = required_argument },
10054 { .val = 's', .name = "start", .has_arg = required_argument },
10055 { .val = 'u', .name = "unset", .has_arg = no_argument },
10056 { .name = NULL } };
10057 struct llapi_lu_ladvise advice;
10058 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10059 unsigned long long start = 0;
10060 unsigned long long end = LUSTRE_EOF;
10061 unsigned long long length = 0;
10062 unsigned long long size_units;
10063 unsigned long long flags = 0;
10069 while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10070 long_opts, NULL)) != -1) {
10073 advice_type = lfs_get_ladvice(optarg);
10074 if (advice_type == LU_LADVISE_INVALID) {
10076 "%s: invalid advice type '%s'\n",
10078 fprintf(stderr, "Valid types:");
10080 for (advice_type = 0;
10081 advice_type < ARRAY_SIZE(ladvise_names);
10083 if (ladvise_names[advice_type] == NULL)
10085 fprintf(stderr, " %s",
10086 ladvise_names[advice_type]);
10088 fprintf(stderr, "\n");
10101 rc = llapi_parse_size(optarg, &end,
10104 fprintf(stderr, "%s: bad end offset '%s'\n",
10111 rc = llapi_parse_size(optarg, &start,
10115 "%s: bad start offset '%s'\n",
10122 rc = llapi_parse_size(optarg, &length,
10125 fprintf(stderr, "%s: bad length '%s'\n",
10131 mode = lfs_get_mode(optarg);
10134 "%s: bad mode '%s', valid modes are READ or WRITE\n",
10140 fprintf(stderr, "%s: unrecognized option '%s'\n",
10141 progname, argv[optind - 1]);
10148 if (advice_type == LU_LADVISE_INVALID) {
10149 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10150 fprintf(stderr, "Valid types:");
10151 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10153 if (ladvise_names[advice_type] == NULL)
10155 fprintf(stderr, " %s", ladvise_names[advice_type]);
10157 fprintf(stderr, "\n");
10161 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10163 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10168 if (argc <= optind) {
10169 fprintf(stderr, "%s: please give one or more file names\n",
10174 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10175 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10180 if (end == LUSTRE_EOF && length != 0)
10181 end = start + length;
10183 if (end <= start) {
10184 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10185 argv[0], start, end);
10189 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10190 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10195 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10196 fprintf(stderr, "%s: mode is required with lockahead\n",
10201 while (optind < argc) {
10204 path = argv[optind++];
10206 fd = open(path, O_RDONLY);
10208 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10209 argv[0], path, strerror(errno));
10214 advice.lla_start = start;
10215 advice.lla_end = end;
10216 advice.lla_advice = advice_type;
10217 advice.lla_value1 = 0;
10218 advice.lla_value2 = 0;
10219 advice.lla_value3 = 0;
10220 advice.lla_value4 = 0;
10221 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10222 advice.lla_lockahead_mode = mode;
10223 advice.lla_peradvice_flags = flags;
10226 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10230 "%s: cannot give advice '%s' to file '%s': %s\n",
10231 argv[0], ladvise_names[advice_type],
10232 path, strerror(errno));
10238 if (rc == 0 && rc2 < 0)
10244 static const char *const heat_names[] = LU_HEAT_NAMES;
10246 static int lfs_heat_get(int argc, char **argv)
10248 struct lu_heat *heat;
10257 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10259 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10264 while (optind < argc) {
10265 path = argv[optind++];
10267 fd = open(path, O_RDONLY);
10269 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10270 argv[0], path, strerror(errno));
10275 heat->lh_count = OBD_HEAT_COUNT;
10276 rc2 = llapi_heat_get(fd, heat);
10280 "%s: cannot get heat of file '%s': %s\n",
10281 argv[0], path, strerror(errno));
10285 printf("flags: %x\n", heat->lh_flags);
10286 for (i = 0; i < heat->lh_count; i++)
10287 printf("%s: %llu\n", heat_names[i],
10288 (unsigned long long)heat->lh_heat[i]);
10290 if (rc == 0 && rc2 < 0)
10298 static int lfs_heat_set(int argc, char **argv)
10300 struct option long_opts[] = {
10301 { .val = 'c', .name = "clear", .has_arg = no_argument },
10302 { .val = 'h', .name = "help", .has_arg = no_argument },
10303 { .val = 'o', .name = "off", .has_arg = no_argument },
10304 { .val = 'O', .name = "on", .has_arg = no_argument },
10305 { .name = NULL } };
10306 enum lu_heat_flag flags = 0;
10316 while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
10319 flags |= LU_HEAT_FLAG_CLEAR;
10322 flags |= LU_HEAT_FLAG_CLEAR;
10323 flags |= LU_HEAT_FLAG_OFF;
10326 flags &= ~LU_HEAT_FLAG_OFF;
10329 fprintf(stderr, "%s: unrecognized option '%s'\n",
10330 progname, argv[optind - 1]);
10337 if (argc <= optind) {
10338 fprintf(stderr, "%s: please give one or more file names\n",
10343 while (optind < argc) {
10344 path = argv[optind++];
10346 fd = open(path, O_RDONLY);
10348 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10349 argv[0], path, strerror(errno));
10354 rc2 = llapi_heat_set(fd, flags);
10358 "%s: cannot setflags heat of file '%s': %s\n",
10359 argv[0], path, strerror(errno));
10363 if (rc == 0 && rc2 < 0)
10370 * The input string contains a comma delimited list of component ids and
10371 * ranges, for example "1,2-4,7".
10373 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10375 bool end_of_loop = false;
10383 while (!end_of_loop) {
10387 char *endptr = NULL;
10390 ptr = strchrnul(arg, ',');
10391 end_of_loop = *ptr == '\0';
10394 start_index = strtol(arg, &endptr, 0);
10395 if (endptr == arg) /* no data at all */
10397 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10399 if (start_index < 0)
10402 end_index = start_index;
10403 if (*endptr == '-') {
10404 end_index = strtol(endptr + 1, &endptr, 0);
10405 if (*endptr != '\0')
10407 if (end_index < start_index)
10411 for (i = start_index; i <= end_index && size > 0; i++) {
10414 /* remove duplicate */
10415 for (j = 0; j < nr; j++) {
10419 if (j == nr) { /* no duplicate */
10425 if (size == 0 && i < end_index)
10432 if (!end_of_loop && ptr)
10435 return rc < 0 ? rc : nr;
10439 * struct verify_mirror_id - Mirror id to be verified.
10440 * @mirror_id: A specified mirror id.
10441 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10443 struct verify_mirror_id {
10449 * compare_mirror_ids() - Compare mirror ids.
10450 * @layout: Mirror component list.
10451 * @cbdata: Callback data in verify_mirror_id structure.
10453 * This is a callback function called by llapi_layout_comp_iterate()
10454 * to compare the specified mirror id with the one in the current
10455 * component of @layout. If they are the same, then the specified
10456 * mirror id is valid.
10458 * Return: a negative error code on failure or
10459 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10460 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
10463 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10465 struct verify_mirror_id *mirror_id_cbdata =
10466 (struct verify_mirror_id *)cbdata;
10467 uint32_t mirror_id;
10470 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10474 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10475 progname, strerror(errno));
10479 if (mirror_id_cbdata->mirror_id == mirror_id) {
10480 mirror_id_cbdata->is_valid_id = true;
10481 return LLAPI_LAYOUT_ITER_STOP;
10484 return LLAPI_LAYOUT_ITER_CONT;
10488 * verify_mirror_ids() - Verify specified mirror ids.
10489 * @fname: Mirrored file name.
10490 * @mirror_ids: Specified mirror ids to be verified.
10491 * @ids_nr: Number of specified mirror ids.
10493 * This function verifies that specified @mirror_ids are valid
10494 * in the mirrored file @fname.
10496 * Return: 0 on success or a negative error code on failure.
10499 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10501 struct llapi_layout *layout = NULL;
10502 struct verify_mirror_id mirror_id_cbdata = { 0 };
10504 uint32_t flr_state;
10513 if (stat(fname, &stbuf) < 0) {
10514 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10515 progname, fname, strerror(errno));
10520 if (!S_ISREG(stbuf.st_mode)) {
10521 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10527 fd = open(fname, O_DIRECT | O_RDONLY);
10529 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10530 progname, fname, strerror(errno));
10535 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10537 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10538 progname, fname, strerror(errno));
10542 layout = llapi_layout_get_by_fd(fd, 0);
10544 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10545 progname, fname, strerror(errno));
10547 llapi_lease_release(fd);
10551 rc = llapi_layout_flags_get(layout, &flr_state);
10553 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10554 progname, fname, strerror(errno));
10559 flr_state &= LCM_FL_FLR_MASK;
10560 switch (flr_state) {
10563 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10564 progname, fname, llapi_layout_flags_string(flr_state));
10571 for (i = 0; i < ids_nr; i++) {
10572 mirror_id_cbdata.mirror_id = mirror_ids[i];
10573 mirror_id_cbdata.is_valid_id = false;
10575 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10576 &mirror_id_cbdata);
10580 "%s: '%s' failed to verify mirror id: %u.\n",
10581 progname, fname, mirror_ids[i]);
10585 if (!mirror_id_cbdata.is_valid_id) {
10588 "%s: '%s' invalid specified mirror id: %u.\n",
10589 progname, fname, mirror_ids[i]);
10595 llapi_layout_free(layout);
10596 llapi_lease_release(fd);
10604 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10605 __u16 *mirror_ids, int ids_nr)
10607 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10608 struct llapi_layout *layout;
10610 uint32_t flr_state;
10619 if (stat(fname, &stbuf) < 0) {
10620 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10621 progname, fname, strerror(errno));
10625 if (!S_ISREG(stbuf.st_mode)) {
10626 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10632 /* Allow mirror resync even without the key on encrypted files */
10633 fd = open(fname, O_DIRECT | O_RDWR | O_FILE_ENC);
10635 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10636 progname, fname, strerror(errno));
10641 layout = llapi_layout_get_by_fd(fd, 0);
10643 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10644 progname, fname, strerror(errno));
10649 rc = llapi_layout_flags_get(layout, &flr_state);
10651 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10652 progname, fname, strerror(errno));
10657 flr_state &= LCM_FL_FLR_MASK;
10658 if (flr_state == LCM_FL_NONE) {
10660 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10665 /* get stale component info */
10666 comp_size = llapi_mirror_find_stale(layout, comp_array,
10667 ARRAY_SIZE(comp_array),
10668 mirror_ids, ids_nr);
10669 if (comp_size <= 0) {
10674 ioc->lil_mode = LL_LEASE_WRLCK;
10675 ioc->lil_flags = LL_LEASE_RESYNC;
10676 rc = llapi_lease_set(fd, ioc);
10678 if (rc == -EALREADY)
10682 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10683 progname, fname, strerror(-rc));
10687 /* get the read range [start, end) */
10688 start = comp_array[0].lrc_start;
10689 end = comp_array[0].lrc_end;
10690 for (idx = 1; idx < comp_size; idx++) {
10691 if (comp_array[idx].lrc_start < start)
10692 start = comp_array[idx].lrc_start;
10693 if (end < comp_array[idx].lrc_end)
10694 end = comp_array[idx].lrc_end;
10697 rc = llapi_lease_check(fd);
10698 if (rc != LL_LEASE_WRLCK) {
10699 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10704 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10707 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10708 progname, fname, strerror(-rc));
10710 rc = migrate_set_timestamps(fd, &stbuf);
10712 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
10713 progname, fname, strerror(-rc));
10717 /* need to do the lease unlock even resync fails */
10718 ioc->lil_mode = LL_LEASE_UNLCK;
10719 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10720 ioc->lil_count = 0;
10721 for (idx = 0; idx < comp_size; idx++) {
10722 if (comp_array[idx].lrc_synced) {
10723 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10728 rc2 = llapi_lease_set(fd, ioc);
10730 * llapi_lease_set returns lease mode when it request to unlock
10734 /* rc2 == 0 means lost lease lock */
10735 if (rc2 == 0 && rc == 0)
10739 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10741 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10743 llapi_lease_release(fd);
10748 llapi_layout_free(layout);
10755 static inline int lfs_mirror_resync(int argc, char **argv)
10757 struct option long_opts[] = {
10758 { .val = 'h', .name = "help", .has_arg = no_argument },
10759 { .val = 'o', .name = "only", .has_arg = required_argument },
10760 { .name = NULL } };
10761 struct ll_ioc_lease *ioc = NULL;
10762 __u16 mirror_ids[128] = { 0 };
10767 while ((c = getopt_long(argc, argv, "ho:", long_opts, NULL)) >= 0) {
10770 rc = parse_mirror_ids(mirror_ids,
10771 sizeof(mirror_ids) / sizeof(__u16),
10775 "%s: bad mirror ids '%s'.\n",
10782 fprintf(stderr, "%s: unrecognized option '%s'\n",
10783 progname, argv[optind - 1]);
10791 if (argc == optind) {
10792 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10797 if (ids_nr > 0 && argc > optind + 1) {
10799 "%s: option '--only' cannot be used upon multiple files.\n",
10806 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10811 /* set the lease on the file */
10812 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10814 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10815 argv[0], strerror(errno));
10820 for (; optind < argc; optind++) {
10821 rc = lfs_mirror_resync_file(argv[optind], ioc,
10822 mirror_ids, ids_nr);
10823 /* ignore previous file's error, continue with next file */
10826 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10834 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10836 struct llapi_layout *layout;
10839 layout = llapi_layout_get_by_fd(fd, 0);
10841 fprintf(stderr, "could not get layout.\n");
10845 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10847 fprintf(stderr, "failed to iterate layout\n");
10848 llapi_layout_free(layout);
10851 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10852 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10853 llapi_layout_free(layout);
10857 llapi_layout_free(layout);
10863 * Check whether two files are the same file
10864 * \retval 0 same file
10865 * \retval 1 not the same file
10866 * \retval <0 error code
10868 static inline int check_same_file(int fd, const char *f2)
10870 struct stat stbuf1;
10871 struct stat stbuf2;
10873 if (fstat(fd, &stbuf1) < 0)
10876 if (stat(f2, &stbuf2) < 0)
10879 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10880 stbuf1.st_ino == stbuf2.st_ino)
10886 static inline int lfs_mirror_read(int argc, char **argv)
10889 __u16 mirror_id = 0;
10890 const char *outfile = NULL;
10896 const size_t buflen = 4 << 20;
10898 struct option long_opts[] = {
10899 { .val = 'h', .name = "help", .has_arg = no_argument },
10900 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10901 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10902 { .name = NULL } };
10904 while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
10909 unsigned long int id;
10912 id = strtoul(optarg, &end, 0);
10913 if (errno != 0 || *end != '\0' || id == 0 ||
10916 "%s %s: invalid mirror ID '%s'\n",
10917 progname, argv[0], optarg);
10921 mirror_id = (__u16)id;
10928 fprintf(stderr, "%s: unrecognized option '%s'\n",
10929 progname, argv[optind - 1]);
10936 if (argc == optind) {
10937 fprintf(stderr, "%s %s: no mirrored file provided\n",
10938 progname, argv[0]);
10940 } else if (argc > optind + 1) {
10941 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10945 if (mirror_id == 0) {
10946 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10947 progname, argv[0]);
10951 /* open mirror file */
10952 fname = argv[optind];
10953 fd = open(fname, O_DIRECT | O_RDONLY);
10955 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10956 progname, argv[0], fname, strerror(errno));
10960 /* verify mirror id */
10961 rc = verify_mirror_id_by_fd(fd, mirror_id);
10964 "%s %s: cannot find mirror with ID %u in '%s'\n",
10965 progname, argv[0], mirror_id, fname);
10969 /* open output file - O_EXCL ensures output is not the same as input */
10971 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10973 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10974 progname, argv[0], outfile, strerror(errno));
10979 outfd = STDOUT_FILENO;
10982 /* allocate buffer */
10983 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10985 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10986 progname, argv[0], rc);
10992 ssize_t bytes_read;
10993 ssize_t written = 0;
10995 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10996 if (bytes_read < 0) {
10999 "%s %s: fail to read data from mirror %u: %s\n",
11000 progname, argv[0], mirror_id, strerror(-rc));
11005 if (bytes_read == 0)
11008 while (written < bytes_read) {
11011 written2 = write(outfd, buf + written,
11012 bytes_read - written);
11013 if (written2 < 0) {
11015 "%s %s: fail to write %s: %s\n",
11016 progname, argv[0], outfile ? : "STDOUT",
11021 written += written2;
11024 if (written != bytes_read) {
11026 "%s %s: written %ld bytes does not match with %ld read.\n",
11027 progname, argv[0], written, bytes_read);
11049 static inline int lfs_mirror_write(int argc, char **argv)
11052 __u16 mirror_id = 0;
11053 const char *inputfile = NULL;
11059 const size_t buflen = 4 << 20;
11061 size_t page_size = sysconf(_SC_PAGESIZE);
11062 struct ll_ioc_lease_id ioc;
11063 struct option long_opts[] = {
11064 { .val = 'h', .name = "help", .has_arg = no_argument },
11065 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
11066 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11067 { .name = NULL } };
11069 while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11074 unsigned long int id;
11077 id = strtoul(optarg, &end, 0);
11078 if (errno != 0 || *end != '\0' || id == 0 ||
11081 "%s %s: invalid mirror ID '%s'\n",
11082 progname, argv[0], optarg);
11086 mirror_id = (__u16)id;
11090 inputfile = optarg;
11093 fprintf(stderr, "%s: unrecognized option '%s'\n",
11094 progname, argv[optind - 1]);
11101 if (argc == optind) {
11102 fprintf(stderr, "%s %s: no mirrored file provided\n",
11103 progname, argv[0]);
11105 } else if (argc > optind + 1) {
11106 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11110 if (mirror_id == 0) {
11111 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11112 progname, argv[0]);
11116 /* open mirror file */
11117 fname = argv[optind];
11118 fd = open(fname, O_DIRECT | O_WRONLY);
11120 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11121 progname, argv[0], fname, strerror(errno));
11125 /* verify mirror id */
11126 rc = verify_mirror_id_by_fd(fd, mirror_id);
11129 "%s %s: cannot find mirror with ID %u in '%s'\n",
11130 progname, argv[0], mirror_id, fname);
11134 /* open input file */
11136 rc = check_same_file(fd, inputfile);
11139 "%s %s: input file cannot be the mirrored file\n",
11140 progname, argv[0]);
11146 inputfd = open(inputfile, O_RDONLY, 0644);
11148 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11149 progname, argv[0], inputfile, strerror(errno));
11154 inputfd = STDIN_FILENO;
11157 /* allocate buffer */
11158 rc = posix_memalign(&buf, page_size, buflen);
11160 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11161 progname, argv[0], rc);
11162 goto close_inputfd;
11165 /* prepare target mirror components instantiation */
11166 ioc.lil_mode = LL_LEASE_WRLCK;
11167 ioc.lil_flags = LL_LEASE_RESYNC;
11168 ioc.lil_mirror_id = mirror_id;
11169 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11172 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11173 progname, argv[0], fname, strerror(errno));
11179 ssize_t bytes_read;
11183 rc = llapi_lease_check(fd);
11184 if (rc != LL_LEASE_WRLCK) {
11185 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11186 progname, argv[0], fname);
11190 bytes_read = read(inputfd, buf, buflen);
11191 if (bytes_read < 0) {
11194 "%s %s: fail to read data from '%s': %s\n",
11195 progname, argv[0], inputfile ? : "STDIN",
11202 if (bytes_read == 0)
11205 /* round up to page align to make direct IO happy. */
11206 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11208 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11213 "%s %s: fail to write to mirror %u: %s\n",
11214 progname, argv[0], mirror_id,
11222 if (pos & (page_size - 1)) {
11223 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11228 ioc.lil_mode = LL_LEASE_UNLCK;
11229 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11231 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11236 "%s %s: release lease lock of '%s' failed: %s\n",
11237 progname, argv[0], fname, strerror(errno));
11254 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11256 struct llapi_layout *layout;
11257 struct collect_ids_data cid = { .cid_ids = ids,
11259 .cid_exclude = exclude_id, };
11262 layout = llapi_layout_get_by_fd(fd, 0);
11264 fprintf(stderr, "could not get layout\n");
11268 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11270 fprintf(stderr, "failed to iterate layout\n");
11271 llapi_layout_free(layout);
11275 llapi_layout_free(layout);
11277 return cid.cid_count;
11280 #ifndef MIRROR_ID_NEG
11281 #define MIRROR_ID_NEG 0x8000
11284 static inline int lfs_mirror_copy(int argc, char **argv)
11287 __u16 read_mirror_id = 0;
11288 __u16 ids[128] = { 0 };
11290 struct llapi_layout *layout = NULL;
11291 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11298 struct ll_ioc_lease *ioc = NULL;
11299 struct ll_ioc_lease_id *resync_ioc;
11300 struct option long_opts[] = {
11301 { .val = 'h', .name = "help", .has_arg = no_argument },
11302 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
11303 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
11304 { .name = NULL } };
11305 char cmd[PATH_MAX];
11307 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11309 while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
11314 unsigned long int id;
11317 id = strtoul(optarg, &end, 0);
11318 if (errno != 0 || *end != '\0' || id == 0 ||
11321 "%s: invalid read mirror ID '%s'\n",
11326 read_mirror_id = (__u16)id;
11330 if (!strcmp(optarg, "-1")) {
11331 /* specify all other mirrors */
11332 ids[0] = (__u16)-1;
11335 count = parse_mirror_ids((__u16 *)ids,
11343 fprintf(stderr, "%s: unrecognized option '%s'\n",
11344 progname, argv[optind - 1]);
11351 if (argc == optind) {
11352 fprintf(stderr, "%s %s: no mirrored file provided\n",
11353 progname, argv[0]);
11355 } else if (argc > optind + 1) {
11356 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11360 if (read_mirror_id == 0) {
11362 "%s %s: no valid read mirror ID %d is provided\n",
11363 progname, argv[0], read_mirror_id);
11369 "%s %s: no write mirror ID is provided\n",
11370 progname, argv[0]);
11374 for (i = 0; i < count; i++) {
11375 if (read_mirror_id == ids[i]) {
11377 "%s %s: read and write mirror ID cannot be the same\n",
11378 progname, argv[0]);
11383 /* open mirror file */
11384 fname = argv[optind];
11386 fd = open(fname, O_DIRECT | O_RDWR);
11388 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11389 progname, argv[0], fname, strerror(errno));
11393 /* write to all other mirrors */
11394 if (ids[0] == (__u16)-1) {
11395 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11399 "%s %s: failed to get other mirror ids in '%s': %d\n",
11400 progname, argv[0], fname, rc);
11405 /* verify mirror id */
11406 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11409 "%s %s: cannot find mirror with ID %u in '%s'\n",
11410 progname, argv[0], read_mirror_id, fname);
11414 for (i = 0; i < count; i++) {
11415 rc = verify_mirror_id_by_fd(fd, ids[i]);
11418 "%s %s: cannot find mirror with ID %u in '%s'\n",
11419 progname, argv[0], ids[i], fname);
11424 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11427 "%s %s: cannot alloc comp id array for ioc: %s\n",
11428 progname, argv[0], strerror(errno));
11433 /* get stale component info */
11434 layout = llapi_layout_get_by_fd(fd, 0);
11436 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11437 progname, argv[0], fname, strerror(errno));
11441 comp_size = llapi_mirror_find_stale(layout, comp_array,
11442 ARRAY_SIZE(comp_array),
11444 llapi_layout_free(layout);
11445 if (comp_size < 0) {
11450 /* prepare target mirror components instantiation */
11451 resync_ioc = (struct ll_ioc_lease_id *)ioc;
11452 resync_ioc->lil_mode = LL_LEASE_WRLCK;
11453 resync_ioc->lil_flags = LL_LEASE_RESYNC;
11455 resync_ioc->lil_mirror_id = ids[0];
11457 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11458 rc = llapi_lease_set(fd, ioc);
11461 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11462 progname, argv[0], fname, strerror(errno));
11466 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11469 fprintf(stderr, "%s %s: copy error: %d\n",
11470 progname, argv[0], rc);
11474 fprintf(stdout, "mirror copied successfully: ");
11475 for (i = 0; i < copied; i++)
11476 fprintf(stdout, "%d ", ids[i]);
11477 fprintf(stdout, "\n");
11479 ioc->lil_mode = LL_LEASE_UNLCK;
11480 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11481 ioc->lil_count = 0;
11482 for (i = 0; i < comp_size; i++) {
11485 for (j = 0; j < copied; j++) {
11486 if (comp_array[i].lrc_mirror_id != ids[j])
11489 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11493 rc = llapi_lease_set(fd, ioc);
11498 "%s %s: release lease lock of '%s' failed: %s\n",
11499 progname, argv[0], fname, strerror(errno));
11514 * struct verify_chunk - Mirror chunk to be verified.
11515 * @chunk: [start, end) of the chunk.
11516 * @mirror_count: Number of mirror ids in @mirror_id array.
11517 * @mirror_id: Array of valid mirror ids that cover the chunk.
11519 struct verify_chunk {
11520 struct lu_extent chunk;
11521 unsigned int mirror_count;
11522 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11526 * print_chunks() - Print chunk information.
11527 * @fname: Mirrored file name.
11528 * @chunks: Array of chunks.
11529 * @chunk_count: Number of chunks in @chunks array.
11531 * This function prints [start, end) of each chunk in @chunks
11532 * for mirrored file @fname, and also prints the valid mirror ids
11533 * that cover the chunk.
11538 void print_chunks(const char *fname, struct verify_chunk *chunks,
11544 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11545 for (i = 0; i < chunk_count; i++) {
11546 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11548 if (chunks[i].mirror_count == 0)
11549 fprintf(stdout, "\t[");
11551 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11552 for (j = 1; j < chunks[i].mirror_count; j++)
11553 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11555 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11557 fprintf(stdout, "\n");
11561 * print_checksums() - Print CRC-32 checksum values.
11562 * @chunk: A chunk and its corresponding valid mirror ids.
11563 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
11565 * This function prints CRC-32 checksum values on @chunk for
11566 * each valid mirror that covers it.
11571 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
11576 "CRC-32 checksum value for chunk "DEXT":\n",
11577 PEXT(&chunk->chunk));
11578 for (i = 0; i < chunk->mirror_count; i++)
11579 fprintf(stdout, "Mirror %u:\t%#lx\n",
11580 chunk->mirror_id[i], crc[i]);
11581 fprintf(stdout, "\n");
11585 * filter_mirror_id() - Filter specified mirror ids.
11586 * @chunks: Array of chunks.
11587 * @chunk_count: Number of chunks in @chunks array.
11588 * @mirror_ids: Specified mirror ids to be verified.
11589 * @ids_nr: Number of specified mirror ids.
11591 * This function scans valid mirror ids that cover each chunk in @chunks
11592 * and filters specified mirror ids.
11597 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11598 __u16 *mirror_ids, int ids_nr)
11603 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11604 unsigned int valid_count = 0;
11606 for (i = 0; i < chunk_count; i++) {
11607 if (chunks[i].mirror_count == 0)
11611 for (j = 0; j < ids_nr; j++) {
11612 for (k = 0; k < chunks[i].mirror_count; k++) {
11613 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11614 valid_id[valid_count] = mirror_ids[j];
11621 memcpy(chunks[i].mirror_id, valid_id,
11622 sizeof(__u16) * valid_count);
11623 chunks[i].mirror_count = valid_count;
11628 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11629 * @layout: Mirror component list.
11630 * @chunks: Array of chunks.
11631 * @chunks_size: Array size of @chunks.
11633 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11634 * to find out chunk segments and store them in @chunks array.
11636 * The @mirror_id array in each element of @chunks will store the valid
11637 * mirror ids that cover the chunk. If a mirror component covering the
11638 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11639 * will not be stored into the @mirror_id array, and the chunk for that
11640 * mirror will not be verified.
11642 * The @mirror_count in each element of @chunks will store the number of
11643 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11644 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11645 * indicates the chunk is valid in only one mirror. In both cases, the
11646 * chunk will not be verified.
11648 * Here is an example:
11650 * 0 1M 2M 3M 4M EOF
11651 * +------+-------------+--------------------+
11652 * | | | S | mirror1
11653 * +------+------+------+------+-------------+
11654 * | | S | S | | mirror2
11655 * +-------------+------+------+-------------+
11657 * prepared @chunks array will contain 5 elements:
11658 * (([0, 1M), [1, 2], 2),
11659 * ([1M, 2M), [1, 2], 2),
11660 * ([2M, 3M), [1], 1),
11661 * ([3M, 4M], [], 0),
11662 * ([4M, EOF), [2], 1))
11664 * Return: the actual array size of @chunks on success
11665 * or a negative error code on failure.
11668 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11669 struct verify_chunk *chunks,
11670 size_t chunks_size)
11674 uint32_t mirror_id;
11680 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11683 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11686 "%s: move to the first layout component: %s.\n",
11687 progname, strerror(errno));
11693 chunks[idx].chunk.e_end = LUSTRE_EOF;
11695 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11698 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11699 progname, strerror(errno));
11703 if (start > chunks[idx].chunk.e_start ||
11704 end <= chunks[idx].chunk.e_start)
11707 if (end < chunks[idx].chunk.e_end)
11708 chunks[idx].chunk.e_end = end;
11710 rc = llapi_layout_comp_flags_get(layout, &flags);
11713 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11714 progname, strerror(errno));
11718 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11721 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11724 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11725 progname, strerror(errno));
11729 chunks[idx].mirror_id[i] = mirror_id;
11731 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11733 "%s: mirror_id array is too small.\n",
11740 rc = llapi_layout_comp_use(layout,
11741 LLAPI_LAYOUT_COMP_USE_NEXT);
11744 "%s: move to the next layout component: %s.\n",
11745 progname, strerror(errno));
11748 } /* loop through all components */
11750 chunks[idx].mirror_count = i;
11752 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11756 if (idx >= chunks_size) {
11757 fprintf(stderr, "%s: chunks array is too small.\n",
11763 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11767 return rc < 0 ? rc : idx + 1;
11771 * lfs_mirror_verify_chunk() - Verify a chunk.
11772 * @fd: File descriptor of the mirrored file.
11773 * @file_size: Size of the mirrored file.
11774 * @chunk: A chunk and its corresponding valid mirror ids.
11775 * @verbose: Verbose mode.
11777 * This function verifies a @chunk contains exactly the same data
11778 * ammong the mirrors that cover it.
11780 * If @verbose is specified, then the function will print where the
11781 * differences are if the data do not match. Otherwise, it will
11782 * just return an error in that case.
11784 * Return: 0 on success or a negative error code on failure.
11787 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11788 struct verify_chunk *chunk, int verbose)
11790 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11792 size_t page_size = sysconf(_SC_PAGESIZE);
11793 ssize_t bytes_read;
11794 ssize_t bytes_done;
11798 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11802 if (file_size == 0)
11805 rc = posix_memalign(&buf, page_size, buflen);
11806 if (rc) /* error code is returned directly */
11810 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11811 PEXT(&chunk->chunk));
11812 for (i = 0; i < chunk->mirror_count; i++)
11813 fprintf(stdout, " %u", chunk->mirror_id[i]);
11814 fprintf(stdout, "\n");
11818 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11819 pos = chunk->chunk.e_start;
11820 while (bytes_done < count) {
11821 /* compute initial CRC-32 checksum */
11822 crc = crc32(0L, Z_NULL, 0);
11823 memset(crc_array, 0, sizeof(crc_array));
11826 for (i = 0; i < chunk->mirror_count; i++) {
11827 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11829 if (bytes_read < 0) {
11832 "%s: failed to read data from mirror %u: %s.\n",
11833 progname, chunk->mirror_id[i],
11838 /* compute new CRC-32 checksum */
11839 crc_array[i] = crc32(crc, buf, bytes_read);
11843 print_checksums(chunk, crc_array);
11845 /* compare CRC-32 checksum values */
11846 for (i = 1; i < chunk->mirror_count; i++) {
11847 if (crc_array[i] != crc_array[0]) {
11851 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11852 progname, PEXT(&chunk->chunk),
11853 chunk->mirror_id[0],
11854 chunk->mirror_id[i]);
11859 bytes_done += bytes_read;
11862 if (verbose > 1 && rc == 0) {
11863 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11864 PEXT(&chunk->chunk));
11865 for (i = 0; i < chunk->mirror_count; i++)
11866 fprintf(stdout, " %u", chunk->mirror_id[i]);
11867 fprintf(stdout, " PASS\n\n");
11876 * lfs_mirror_verify_file() - Verify a mirrored file.
11877 * @fname: Mirrored file name.
11878 * @mirror_ids: Specified mirror ids to be verified.
11879 * @ids_nr: Number of specified mirror ids.
11880 * @verbose: Verbose mode.
11882 * This function verifies that each SYNC mirror of a mirrored file
11883 * specified by @fname contains exactly the same data.
11885 * If @mirror_ids is specified, then the function will verify the
11886 * mirrors specified by @mirror_ids contain exactly the same data.
11888 * If @verbose is specified, then the function will print where the
11889 * differences are if the data do not match. Otherwise, it will
11890 * just return an error in that case.
11892 * Return: 0 on success or a negative error code on failure.
11895 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11898 struct verify_chunk chunks_array[1024] = { };
11899 struct llapi_layout *layout = NULL;
11901 uint32_t flr_state;
11903 int chunk_count = 0;
11909 if (stat(fname, &stbuf) < 0) {
11910 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11911 progname, fname, strerror(errno));
11916 if (!S_ISREG(stbuf.st_mode)) {
11917 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11923 if (stbuf.st_size == 0) {
11925 fprintf(stdout, "%s: '%s' file size is 0.\n",
11931 /* Allow mirror verify even without the key on encrypted files */
11932 fd = open(fname, O_DIRECT | O_RDONLY | O_FILE_ENC);
11934 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11935 progname, fname, strerror(errno));
11940 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11942 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11943 progname, fname, strerror(errno));
11947 layout = llapi_layout_get_by_fd(fd, 0);
11949 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11950 progname, fname, strerror(errno));
11952 llapi_lease_release(fd);
11956 rc = llapi_layout_flags_get(layout, &flr_state);
11958 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11959 progname, fname, strerror(errno));
11964 flr_state &= LCM_FL_FLR_MASK;
11965 switch (flr_state) {
11968 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11969 progname, fname, llapi_layout_flags_string(flr_state));
11975 /* find out mirror chunks to be verified */
11976 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11977 ARRAY_SIZE(chunks_array));
11978 if (chunk_count < 0) {
11984 /* filter specified mirror ids */
11985 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11988 print_chunks(fname, chunks_array, chunk_count);
11990 for (idx = 0; idx < chunk_count; idx++) {
11991 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11994 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11996 PEXT(&chunks_array[idx].chunk),
11997 (unsigned long long)stbuf.st_size);
12001 if (chunks_array[idx].mirror_count == 0) {
12003 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
12005 PEXT(&chunks_array[idx].chunk));
12007 fprintf(stderr, "skipped\n");
12011 fprintf(stderr, "failed\n");
12015 if (chunks_array[idx].mirror_count == 1) {
12018 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12020 PEXT(&chunks_array[idx].chunk),
12021 chunks_array[idx].mirror_id[0]);
12025 rc = llapi_lease_check(fd);
12026 if (rc != LL_LEASE_RDLCK) {
12027 fprintf(stderr, "%s: '%s' lost lease lock.\n",
12032 /* verify one chunk */
12033 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12034 &chunks_array[idx], verbose);
12048 llapi_layout_free(layout);
12049 llapi_lease_release(fd);
12057 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12058 * @argc: The count of lfs mirror verify command line arguments.
12059 * @argv: Array of strings for lfs mirror verify command line arguments.
12061 * This function parses lfs mirror verify command and verifies the
12062 * specified mirrored file(s).
12064 * Return: 0 on success or a negative error code on failure.
12066 static inline int lfs_mirror_verify(int argc, char **argv)
12068 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12074 char cmd[PATH_MAX];
12076 struct option long_opts[] = {
12077 { .val = 'h', .name = "help", .has_arg = no_argument },
12078 { .val = 'o', .name = "only", .has_arg = required_argument },
12079 { .val = 'v', .name = "verbose", .has_arg = no_argument },
12080 { .name = NULL } };
12082 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12084 while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12087 rc = parse_mirror_ids(mirror_ids,
12088 ARRAY_SIZE(mirror_ids),
12092 "%s: bad mirror ids '%s'.\n",
12099 "%s: at least 2 mirror ids needed with '--only' option.\n",
12109 fprintf(stderr, "%s: unrecognized option '%s'\n",
12110 progname, argv[optind - 1]);
12118 if (argc == optind) {
12119 fprintf(stderr, "%s: no file name given.\n", progname);
12124 if (ids_nr > 0 && argc > optind + 1) {
12126 "%s: '--only' cannot be used upon multiple files.\n",
12133 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12139 for (; optind < argc; optind++) {
12140 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12150 * lfs_mirror() - Parse and execute lfs mirror commands.
12151 * @argc: The count of lfs mirror command line arguments.
12152 * @argv: Array of strings for lfs mirror command line arguments.
12154 * This function parses lfs mirror commands and performs the
12155 * corresponding functions specified in mirror_cmdlist[].
12157 * Return: 0 on success or an error code on failure.
12159 static int lfs_mirror(int argc, char **argv)
12161 char cmd[PATH_MAX];
12164 setlinebuf(stdout);
12166 Parser_init("lfs-mirror > ", mirror_cmdlist);
12168 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12170 program_invocation_short_name = cmd;
12172 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12174 rc = Parser_commands();
12176 return rc < 0 ? -rc : rc;
12179 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12181 #if __BYTE_ORDER == __BIG_ENDIAN
12182 __swab16s(&attrs->lsa_valid);
12183 __swab64s(&attrs->lsa_size);
12184 __swab64s(&attrs->lsa_blocks);
12188 enum lfs_som_type {
12189 LFS_SOM_SIZE = 0x1,
12190 LFS_SOM_BLOCKS = 0x2,
12191 LFS_SOM_FLAGS = 0x4,
12192 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12196 static int lfs_getsom(int argc, char **argv)
12199 struct lustre_som_attrs *attrs;
12200 char buf[sizeof(*attrs) + 64];
12201 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12204 while ((c = getopt(argc, argv, "bfhs")) != -1) {
12207 type = LFS_SOM_BLOCKS;
12210 type = LFS_SOM_FLAGS;
12213 type = LFS_SOM_SIZE;
12216 fprintf(stderr, "%s: unrecognized option '%s'\n",
12217 progname, argv[optind - 1]);
12228 fprintf(stderr, "%s: %s\n",
12229 progname, argc == 0 ? "miss file target" :
12230 "input more than 2 files");
12235 attrs = (void *)buf;
12236 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12239 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12240 argv[0], strerror(errno), errno);
12244 lustre_som_swab(attrs);
12247 case LFS_SOM_ATTR_ALL:
12248 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12249 path, (unsigned long long)attrs->lsa_size,
12250 (unsigned long long)attrs->lsa_blocks,
12254 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12256 case LFS_SOM_BLOCKS:
12257 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12259 case LFS_SOM_FLAGS:
12260 printf("%x\n", attrs->lsa_valid);
12263 fprintf(stderr, "%s: unknown option\n", progname);
12271 * lfs_mirror_list_commands() - List lfs mirror commands.
12272 * @argc: The count of command line arguments.
12273 * @argv: Array of strings for command line arguments.
12275 * This function lists lfs mirror commands defined in mirror_cmdlist[].
12277 * Return: 0 on success.
12279 static int lfs_mirror_list_commands(int argc, char **argv)
12281 char buffer[81] = "";
12283 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12289 static int lfs_pcc_attach(int argc, char **argv)
12291 struct option long_opts[] = {
12292 { .val = 'h', .name = "help", .has_arg = no_argument },
12293 { .val = 'i', .name = "id", .has_arg = required_argument },
12294 { .name = NULL } };
12297 __u32 archive_id = 0;
12300 char fullpath[PATH_MAX];
12301 enum lu_pcc_type type = LU_PCC_READWRITE;
12304 while ((c = getopt_long(argc, argv, "hi:",
12305 long_opts, NULL)) != -1) {
12309 archive_id = strtoul(optarg, &end, 0);
12310 if (errno != 0 || *end != '\0' ||
12311 archive_id == 0 || archive_id > UINT32_MAX) {
12313 "error: %s: bad archive ID '%s'\n",
12319 fprintf(stderr, "%s: unrecognized option '%s'\n",
12320 progname, argv[optind - 1]);
12327 if (archive_id == 0) {
12328 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12332 if (argc <= optind) {
12333 fprintf(stderr, "%s: must specify one or more file names\n",
12338 while (optind < argc) {
12341 path = argv[optind++];
12342 if (!realpath(path, fullpath)) {
12343 fprintf(stderr, "%s: could not find path '%s': %s\n",
12344 argv[0], path, strerror(errno));
12350 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12353 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12354 argv[0], path, archive_id, strerror(-rc2));
12362 static int lfs_pcc_attach_fid(int argc, char **argv)
12364 struct option long_opts[] = {
12365 { .val = 'h', .name = "help", .has_arg = no_argument },
12366 { .val = 'i', .name = "id", .has_arg = required_argument },
12367 { .val = 'm', .name = "mnt", .has_arg = required_argument },
12368 { .name = NULL } };
12371 __u32 archive_id = 0;
12373 const char *mntpath = NULL;
12374 const char *fidstr;
12375 enum lu_pcc_type type = LU_PCC_READWRITE;
12378 while ((c = getopt_long(argc, argv, "hi:m:",
12379 long_opts, NULL)) != -1) {
12383 archive_id = strtoul(optarg, &end, 0);
12384 if (errno != 0 || *end != '\0' ||
12385 archive_id > UINT32_MAX) {
12387 "error: %s: bad archive ID '%s'\n",
12396 fprintf(stderr, "%s: unrecognized option '%s'\n",
12397 progname, argv[optind - 1]);
12404 if (archive_id == 0) {
12405 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12410 fprintf(stderr, "%s: must specify Lustre mount point\n",
12415 if (argc <= optind) {
12416 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12420 while (optind < argc) {
12423 fidstr = argv[optind++];
12425 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12429 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12430 argv[0], fidstr, mntpath, archive_id,
12433 if (rc == 0 && rc2 < 0)
12439 static int lfs_pcc_detach(int argc, char **argv)
12441 struct option long_opts[] = {
12442 { .val = 'h', .name = "help", .has_arg = no_argument },
12443 { .val = 'k', .name = "keep", .has_arg = no_argument },
12444 { .name = NULL } };
12448 char fullpath[PATH_MAX];
12449 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12452 while ((c = getopt_long(argc, argv, "hk",
12453 long_opts, NULL)) != -1) {
12456 detach_opt = PCC_DETACH_OPT_NONE;
12459 fprintf(stderr, "%s: unrecognized option '%s'\n",
12460 progname, argv[optind - 1]);
12467 while (optind < argc) {
12470 path = argv[optind++];
12471 if (!realpath(path, fullpath)) {
12472 fprintf(stderr, "%s: could not find path '%s': %s\n",
12473 argv[0], path, strerror(errno));
12479 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12483 "%s: cannot detach '%s' from PCC: %s\n",
12484 argv[0], path, strerror(errno));
12492 static int lfs_pcc_detach_fid(int argc, char **argv)
12494 struct option long_opts[] = {
12495 { .val = 'h', .name = "help", .has_arg = no_argument },
12496 { .val = 'k', .name = "keep", .has_arg = no_argument },
12497 { .name = NULL } };
12501 const char *mntpath;
12502 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12505 while ((c = getopt_long(argc, argv, "hk",
12506 long_opts, NULL)) != -1) {
12509 detach_opt = PCC_DETACH_OPT_NONE;
12512 fprintf(stderr, "%s: unrecognized option '%s'\n",
12513 progname, argv[optind - 1]);
12520 mntpath = argv[optind++];
12522 while (optind < argc) {
12525 fid = argv[optind++];
12527 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12530 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12531 argv[0], fid, mntpath, strerror(-rc2));
12539 static int lfs_pcc_state(int argc, char **argv)
12543 char fullpath[PATH_MAX];
12544 struct lu_pcc_state state;
12549 fprintf(stderr, "%s: must specify one or more file names\n",
12554 while (optind < argc) {
12557 path = argv[optind++];
12558 if (!realpath(path, fullpath)) {
12559 fprintf(stderr, "%s: could not find path '%s': %s\n",
12560 argv[0], path, strerror(errno));
12566 rc2 = llapi_pcc_state_get(fullpath, &state);
12571 "%s: cannot get PCC state of '%s': %s\n",
12572 argv[0], path, strerror(-rc2));
12576 printf("file: %s", path);
12577 printf(", type: %s", pcc_type2string(state.pccs_type));
12578 if (state.pccs_type == LU_PCC_NONE &&
12579 state.pccs_open_count == 0) {
12584 printf(", PCC file: %s", state.pccs_path);
12585 printf(", user number: %u", state.pccs_open_count);
12586 printf(", flags: %x", state.pccs_flags);
12593 * lfs_pcc_list_commands() - List lfs pcc commands.
12594 * @argc: The count of command line arguments.
12595 * @argv: Array of strings for command line arguments.
12597 * This function lists lfs pcc commands defined in pcc_cmdlist[].
12599 * Return: 0 on success.
12601 static int lfs_pcc_list_commands(int argc, char **argv)
12603 char buffer[81] = "";
12605 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12612 * lfs_pcc() - Parse and execute lfs pcc commands.
12613 * @argc: The count of lfs pcc command line arguments.
12614 * @argv: Array of strings for lfs pcc command line arguments.
12616 * This function parses lfs pcc commands and performs the
12617 * corresponding functions specified in pcc_cmdlist[].
12619 * Return: 0 on success or an error code on failure.
12621 static int lfs_pcc(int argc, char **argv)
12623 char cmd[PATH_MAX];
12626 setlinebuf(stdout);
12628 Parser_init("lfs-pcc > ", pcc_cmdlist);
12630 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12632 program_invocation_short_name = cmd;
12634 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12636 rc = Parser_commands();
12638 return rc < 0 ? -rc : rc;
12641 static int lfs_list_commands(int argc, char **argv)
12643 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12645 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12650 int main(int argc, char **argv)
12654 /* Ensure that liblustreapi constructor has run */
12655 if (!llapi_liblustreapi_initialized())
12656 fprintf(stderr, "liblustreapi was not properly initialized\n");
12658 setlinebuf(stdout);
12661 Parser_init("lfs > ", cmdlist);
12663 progname = program_invocation_short_name; /* Used in error messages */
12665 llapi_set_command_name(argv[1]);
12666 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12667 llapi_clear_command_name();
12669 rc = Parser_commands();
12672 return rc < 0 ? -rc : rc;
12675 #ifdef _LUSTRE_IDL_H_
12676 /* Everything we need here should be included by lustreapi.h. */
12677 # error "lfs should not depend on lustre_idl.h"
12678 #endif /* _LUSTRE_IDL_H_ */