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] [--printf <format>]\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\n"
448 " setquota {-u|-g|-p} --delete FILESYSTEM\n"},
449 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
450 "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
451 " [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID]\n"
452 " [--pool <OST pool name>] <filesystem>\n"
453 " quota -t <-u|-g|-p> [--pool <OST pool name>] <filesystem>\n"
454 " quota [-q] [-v] [h] {-U|-G|-P} [--pool <OST pool name>] <filesystem>"},
455 {"project", lfs_project, 0,
456 "Change or list project attribute for specified file or directory.\n"
457 "usage: project [-d|-r] <file|directory...>\n"
458 " list project ID and flags on file(s) or directories\n"
459 " project [-p id] [-s] [-r] <file|directory...>\n"
460 " set project ID and/or inherit flag for specified file(s) or directories\n"
461 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
462 " check project ID and flags on file(s) or directories, print outliers\n"
463 " project -C [-r] [-k] <file|directory...>\n"
464 " clear the project inherit flag and ID on the file or directory\n"
467 {"flushctx", lfs_flushctx, 0,
468 "Flush security context for current user.\n"
469 "usage: flushctx [-k] [-r] [mountpoint...]"},
470 {"changelog", lfs_changelog, 0,
471 "Show the metadata changes on an MDT."
472 "\nusage: changelog <mdtname> [startrec [endrec]]"},
473 {"changelog_clear", lfs_changelog_clear, 0,
474 "Indicate that old changelog records up to <endrec> are no longer of "
475 "interest to consumer <id>, allowing the system to free up space.\n"
476 "An <endrec> of 0 means all records.\n"
477 "usage: changelog_clear <mdtname> <id> <endrec>"},
478 {"fid2path", lfs_fid2path, 0,
479 "Resolve the full path(s) for given FID(s). For a specific hardlink "
480 "specify link number <linkno>.\n"
481 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
482 "<fsname|root> <fid>..."},
483 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
484 "usage: path2fid [--parents] <path> ..."},
485 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
486 "usage: rmfid <fsname|rootpath> <fid> ..."},
487 {"data_version", lfs_data_version, 0, "Display file data version for "
488 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
489 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
490 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
491 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
492 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
493 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
494 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
496 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
497 "[--archived] [--lost] <file> ..."},
498 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
499 "given files.\n" "usage: hsm_action <file> ..."},
500 {"hsm_archive", lfs_hsm_archive, 0,
501 "Archive file to external storage.\n"
502 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
504 {"hsm_restore", lfs_hsm_restore, 0,
505 "Restore file from external storage.\n"
506 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
507 {"hsm_release", lfs_hsm_release, 0,
508 "Release files from Lustre.\n"
509 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
510 {"hsm_remove", lfs_hsm_remove, 0,
511 "Remove file copy from external storage.\n"
512 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
514 " (FILE [FILE ...] | "
515 "--mntpath MOUNTPATH FID [FID ...])\n"
517 "Note: To remove an archived copy of a file already deleted from a "
519 "--mntpath option and a list of FIDs must be specified"
521 {"hsm_cancel", lfs_hsm_cancel, 0,
522 "Cancel requests related to specified files.\n"
523 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
524 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
525 "usage: swap_layouts <path1> <path2>"},
526 {"migrate", lfs_setstripe_migrate, 0,
527 "migrate directories and their inodes between MDTs.\n"
528 "usage: migrate [--mdt-count|-c STRIPE_COUNT] [--directory|-d]\n"
529 " [--mdt-hash|-H HASH_TYPE]\n"
530 " [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
533 "migrate file objects from one OST layout to another\n"
534 "(may be not safe with concurent writes).\n"
537 "To move directories between MDTs. This command is deprecated, "
538 "use \"migrate\" instead.\n"
539 "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
541 {"ladvise", lfs_ladvise, 0,
542 "Provide servers with advice about access patterns for a file.\n"
543 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
544 " [--background|-b] [--unset|-u]\n\n"
545 " {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
546 " {[--mode|-m [READ,WRITE]}\n"
548 {"mirror", lfs_mirror, mirror_cmdlist,
549 "lfs commands used to manage files with mirrored components:\n"
550 "lfs mirror create - create a mirrored file or directory\n"
551 "lfs mirror extend - add mirror(s) to an existing file\n"
552 "lfs mirror split - split a mirror from an existing mirrored file\n"
553 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
554 "lfs mirror read - read a mirror content of a mirrored file\n"
555 "lfs mirror write - write to a mirror of a mirrored file\n"
556 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
557 "lfs mirror verify - verify mirrored file(s)\n"},
558 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
559 "usage: getsom [-s] [-b] [-f] <path>\n"
560 "\t-s: Only show the size value of the SOM data for a given file\n"
561 "\t-b: Only show the blocks value of the SOM data for a given file\n"
562 "\t-f: Only show the flags value of the SOM data for a given file\n"},
563 {"heat_get", lfs_heat_get, 0,
564 "To get heat of files.\n"
565 "usage: heat_get <file> ...\n"},
566 {"heat_set", lfs_heat_set, 0,
567 "To set heat flags of files.\n"
568 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
569 "\t--clear|-c: Clear file heat for given files\n"
570 "\t--off|-o: Turn off file heat for given files\n"
571 "\t--on|-O: Turn on file heat for given files\n"},
572 {"pcc", lfs_pcc, pcc_cmdlist,
573 "lfs commands used to interact with PCC features:\n"
574 "lfs pcc attach - attach given files to Persistent Client Cache\n"
575 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
576 "lfs pcc state - display the PCC state for given files\n"
577 "lfs pcc detach - detach given files from Persistent Client Cache\n"
578 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
579 {"help", Parser_help, 0, "help"},
580 {"exit", Parser_quit, 0, "quit"},
581 {"quit", Parser_quit, 0, "quit"},
582 {"--version", Parser_version, 0,
583 "output build version of the utility and exit"},
584 {"--list-commands", lfs_list_commands, 0,
585 "list commands supported by the utility and exit"},
589 static int check_hashtype(const char *hashtype)
591 int type_num = atoi(hashtype);
594 /* numeric hash type */
595 if (hashtype && strlen(hashtype) == 1 &&
596 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
598 /* string hash type */
599 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
600 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
606 static uint32_t check_foreign_type_name(const char *foreign_type_name)
610 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
611 if (!lu_foreign_types[i].lft_name)
613 if (strcmp(foreign_type_name,
614 lu_foreign_types[i].lft_name) == 0)
615 return lu_foreign_types[i].lft_type;
618 return LU_FOREIGN_TYPE_UNKNOWN;
621 static const char *error_loc = "syserror";
624 migrate_open_files(const char *name, __u64 migration_flags,
625 const struct llapi_stripe_param *param,
626 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
633 char parent[PATH_MAX];
634 char volatile_file[PATH_MAX];
640 if (!param && !layout) {
641 error_loc = "layout information";
645 /* search for file directory pathname */
646 if (strlen(name) > sizeof(parent) - 1) {
647 error_loc = "source file name";
651 strncpy(parent, name, sizeof(parent));
652 ptr = strrchr(parent, '/');
654 if (!getcwd(parent, sizeof(parent))) {
655 error_loc = "getcwd";
659 if (ptr == parent) /* leading '/' */
664 /* even if the file is only read, WR mode is nedeed to allow
667 /* Allow migrating even without the key on encrypted files */
668 rflags = O_RDWR | O_NOATIME | O_FILE_ENC;
669 if (!(migration_flags & LLAPI_MIGRATION_NONDIRECT))
672 fd = open(name, rflags);
674 /* If encrypted file without the key,
675 * retry mirror extend in O_DIRECT.
677 if (errno == ENOKEY && !(rflags & O_DIRECT) &&
678 migration_flags & LLAPI_MIGRATION_MIRROR) {
683 error_loc = "cannot open source file";
687 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
689 error_loc = "cannot get MDT index";
694 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW |
695 /* Allow migrating without the key on encrypted files */
697 mode_t open_mode = S_IRUSR | S_IWUSR;
699 if (rflags & O_DIRECT)
700 open_flags |= O_DIRECT;
701 random_value = random();
702 rc = snprintf(volatile_file, sizeof(volatile_file),
703 "%s/%s:%.4X:%.4X:fd=%.2d", parent,
704 LUSTRE_VOLATILE_HDR, mdt_index,
706 if (rc >= sizeof(volatile_file)) {
711 /* create, open a volatile file, use caching (ie no directio) */
713 /* Returns -1 and sets errno on error: */
714 fdv = llapi_layout_file_open(volatile_file, open_flags,
719 /* Does the right thing on error: */
720 fdv = llapi_file_open_param(volatile_file, open_flags,
723 } while (fdv < 0 && (rc = fdv) == -EEXIST);
726 error_loc = "cannot create volatile file";
731 * In case the MDT does not support creation of volatile files
732 * we should try to unlink it.
734 (void)unlink(volatile_file);
737 * Not-owner (root?) special case.
738 * Need to set owner/group of volatile file like original.
739 * This will allow to pass related check during layout_swap.
744 error_loc = "cannot stat source file";
748 rc = fstat(fdv, &stv);
751 error_loc = "cannot stat volatile";
755 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
756 rc = fchown(fdv, st.st_uid, st.st_gid);
759 error_loc = "cannot change ownwership of volatile";
778 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
780 struct llapi_layout *layout;
781 size_t buf_size = 4 * 1024 * 1024;
785 size_t page_size = sysconf(_SC_PAGESIZE);
789 layout = llapi_layout_get_by_fd(fd_src, 0);
791 uint64_t stripe_size;
793 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
795 buf_size = stripe_size;
797 llapi_layout_free(layout);
800 /* Use a page-aligned buffer for direct I/O */
801 rc = posix_memalign(&buf, page_size, buf_size);
805 sparse = llapi_file_is_sparse(fd_src);
807 rc = ftruncate(fd_dst, pos);
816 size_t to_read, to_write;
819 if (sparse && pos >= data_end) {
822 data_off = llapi_data_seek(fd_src, pos, &data_size);
824 /* Non-fatal, switch to full copy */
828 /* hole at the end of file, truncate up to it */
830 rc = ftruncate(fd_dst, data_off);
834 pos = data_off & ~(page_size - 1);
835 data_end = data_off + data_size;
836 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
837 to_read = MIN(to_read, buf_size);
843 rc = check_file(fd_src);
848 rsize = pread(fd_src, buf, to_read, pos);
858 while (to_write > 0) {
861 written = pwrite(fd_dst, buf, to_write, pos);
869 if (rc || rsize < to_read)
877 /* Try to avoid page cache pollution after migration. */
878 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
879 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
885 static int migrate_set_timestamps(int fd, const struct stat *st)
887 struct timeval tv[2] = {
888 {.tv_sec = st->st_atime},
889 {.tv_sec = st->st_mtime}
892 return futimes(fd, tv);
895 static int migrate_block(int fd, int fdv)
905 error_loc = "cannot stat source file";
909 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
911 error_loc = "cannot get dataversion";
920 * The grouplock blocks all concurrent accesses to the file.
921 * It has to be taken after llapi_get_data_version as it would
924 rc = llapi_group_lock(fd, gid);
926 error_loc = "cannot get group lock";
930 rc = migrate_copy_data(fd, fdv, NULL);
932 error_loc = "data copy failed";
936 /* Make sure we keep original atime/mtime values */
937 rc = migrate_set_timestamps(fdv, &st);
939 error_loc = "set target file timestamp failed";
945 * for a migration we need to check data version on file did
948 * Pass in gid=0 since we already own grouplock.
950 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
951 SWAP_LAYOUTS_CHECK_DV1);
953 error_loc = "file changed";
956 error_loc = "cannot swap layout";
961 rc2 = llapi_group_unlock(fd, gid);
962 if (rc2 < 0 && rc == 0) {
963 error_loc = "unlock group lock";
971 * Internal helper for migrate_copy_data(). Check lease and report error if
974 * \param[in] fd File descriptor on which to check the lease.
976 * \retval 0 Migration can keep on going.
977 * \retval -errno Error occurred, abort migration.
979 static int check_lease(int fd)
983 rc = llapi_lease_check(fd);
985 return 0; /* llapi_check_lease returns > 0 on success. */
990 static int migrate_nonblock(int fd, int fdv)
999 error_loc = "cannot stat source file";
1003 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1005 error_loc = "cannot get data version";
1009 rc = migrate_copy_data(fd, fdv, check_lease);
1011 error_loc = "data copy failed";
1015 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1017 error_loc = "cannot get data version";
1023 error_loc = "source file changed";
1027 /* Make sure we keep original atime/mtime values */
1028 rc = migrate_set_timestamps(fdv, &st);
1030 error_loc = "set target file timestamp failed";
1037 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1039 struct pool_to_id_cbdata data = { .pool = pool };
1040 struct llapi_layout *layout = NULL;
1043 layout = llapi_layout_get_by_path(fname, 0);
1046 "error %s: file '%s' couldn't get layout: rc=%d\n",
1047 progname, fname, errno);
1051 rc = llapi_layout_sanity(layout, false, true);
1053 llapi_layout_sanity_perror(errno);
1056 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1065 llapi_layout_free(layout);
1069 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1070 __u32 flags, __u32 neg_flags)
1073 __u32 flags_array[2];
1080 "error %s: neither component id nor pool is specified\n",
1084 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1090 ids[count] = comp_id;
1091 flags_array[count] = flags;
1096 if (neg_flags & LCME_FL_STALE) {
1098 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1103 ids[count] = comp_id;
1104 flags_array[count] = neg_flags | LCME_FL_NEG;
1108 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1110 if (errno == EUCLEAN) {
1113 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1114 progname, comp_id, fname);
1117 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1118 progname, comp_id, fname, flags, neg_flags);
1125 static int lfs_component_del(char *fname, __u32 comp_id,
1126 __u32 flags, __u32 neg_flags)
1130 if (flags && neg_flags) {
1132 "%s: cannot specify both positive and negative flags\n",
1137 if (!flags && neg_flags)
1138 flags = neg_flags | LCME_FL_NEG;
1140 if (flags && comp_id) {
1142 "%s: cannot specify component ID and flags at the same time\n",
1147 if (!flags && !comp_id) {
1149 "%s: neither flags nor component ID is specified\n",
1155 if (flags & ~LCME_KNOWN_FLAGS) {
1157 "%s setstripe: unknown flags %#x\n",
1161 } else if (comp_id > LCME_ID_MAX) {
1162 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1167 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1170 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1171 progname, comp_id, fname, strerror(errno));
1175 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1182 rc = llapi_layout_file_comp_add(fname, layout);
1184 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1185 fname, strerror(errno));
1189 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1190 struct llapi_layout *layout)
1198 fd = lstat(fname, &st);
1199 if (fd == 0 && S_ISDIR(st.st_mode))
1200 open_flags = O_DIRECTORY | O_RDONLY;
1202 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1204 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1205 S_ISDIR(st.st_mode) ?
1206 "set default composite layout for" :
1207 "create composite file",
1208 fname, strerror(errno));
1212 static int lfs_migrate(char *name, __u64 migration_flags,
1213 struct llapi_stripe_param *param,
1214 struct llapi_layout *layout)
1216 struct llapi_layout *existing;
1217 uint64_t dom_new, dom_cur;
1222 rc = migrate_open_files(name, migration_flags, param, layout,
1227 rc = llapi_layout_dom_size(layout, &dom_new);
1229 error_loc = "cannot get new layout DoM size";
1232 /* special case for migration to DOM layout*/
1233 existing = llapi_layout_get_by_fd(fd, 0);
1235 error_loc = "cannot get existing layout";
1239 rc = llapi_layout_dom_size(existing, &dom_cur);
1241 error_loc = "cannot get current layout DoM size";
1246 * if file has DoM layout already then migration is possible to
1247 * the new layout with the same DoM component via swap layout,
1248 * if new layout used bigger DOM size, then mirroring is used
1250 if (dom_new > dom_cur) {
1251 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags);
1253 error_loc = "cannot migrate to DOM layout";
1257 if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1259 * Blocking mode (forced if servers do not support file lease).
1260 * It is also the default mode, since we cannot distinguish
1261 * between a broken lease and a server that does not support
1262 * atomic swap/close (LU-6785)
1264 rc = migrate_block(fd, fdv);
1268 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1270 error_loc = "cannot get lease";
1274 rc = migrate_nonblock(fd, fdv);
1276 llapi_lease_release(fd);
1281 * Atomically put lease, swap layouts and close.
1282 * for a migration we need to check data version on file did
1285 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1287 error_loc = "cannot swap layout";
1299 fprintf(stderr, "error: %s: %s: %s: %s\n",
1300 progname, name, error_loc, strerror(-rc));
1301 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1302 printf("%s\n", name);
1307 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1310 char *dup_string = NULL;
1316 if (!string || !string[0])
1319 dup_string = strdup(string);
1321 llapi_printf(LLAPI_MSG_ERROR,
1322 "%s: insufficient memory\n",
1327 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1331 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1332 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1333 const char *comp_name = comp_flags_table[i].cfn_name;
1335 if (strcmp(name, comp_name) == 0) {
1336 *flags |= comp_flag;
1338 } else if (strncmp(name, "^", 1) == 0 &&
1339 strcmp(name + 1, comp_name) == 0) {
1340 *neg_flags |= comp_flag;
1345 llapi_printf(LLAPI_MSG_ERROR,
1346 "%s: component flag '%s' not supported\n",
1353 if (!*flags && !*neg_flags)
1356 /* don't allow to set and exclude the same flag */
1357 if (*flags & *neg_flags)
1365 static int mdthash_input(char *string, __u32 *inflags,
1366 __u32 *exflags, __u32 *type)
1373 {"migrating", LMV_HASH_FLAG_MIGRATION},
1374 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1375 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1384 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1388 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1389 if (strcmp(name, mhflist[i].name) == 0 ||
1390 name[0] == mhflist[i].name[0]) {
1391 *inflags |= mhflist[i].flag;
1393 } else if (name[0] == '^' &&
1394 (strcmp(name + 1, mhflist[i].name) == 0 ||
1395 name[1] == mhflist[i].name[0])) {
1396 *exflags |= mhflist[i].flag;
1401 i = check_hashtype(name);
1406 llapi_printf(LLAPI_MSG_ERROR,
1407 "%s: invalid mdt_hash value '%s'\n",
1413 /* don't allow to include and exclude the same flag */
1414 if (*inflags & *exflags) {
1415 llapi_printf(LLAPI_MSG_ERROR,
1416 "%s: include and exclude same flag '%s'\n",
1424 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1432 if (strncmp(string, "^", 1) == 0) {
1433 *neg_state = llapi_layout_string_flags(string + 1);
1434 if (*neg_state != 0)
1437 *state = llapi_layout_string_flags(string);
1442 llapi_printf(LLAPI_MSG_ERROR,
1443 "%s: mirrored file state '%s' not supported\n",
1449 * struct mirror_args - Command-line arguments for mirror(s).
1450 * @m_count: Number of mirrors to be created with this layout.
1451 * @m_flags: Mirror level flags, only 'prefer' is supported.
1452 * @m_layout: Mirror layout.
1453 * @m_file: A victim file. Its layout will be split and used as a mirror.
1454 * @m_next: Point to the next node of the list.
1456 * Command-line arguments for mirror(s) will be parsed and stored in
1457 * a linked list that consists of this structure.
1459 struct mirror_args {
1462 struct llapi_layout *m_layout;
1464 struct mirror_args *m_next;
1469 * enum mirror_flags - Flags for extending a mirrored file.
1470 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1471 * in case the victim file(s) contains the same data as the
1472 * original mirrored file.
1473 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1474 * @MF_COMP_ID: specified component id instead of mirror id
1476 * Flags for extending a mirrored file.
1486 * mirror_create_sanity_check() - Check mirror list.
1487 * @list: A linked list that stores the mirror arguments.
1489 * This function does a sanity check on @list for creating
1492 * Return: 0 on success or a negative error code on failure.
1494 static int mirror_create_sanity_check(const char *fname,
1495 struct mirror_args *list,
1499 bool has_m_file = false;
1500 bool has_m_layout = false;
1505 if (fname && check_fname) {
1506 struct llapi_layout *layout;
1508 layout = llapi_layout_get_by_path(fname, 0);
1511 "error: %s: file '%s' couldn't get layout\n",
1516 rc = llapi_layout_sanity(layout, false, true);
1518 llapi_layout_free(layout);
1521 llapi_layout_sanity_perror(rc);
1529 llapi_layout_free(list->m_layout);
1532 llapi_layout_get_by_path(list->m_file, 0);
1533 if (!list->m_layout) {
1535 "error: %s: file '%s' has no layout\n",
1536 progname, list->m_file);
1540 has_m_layout = true;
1541 if (!list->m_layout) {
1542 fprintf(stderr, "error: %s: no mirror layout\n",
1548 rc = llapi_layout_sanity(list->m_layout, false, true);
1550 llapi_layout_sanity_perror(rc);
1554 list = list->m_next;
1557 if (has_m_file && has_m_layout) {
1559 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1567 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1569 __u32 mirror_flags = *(__u32 *)cbdata;
1573 rc = llapi_layout_comp_flags_get(layout, &flags);
1578 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1583 return LLAPI_LAYOUT_ITER_CONT;
1587 * mirror_create() - Create a mirrored file.
1588 * @fname: The file to be created.
1589 * @mirror_list: A linked list that stores the mirror arguments.
1591 * This function creates a mirrored file @fname with the mirror(s)
1592 * from @mirror_list.
1594 * Return: 0 on success or a negative error code on failure.
1596 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1598 struct llapi_layout *layout = NULL;
1599 struct mirror_args *cur_mirror = NULL;
1600 uint16_t mirror_count = 0;
1604 rc = mirror_create_sanity_check(fname, mirror_list, false);
1608 cur_mirror = mirror_list;
1609 while (cur_mirror) {
1610 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1612 &cur_mirror->m_flags);
1615 fprintf(stderr, "%s: failed to set mirror flags\n",
1620 for (i = 0; i < cur_mirror->m_count; i++) {
1621 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1625 "error: %s: merge layout failed: %s\n",
1626 progname, strerror(errno));
1630 mirror_count += cur_mirror->m_count;
1631 cur_mirror = cur_mirror->m_next;
1635 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1639 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1642 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1643 progname, strerror(errno));
1647 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1655 llapi_layout_free(layout);
1660 * Compare files and check lease on @fd.
1662 * \retval bytes number of bytes are the same
1664 static ssize_t mirror_file_compare(int fd, int fdv)
1666 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1668 ssize_t bytes_done = 0;
1669 ssize_t bytes_read = 0;
1671 buf = malloc(buflen * 2);
1676 if (!llapi_lease_check(fd)) {
1677 bytes_done = -EBUSY;
1681 bytes_read = read(fd, buf, buflen);
1682 if (bytes_read <= 0)
1685 if (bytes_read != read(fdv, buf + buflen, buflen))
1689 * XXX: should compute the checksum on each buffer and then
1690 * compare checksum to avoid cache collision
1692 if (memcmp(buf, buf + buflen, bytes_read))
1695 bytes_done += bytes_read;
1703 static int mirror_extend_file(const char *fname, const char *victim_file,
1704 enum mirror_flags mirror_flags)
1709 struct stat stbuf_v;
1710 struct ll_ioc_lease *data = NULL;
1713 fd = open(fname, O_RDWR);
1715 error_loc = "open source file";
1720 fdv = open(victim_file, O_RDWR);
1722 error_loc = "open target file";
1727 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1728 error_loc = "stat source or target file";
1733 if (stbuf.st_dev != stbuf_v.st_dev) {
1734 error_loc = "stat source and target file";
1739 /* mirrors should be of the same size */
1740 if (stbuf.st_size != stbuf_v.st_size) {
1741 error_loc = "file sizes don't match";
1746 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1748 error_loc = "cannot get lease";
1752 if (!(mirror_flags & MF_NO_VERIFY)) {
1754 /* mirrors should have the same contents */
1755 ret = mirror_file_compare(fd, fdv);
1756 if (ret != stbuf.st_size) {
1757 error_loc = "file busy or contents don't match";
1758 rc = ret < 0 ? ret : -EINVAL;
1763 /* Get rid of caching pages from clients */
1764 rc = llapi_file_flush(fd);
1766 error_loc = "cannot get data version";
1770 rc = llapi_file_flush(fdv);
1772 error_loc = "cannot get data version";
1776 rc = migrate_set_timestamps(fd, &stbuf);
1778 error_loc = "cannot set source file timestamp";
1782 /* Atomically put lease, merge layouts and close. */
1783 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1785 error_loc = "memory allocation";
1788 data->lil_mode = LL_LEASE_UNLCK;
1789 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1790 data->lil_count = 1;
1791 data->lil_ids[0] = fdv;
1792 rc = llapi_lease_set(fd, data);
1794 error_loc = "cannot merge layout";
1796 } else if (rc == 0) {
1798 error_loc = "lost lease lock";
1811 (void) unlink(victim_file);
1813 fprintf(stderr, "error: %s: %s: %s: %s\n",
1814 progname, fname, error_loc, strerror(-rc));
1818 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1819 bool inherit, uint32_t flags)
1821 struct llapi_layout *f_layout = NULL;
1822 struct ll_ioc_lease *data = NULL;
1829 f_layout = llapi_layout_get_by_path(name, 0);
1832 fprintf(stderr, "%s: cannot get layout\n", progname);
1835 rc = llapi_layout_get_last_init_comp(f_layout);
1837 fprintf(stderr, "%s: cannot get the last init comp\n",
1841 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1844 "%s: cannot inherit from the last init comp\n",
1850 llapi_layout_comp_flags_set(m_layout, flags);
1851 rc = migrate_open_files(name,
1852 LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
1853 NULL, m_layout, &fd, &fdv);
1857 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1859 error_loc = "cannot get lease";
1863 rc = fstat(fd, &st);
1865 error_loc = "cannot stat source file";
1869 rc = migrate_nonblock(fd, fdv);
1871 llapi_lease_release(fd);
1875 rc = migrate_set_timestamps(fd, &st);
1877 error_loc = "cannot set source file timestamp";
1881 /* Atomically put lease, merge layouts and close. */
1882 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1884 error_loc = "memory allocation";
1887 data->lil_mode = LL_LEASE_UNLCK;
1888 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1889 data->lil_count = 1;
1890 data->lil_ids[0] = fdv;
1891 rc = llapi_lease_set(fd, data);
1893 error_loc = "cannot merge layout";
1895 } else if (rc == 0) {
1897 error_loc = "lost lease lock";
1910 fprintf(stderr, "error: %s: %s: %s: %s\n",
1911 progname, name, error_loc, strerror(-rc));
1915 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1916 enum mirror_flags mirror_flags)
1920 while (mirror_list) {
1921 if (mirror_list->m_file) {
1922 rc = mirror_extend_file(fname, mirror_list->m_file,
1925 __u32 mirror_count = mirror_list->m_count;
1927 while (mirror_count > 0) {
1928 rc = mirror_extend_layout(fname,
1929 mirror_list->m_layout,
1930 mirror_list->m_inherit,
1931 mirror_list->m_flags);
1941 mirror_list = mirror_list->m_next;
1947 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1952 rc = llapi_layout_mirror_id_get(layout, &id);
1956 if ((__u16)id == *(__u16 *)cbdata)
1957 return LLAPI_LAYOUT_ITER_STOP;
1959 return LLAPI_LAYOUT_ITER_CONT;
1962 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1967 rc = llapi_layout_comp_id_get(layout, &id);
1971 if (id == *(__u32 *)cbdata)
1972 return LLAPI_LAYOUT_ITER_STOP;
1974 return LLAPI_LAYOUT_ITER_CONT;
1977 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
1979 char buf[LOV_MAXPOOLNAME + 1];
1980 struct pool_to_id_cbdata *d = (void *)cbdata;
1984 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
1987 if (strcmp(d->pool, buf))
1988 return LLAPI_LAYOUT_ITER_CONT;
1990 rc = llapi_layout_mirror_id_get(layout, &id);
1995 return LLAPI_LAYOUT_ITER_STOP;
1998 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2000 char buf[LOV_MAXPOOLNAME + 1];
2001 struct pool_to_id_cbdata *d = (void *)cbdata;
2005 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2008 if (strcmp(d->pool, buf))
2009 return LLAPI_LAYOUT_ITER_CONT;
2011 rc = llapi_layout_comp_id_get(layout, &id);
2016 return LLAPI_LAYOUT_ITER_STOP;
2019 struct collect_ids_data {
2025 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2027 struct collect_ids_data *cid = cbdata;
2031 rc = llapi_layout_mirror_id_get(layout, &id);
2035 if ((__u16)id != cid->cid_exclude) {
2038 for (i = 0; i < cid->cid_count; i++) {
2039 /* already collected the mirror id */
2040 if (id == cid->cid_ids[i])
2041 return LLAPI_LAYOUT_ITER_CONT;
2043 cid->cid_ids[cid->cid_count] = id;
2047 return LLAPI_LAYOUT_ITER_CONT;
2051 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2052 * @mirror_id: Mirror id to be checked.
2053 * @layout: Mirror component list.
2055 * This function checks if a mirror with specified @mirror_id is the last
2056 * non-stale mirror of a layout @layout.
2058 * Return: true or false.
2061 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2063 __u16 mirror_ids[128] = { 0 };
2064 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2066 .cid_exclude = mirror_id, };
2069 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2071 for (i = 0; i < cid.cid_count; i++) {
2072 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2075 comp_size = llapi_mirror_find_stale(layout, comp_array,
2076 ARRAY_SIZE(comp_array),
2085 static int mirror_split(const char *fname, __u32 id, const char *pool,
2086 enum mirror_flags mflags, const char *victim_file)
2088 struct llapi_layout *layout;
2089 char parent[PATH_MAX];
2090 char victim[PATH_MAX];
2091 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2093 struct ll_ioc_lease *data;
2094 uint16_t mirror_count;
2098 bool purge = true; /* delete mirror by setting fdv=fd */
2102 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2104 "error %s: the source file '%s' and -f file are the same\n",
2109 /* check fname contains mirror with mirror_id/comp_id */
2110 layout = llapi_layout_get_by_path(fname, 0);
2113 "error %s: file '%s' couldn't get layout\n",
2118 rc = llapi_layout_sanity(layout, false, true);
2120 llapi_layout_sanity_perror(rc);
2124 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2127 "error %s: file '%s' couldn't get mirror count\n",
2131 if (mirror_count < 2) {
2133 "error %s: file '%s' has %d component, cannot split\n",
2134 progname, fname, mirror_count);
2138 if (mflags & MF_COMP_POOL) {
2139 struct pool_to_id_cbdata data = { .pool = pool };
2141 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2143 mirror_id = data.id;
2144 } else if (mflags & MF_COMP_ID) {
2145 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2146 mirror_id = mirror_id_of(id);
2148 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2152 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2155 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2156 if (mflags & MF_COMP_POOL) {
2158 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2159 progname, fname, pool);
2161 } else if (mflags & MF_COMP_ID) {
2163 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2164 progname, fname, id);
2168 "error %s: file '%s' does not contain mirror with id %u\n",
2169 progname, fname, id);
2174 if (!victim_file && mflags & MF_DESTROY)
2175 /* Allow mirror split even without the key on encrypted files,
2176 * and in this case of a 'split -d', open file with O_DIRECT
2177 * (no IOs will be done).
2179 fd = open(fname, O_RDWR | O_DIRECT | O_FILE_ENC);
2181 fd = open(fname, O_RDWR);
2185 "error %s: open file '%s' failed: %s\n",
2186 progname, fname, strerror(errno));
2190 /* get victim file directory pathname */
2191 if (strlen(fname) > sizeof(parent) - 1) {
2192 fprintf(stderr, "error %s: file name of '%s' too long\n",
2197 strncpy(parent, fname, sizeof(parent));
2198 ptr = strrchr(parent, '/');
2200 if (!getcwd(parent, sizeof(parent))) {
2201 fprintf(stderr, "error %s: getcwd failed: %s\n",
2202 progname, strerror(errno));
2212 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2214 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2219 rc = llapi_file_is_encrypted(fd);
2221 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2222 progname, fname, rc);
2229 /* use a temp file to store the splitted layout */
2230 if (mflags & MF_DESTROY) {
2231 char file_path[PATH_MAX];
2232 unsigned int rnumber;
2235 if (last_non_stale_mirror(mirror_id, layout)) {
2238 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2244 /* don't use volatile file for mirror destroy */
2248 * try the old way to delete mirror using
2253 rc = snprintf(file_path,
2255 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2259 rc >= sizeof(file_path)) {
2260 fdv = -ENAMETOOLONG;
2264 open_flags = O_RDWR |
2265 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2266 O_CREAT | O_EXCL | O_NOFOLLOW |
2267 /* O_DIRECT for mirror split -d */
2269 /* Allow split without the key */
2271 fdv = open(file_path, open_flags,
2275 } while (fdv < 0 && rc == -EEXIST);
2281 "error %s: not permitted on encrypted file '%s': %d\n",
2282 progname, fname, rc);
2286 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2288 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2291 /* user specified victim file */
2295 "error %s: not permitted on encrypted file '%s': %d\n",
2296 progname, fname, rc);
2299 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2304 "error %s: create victim file failed: %s\n",
2305 progname, strerror(errno));
2309 /* get lease lock of fname */
2310 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2313 "error %s: cannot get lease of file '%s': %d\n",
2314 progname, fname, rc);
2318 /* Atomatically put lease, split layouts and close. */
2319 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2325 data->lil_mode = LL_LEASE_UNLCK;
2326 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2327 data->lil_count = 2;
2328 data->lil_ids[0] = fdv;
2329 data->lil_ids[1] = mirror_id;
2330 rc = llapi_lease_set(fd, data);
2332 if ((rc == -EINVAL || rc == -EBUSY) && purge) {
2333 /* could be old MDS which prohibit fd==fdv */
2338 if (rc == 0) /* lost lease lock */
2341 "error %s: cannot split '%s': %s\n",
2342 progname, fname, strerror(-rc));
2354 llapi_layout_free(layout);
2359 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2360 __u16 *mirror_ids, int ids_nr);
2362 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2363 __u64 migration_flags)
2365 struct ll_ioc_lease *data = NULL;
2368 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2370 error_loc = "cannot get lease";
2374 rc = migrate_nonblock(fd, fdv);
2378 /* Atomically put lease, merge layouts, resync and close. */
2379 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2381 error_loc = "memory allocation";
2384 data->lil_mode = LL_LEASE_UNLCK;
2385 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2386 data->lil_count = 1;
2387 data->lil_ids[0] = fdv;
2388 rc = llapi_lease_set(fd, data);
2390 error_loc = "cannot merge layout";
2392 } else if (rc == 0) {
2394 error_loc = "lost lease lock";
2400 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2402 error_loc = "cannot resync file";
2406 /* delete first mirror now */
2407 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2409 error_loc = "cannot delete old layout";
2413 llapi_lease_release(fd);
2419 fprintf(stderr, "error: %s: %s: %s: %s\n",
2420 progname, name, error_loc, strerror(-rc));
2421 else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2422 printf("%s\n", name);
2429 * Parse a string containing an target index list into an array of integers.
2431 * The input string contains a comma delimited list of individual
2432 * indices and ranges, for example "1,2-4,7". Add the indices into the
2433 * \a tgts array and remove duplicates.
2435 * \param[out] tgts array to store indices in
2436 * \param[in] size size of \a tgts array
2437 * \param[in] offset starting index in \a tgts
2438 * \param[in] arg string containing OST index list
2439 * \param[in/out] overstriping index list may contain duplicates
2441 * \retval positive number of indices in \a tgts
2442 * \retval -EINVAL unable to parse \a arg
2444 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2445 unsigned long long *pattern)
2449 int slots = size - offset;
2451 bool overstriped = false;
2457 end_of_loop = false;
2458 while (!end_of_loop) {
2459 int start_index = 0;
2462 char *endptr = NULL;
2466 ptr = strchrnul(arg, ',');
2468 end_of_loop = *ptr == '\0';
2472 start_index = strtol(arg, &endptr, 0);
2473 if (endptr == arg) /* no data at all */
2475 if (errno != 0 || start_index < -1 ||
2476 (*endptr != '-' && *endptr != '\0'))
2479 end_index = start_index;
2480 if (*endptr == '-') {
2482 end_index = strtol(endptr + 1, &endptr, 0);
2483 if (errno != 0 || *endptr != '\0' || end_index < -1)
2485 if (end_index < start_index)
2489 for (i = start_index; i <= end_index && slots > 0; i++) {
2492 /* remove duplicate */
2493 for (j = 0; j < offset; j++) {
2494 if (tgts[j] == i && pattern &&
2495 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2497 else if (tgts[j] == i)
2503 if (j == offset) { /* check complete */
2509 if (slots == 0 && i < end_index)
2517 if (!end_of_loop && ptr)
2520 if (!overstriped && pattern)
2521 *pattern = LLAPI_LAYOUT_DEFAULT;
2523 return rc < 0 ? rc : nr;
2526 struct lfs_setstripe_args {
2527 unsigned long long lsa_comp_end;
2528 unsigned long long lsa_stripe_size;
2529 unsigned long long lsa_extension_size;
2530 long long lsa_stripe_count;
2531 long long lsa_stripe_off;
2532 __u32 lsa_comp_flags;
2533 __u32 lsa_comp_neg_flags;
2534 unsigned long long lsa_pattern;
2535 unsigned int lsa_mirror_count;
2537 bool lsa_first_comp;
2538 bool lsa_extension_comp;
2540 char *lsa_pool_name;
2543 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2545 unsigned int mirror_count = lsa->lsa_mirror_count;
2546 bool first_comp = lsa->lsa_first_comp;
2548 memset(lsa, 0, sizeof(*lsa));
2550 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2551 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2552 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2553 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2554 lsa->lsa_pool_name = NULL;
2556 lsa->lsa_mirror_count = mirror_count;
2557 lsa->lsa_first_comp = first_comp;
2561 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2562 * @lsa: Stripe options to be initialized and inherited.
2564 * This function initializes stripe options in @lsa and inherit
2565 * stripe_size, stripe_count and OST pool_name options.
2569 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2571 unsigned long long stripe_size;
2572 long long stripe_count;
2573 char *pool_name = NULL;
2575 stripe_size = lsa->lsa_stripe_size;
2576 stripe_count = lsa->lsa_stripe_count;
2577 pool_name = lsa->lsa_pool_name;
2579 setstripe_args_init(lsa);
2581 lsa->lsa_stripe_size = stripe_size;
2582 lsa->lsa_stripe_count = stripe_count;
2583 lsa->lsa_pool_name = pool_name;
2586 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2588 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2589 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2590 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2591 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2592 lsa->lsa_comp_end != 0);
2595 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2597 if (lsa->lsa_nr_tgts) {
2598 if (lsa->lsa_nr_tgts < 0 ||
2599 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2600 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2606 if (lsa->lsa_stripe_count > 0 &&
2607 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2608 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2609 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2610 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2611 lsa->lsa_stripe_count,
2623 * comp_args_to_layout() - Create or extend a composite layout.
2624 * @composite: Pointer to the composite layout.
2625 * @lsa: Stripe options for the new component.
2627 * This function creates or extends a composite layout by adding a new
2628 * component with stripe options from @lsa.
2630 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2632 * Return: 0 on success or an error code on failure.
2634 static int comp_args_to_layout(struct llapi_layout **composite,
2635 struct lfs_setstripe_args *lsa,
2638 struct llapi_layout *layout = *composite;
2639 uint64_t prev_end = 0;
2645 layout = llapi_layout_alloc();
2647 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2652 *composite = layout;
2653 lsa->lsa_first_comp = true;
2658 * Get current component extent, current component
2659 * must be the tail component.
2661 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2663 fprintf(stderr, "Get comp extent failed. %s\n",
2668 if (lsa->lsa_first_comp) {
2670 rc = llapi_layout_add_first_comp(layout);
2672 rc = llapi_layout_comp_add(layout);
2675 fprintf(stderr, "Add component failed. %s\n",
2681 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2683 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2684 lsa->lsa_comp_flags, strerror(errno));
2689 uint64_t comp_end = lsa->lsa_comp_end;
2692 * The extendable component is 0-length, so it can be removed
2693 * if there is insufficient space to extend it.
2695 if (lsa->lsa_extension_comp)
2696 comp_end = prev_end;
2698 rc = llapi_layout_comp_extent_set(layout, prev_end,
2701 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2702 prev_end, comp_end, strerror(errno));
2706 /* reset lsa_first_comp */
2707 lsa->lsa_first_comp = false;
2709 /* Data-on-MDT component setting */
2710 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2712 if (lsa->lsa_stripe_count == 0)
2713 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2714 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2715 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2716 if (lsa->lsa_stripe_off == -1 ||
2717 lsa->lsa_stripe_off == 0)
2718 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2720 * In case of Data-on-MDT patterns the only extra option
2721 * applicable is stripe size option.
2723 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2725 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2726 lsa->lsa_stripe_count);
2730 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2731 lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2733 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2734 lsa->lsa_stripe_size);
2738 if (lsa->lsa_nr_tgts != 0) {
2740 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2745 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2747 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2748 lsa->lsa_stripe_off);
2752 if (lsa->lsa_pool_name != 0) {
2754 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2755 lsa->lsa_pool_name);
2760 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2762 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2767 /* Data-on-MDT component has always single stripe up to end */
2768 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2769 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2770 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2772 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2779 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2780 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2782 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2783 rc = llapi_layout_extension_size_set(layout, size);
2785 rc = llapi_layout_stripe_size_set(layout, size);
2788 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2789 size, strerror(errno));
2793 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2795 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2796 lsa->lsa_stripe_count, strerror(errno));
2800 if (lsa->lsa_pool_name) {
2801 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2803 fprintf(stderr, "Set pool name: %s failed. %s\n",
2804 lsa->lsa_pool_name, strerror(errno));
2808 rc = llapi_layout_pool_name_set(layout, "");
2810 fprintf(stderr, "Clear pool name failed: %s\n",
2816 rc = lsa_args_stripe_count_check(lsa);
2820 if (lsa->lsa_nr_tgts > 0) {
2823 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2824 rc = llapi_layout_ost_index_set(layout, i,
2829 /* Make sure stripe offset is in OST list. */
2830 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2834 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2835 lsa->lsa_stripe_off);
2839 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2840 lsa->lsa_stripe_off != -1) {
2841 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2844 fprintf(stderr, "Set ost index %d failed. %s\n",
2845 i, strerror(errno));
2849 /* Create the second, virtual component of extension space */
2850 if (lsa->lsa_extension_comp) {
2851 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2852 lsa->lsa_extension_comp = false;
2859 static int build_component(struct llapi_layout **layout,
2860 struct lfs_setstripe_args *lsa, bool set_extent)
2864 rc = comp_args_to_layout(layout, lsa, set_extent);
2868 if (lsa->lsa_mirror_count > 0) {
2869 rc = llapi_layout_mirror_count_set(*layout,
2870 lsa->lsa_mirror_count);
2874 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2877 lsa->lsa_mirror_count = 0;
2883 static int build_prev_component(struct llapi_layout **layout,
2884 struct lfs_setstripe_args *prev,
2885 struct lfs_setstripe_args *lsa,
2888 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2891 if (prev->lsa_stripe_size) {
2893 prev->lsa_comp_end = lsa->lsa_comp_end;
2894 prev->lsa_extension_size = lsa->lsa_extension_size;
2895 prev->lsa_extension_comp = true;
2898 rc = build_component(layout, prev, true);
2904 * Copy lsa to previous lsa;
2905 * if this is an extension component, make the previous invalid;
2908 prev->lsa_stripe_size = 0;
2915 #ifndef LCME_TEMPLATE_FLAGS
2916 #define LCME_TEMPLATE_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
2920 static int build_layout_from_yaml_node(struct cYAML *node,
2921 struct llapi_layout **layout,
2922 struct lfs_setstripe_args *lsa,
2923 struct lfs_setstripe_args *prevp)
2925 struct lfs_setstripe_args prev = { 0 };
2926 __u32 *osts = lsa->lsa_tgts;
2934 string = node->cy_string;
2936 if (node->cy_type == CYAML_TYPE_OBJECT) {
2937 /* go deep to sub blocks */
2938 if (string && !strncmp(string, "component", 9) &&
2939 strncmp(string, "component0", 10) &&
2940 strncmp(string, "components", 10)) {
2941 rc = build_prev_component(layout, prevp, lsa,
2946 /* initialize lsa. */
2947 setstripe_args_init(lsa);
2948 lsa->lsa_first_comp = false;
2949 lsa->lsa_tgts = osts;
2952 rc = build_layout_from_yaml_node(node->cy_child, layout,
2957 if (!node->cy_string)
2960 /* skip leading lmm_ if present, to simplify parsing */
2961 if (strncmp(string, "lmm_", 4) == 0)
2964 if (node->cy_type == CYAML_TYPE_STRING) {
2965 if (!strcmp(string, "lcme_extent.e_end")) {
2966 if (!strcmp(node->cy_valuestring, "EOF") ||
2967 !strcmp(node->cy_valuestring, "eof"))
2968 lsa->lsa_comp_end = LUSTRE_EOF;
2969 } else if (!strcmp(string, "pool")) {
2970 lsa->lsa_pool_name = node->cy_valuestring;
2971 } else if (!strcmp(string, "pattern")) {
2972 if (!strcmp(node->cy_valuestring, "mdt"))
2973 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2974 if (!strcmp(node->cy_valuestring,
2975 "raid0,overstriped"))
2977 LLAPI_LAYOUT_OVERSTRIPING;
2978 } else if (!strcmp(string, "lcme_flags")) {
2979 rc = comp_str2flags(node->cy_valuestring,
2980 &lsa->lsa_comp_flags,
2981 &lsa->lsa_comp_neg_flags);
2985 * Only template flags have meaning in
2986 * the layout for a new file
2988 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2990 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2991 if (!strcmp(string, "lcm_mirror_count")) {
2992 lsa->lsa_mirror_count = node->cy_valueint;
2993 } else if (!strcmp(string, "lcme_extent.e_start")) {
2994 if (node->cy_valueint == 0)
2995 lsa->lsa_first_comp = true;
2996 } else if (!strcmp(string, "lcme_extent.e_end")) {
2997 if (node->cy_valueint == -1)
2998 lsa->lsa_comp_end = LUSTRE_EOF;
3000 lsa->lsa_comp_end = node->cy_valueint;
3001 } else if (!strcmp(string, "stripe_count")) {
3002 lsa->lsa_stripe_count = node->cy_valueint;
3003 } else if (!strcmp(string, "stripe_size")) {
3004 lsa->lsa_stripe_size = node->cy_valueint;
3005 } else if (!strcmp(string, "extension_size")) {
3006 lsa->lsa_extension_size = node->cy_valueint;
3007 lsa->lsa_extension_comp = true;
3008 } else if (!strcmp(string, "stripe_offset")) {
3009 lsa->lsa_stripe_off = node->cy_valueint;
3010 } else if (!strcmp(string, "l_ost_idx")) {
3011 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3016 node = node->cy_next;
3019 if (prevp == &prev) {
3020 rc = build_prev_component(layout, prevp, lsa, true);
3024 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3025 rc = build_component(layout, lsa, *layout != NULL);
3031 static int lfs_comp_create_from_yaml(char *template,
3032 struct llapi_layout **layout,
3033 struct lfs_setstripe_args *lsa,
3036 struct cYAML *tree = NULL, *err_rc = NULL;
3039 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3041 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3042 progname, template);
3043 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3044 "can't parse", &err_rc);
3045 cYAML_print_tree2file(stderr, err_rc);
3046 cYAML_free_tree(err_rc);
3051 /* initialize lsa for plain file */
3052 setstripe_args_init(lsa);
3053 lsa->lsa_tgts = osts;
3055 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3057 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3058 progname, template);
3061 /* clean clean lsa */
3062 setstripe_args_init(lsa);
3066 cYAML_free_tree(tree);
3071 * Get the extension size from the next (SEL) component and extend the
3072 * current component on it. The start of the next component is to be
3075 * \param[in] layout the current layout
3076 * \param[in] start the start of the current component
3077 * \param[in,out] end the end of the current component
3078 * \param[in] offset the offset to adjust the end position to instead of
3081 * \retval 0 - extended successfully
3082 * \retval < 0 - error
3084 static int layout_extend_comp(struct llapi_layout *layout,
3085 uint64_t start, uint64_t *end,
3088 uint64_t size, next_start, next_end;
3091 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3094 "%s setstripe: cannot move component cursor: %s\n",
3095 progname, strerror(errno));
3100 * Even if the @size will not be used below, this will fail if
3101 * this is not a SEL component - a good confirmation we are
3102 * working on right components.
3104 rc = llapi_layout_extension_size_get(layout, &size);
3107 "%s setstripe: cannot get component ext size: %s\n",
3108 progname, strerror(errno));
3112 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3114 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3115 progname, strerror(errno));
3119 next_start += offset ?: size;
3120 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3122 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3123 progname, strerror(errno));
3127 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3130 "%s setstripe: cannot move component cursor: %s\n",
3131 progname, strerror(errno));
3135 *end += offset ?: size;
3136 rc = llapi_layout_comp_extent_set(layout, start, *end);
3138 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3139 progname, strerror(errno));
3147 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3148 * end of the last component in the existing file, and adjust the
3149 * first extent start of the components to be added accordingly.
3151 * In the create mode, we need to check if the first component is an extendable
3152 * SEL component and extend its length to the extension size (first component
3153 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3155 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3158 struct llapi_layout *head;
3159 uint64_t start = 0, prev_end = 0;
3163 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3168 head = llapi_layout_get_by_path(fname, 0);
3171 "%s setstripe: cannot read layout from '%s': %s\n",
3172 progname, fname, strerror(errno));
3174 } else if (errno == ENODATA) {
3176 * file without LOVEA, this component-add will be turned
3177 * into a component-create.
3179 llapi_layout_free(head);
3183 * the new layout will be added to an empty one, it
3184 * still needs to be adjusted below
3188 } else if (!llapi_layout_is_composite(head)) {
3190 "%s setstripe: '%s' not a composite file\n",
3192 llapi_layout_free(head);
3196 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3199 "%s setstripe: cannot get prev extent: %s\n",
3200 progname, strerror(errno));
3201 llapi_layout_free(head);
3205 llapi_layout_free(head);
3209 /* Make sure we use the first component of the layout to be added. */
3210 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3213 "%s setstripe: cannot move component cursor: %s\n",
3214 progname, strerror(errno));
3218 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3220 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3221 progname, strerror(errno));
3225 if (start == 0 && end == 0) {
3226 rc = layout_extend_comp(layout, start, &end,
3227 comp_add ? prev_end : 0);
3232 if (start > prev_end || end < prev_end) {
3234 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3235 progname, start, end, prev_end);
3239 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3242 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3243 progname, prev_end, end, strerror(errno));
3250 static int mirror_adjust_first_extents(struct mirror_args *list)
3258 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3261 list = list->m_next;
3267 static inline bool arg_is_eof(char *arg)
3269 return !strncmp(arg, "-1", strlen("-1")) ||
3270 !strncmp(arg, "EOF", strlen("EOF")) ||
3271 !strncmp(arg, "eof", strlen("eof"));
3275 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3277 * Return: Valid mirror_args pointer on success and
3278 * NULL if memory allocation fails.
3280 static struct mirror_args *lfs_mirror_alloc(void)
3282 struct mirror_args *mirror = NULL;
3285 mirror = calloc(1, sizeof(*mirror));
3287 mirror->m_inherit = false;
3298 * lfs_mirror_free() - Free memory allocated for a mirror argument
3300 * @mirror: Previously allocated mirror argument structure by
3301 * lfs_mirror_alloc().
3303 * Free memory allocated for @mirror.
3307 static void lfs_mirror_free(struct mirror_args *mirror)
3309 if (mirror->m_layout)
3310 llapi_layout_free(mirror->m_layout);
3315 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3316 * @mirror_list: Previously allocated mirror list.
3318 * Free memory allocated for @mirror_list.
3322 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3324 struct mirror_args *next_mirror = NULL;
3326 while (mirror_list) {
3327 next_mirror = mirror_list->m_next;
3328 lfs_mirror_free(mirror_list);
3329 mirror_list = next_mirror;
3334 LFS_SETQUOTA_DELETE = 1,
3342 LFS_COMP_NO_VERIFY_OPT,
3344 LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3346 LFS_MIRROR_STATE_OPT,
3348 LFS_MIRROR_INDEX_OPT,
3349 LFS_LAYOUT_FOREIGN_OPT,
3357 #ifndef LCME_USER_MIRROR_FLAGS
3358 /* The mirror flags can be set by users at creation time. */
3359 #define LCME_USER_MIRROR_FLAGS (LCME_FL_PREF_RW)
3363 static int lfs_setstripe_internal(int argc, char **argv,
3364 enum setstripe_origin opc)
3366 struct lfs_setstripe_args lsa = { 0 };
3367 struct llapi_stripe_param *param = NULL;
3368 struct find_param migrate_mdt_param = {
3378 unsigned long long size_units = 1;
3379 bool migrate_mode = false;
3380 bool migrate_mdt_mode = false;
3381 bool setstripe_mode = false;
3382 bool migration_block = false;
3383 __u64 migration_flags = 0;
3384 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3385 int comp_del = 0, comp_set = 0;
3388 struct llapi_layout *layout = NULL;
3389 struct llapi_layout **lpp = &layout;
3390 bool mirror_mode = false;
3391 bool has_m_file = false;
3392 __u32 mirror_count = 0;
3393 enum mirror_flags mirror_flags = 0;
3394 struct mirror_args *mirror_list = NULL;
3395 struct mirror_args *new_mirror = NULL;
3396 struct mirror_args *last_mirror = NULL;
3397 __u16 mirror_id = 0;
3399 bool from_yaml = false;
3400 bool from_copy = false;
3401 char *template = NULL;
3402 bool foreign_mode = false;
3404 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3405 char *mode_opt = NULL;
3406 mode_t previous_umask = 0;
3407 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3409 struct option long_opts[] = {
3410 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3411 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3412 /* --block is only valid in migrate mode */
3413 { .val = 'b', .name = "block", .has_arg = no_argument },
3414 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3415 { .val = LFS_COMP_ADD_OPT,
3416 .name = "comp-add", .has_arg = no_argument },
3417 { .val = LFS_COMP_ADD_OPT,
3418 .name = "component-add", .has_arg = no_argument },
3419 { .val = LFS_COMP_DEL_OPT,
3420 .name = "comp-del", .has_arg = no_argument },
3421 { .val = LFS_COMP_DEL_OPT,
3422 .name = "component-del", .has_arg = no_argument },
3423 { .val = LFS_COMP_FLAGS_OPT,
3424 .name = "comp-flags", .has_arg = required_argument },
3425 { .val = LFS_COMP_FLAGS_OPT,
3426 .name = "component-flags",
3427 .has_arg = required_argument },
3428 { .val = LFS_COMP_SET_OPT,
3429 .name = "comp-set", .has_arg = no_argument },
3430 { .val = LFS_COMP_SET_OPT,
3431 .name = "component-set",
3432 .has_arg = no_argument},
3433 { .val = LFS_COMP_NO_VERIFY_OPT,
3434 .name = "no-verify", .has_arg = no_argument},
3435 { .val = LFS_LAYOUT_FLAGS_OPT,
3436 .name = "flags", .has_arg = required_argument},
3437 { .val = LFS_LAYOUT_FOREIGN_OPT,
3438 .name = "foreign", .has_arg = optional_argument},
3439 { .val = LFS_MIRROR_ID_OPT,
3440 .name = "mirror-id", .has_arg = required_argument},
3441 { .val = LFS_MODE_OPT,
3442 .name = "mode", .has_arg = required_argument},
3443 { .val = LFS_LAYOUT_COPY,
3444 .name = "copy", .has_arg = required_argument},
3445 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3446 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3447 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3448 { .val = 'C', .name = "overstripe-count",
3449 .has_arg = required_argument},
3450 { .val = 'd', .name = "delete", .has_arg = no_argument},
3451 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3452 /* used with "lfs migrate -m" */
3453 { .val = 'd', .name = "directory", .has_arg = no_argument},
3454 /* --non-direct is only valid in migrate mode */
3455 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3456 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3457 { .val = 'E', .name = "component-end",
3458 .has_arg = required_argument},
3459 { .val = 'f', .name = "file", .has_arg = required_argument },
3460 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3461 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3462 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3463 { .val = 'h', .name = "help", .has_arg = no_argument },
3464 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3465 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3466 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3467 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3468 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3469 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3470 { .val = 'L', .name = "layout", .has_arg = required_argument },
3471 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3472 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3473 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3474 /* --non-block is only valid in migrate mode */
3475 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3476 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3477 { .val = 'o', .name = "ost", .has_arg = required_argument },
3478 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3479 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3480 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3482 { .val = 'p', .name = "pool", .has_arg = required_argument },
3483 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3484 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3485 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3486 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3487 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3488 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3489 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3490 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3491 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3492 /* --verbose is only valid in migrate mode */
3493 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3494 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3495 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3496 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3497 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3498 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3501 setstripe_args_init(&lsa);
3503 migrate_mode = (opc == SO_MIGRATE);
3504 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3505 setstripe_mode = (opc == SO_SETSTRIPE);
3506 if (opc == SO_MIRROR_DELETE) {
3508 mirror_flags = MF_DESTROY;
3511 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3513 while ((c = getopt_long(argc, argv,
3514 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:y:z:",
3515 long_opts, NULL)) >= 0) {
3521 case LFS_COMP_ADD_OPT:
3524 case LFS_COMP_DEL_OPT:
3527 case LFS_COMP_FLAGS_OPT:
3528 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3529 &lsa.lsa_comp_neg_flags);
3532 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3534 "%s: inverted flags are not supported\n",
3539 case LFS_COMP_SET_OPT:
3542 case LFS_COMP_NO_VERIFY_OPT:
3543 mirror_flags |= MF_NO_VERIFY;
3545 case LFS_MIRROR_ID_OPT: {
3546 unsigned long int id;
3549 id = strtoul(optarg, &end, 0);
3550 if (errno != 0 || *end != '\0' || id == 0 ||
3553 "%s %s: invalid mirror ID '%s'\n",
3554 progname, argv[0], optarg);
3558 mirror_id = (__u16)id;
3561 case LFS_LAYOUT_FLAGS_OPT: {
3564 /* check for numeric flags (foreign and mirror cases) */
3565 if (setstripe_mode && !mirror_mode && !last_mirror) {
3567 flags = strtoul(optarg, &end, 16);
3568 if (errno != 0 || *end != '\0' ||
3569 flags >= UINT32_MAX) {
3571 "%s %s: invalid hex flags '%s'\n",
3572 progname, argv[0], optarg);
3575 if (!foreign_mode) {
3577 "%s %s: hex flags must be specified with --foreign option\n",
3584 if (!mirror_mode || !last_mirror) {
3586 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3591 result = comp_str2flags(optarg, &last_mirror->m_flags,
3598 "%s: inverted flags are not supported\n",
3603 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3605 "%s: unsupported mirror flags: %s\n",
3612 case LFS_LAYOUT_FOREIGN_OPT:
3614 /* check pure numeric */
3615 type = strtoul(optarg, &end, 0);
3618 type = check_foreign_type_name(optarg);
3619 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3621 "%s %s: unrecognized foreign type '%s'\n",
3626 } else if (type >= UINT32_MAX) {
3628 "%s %s: invalid foreign type '%s'\n",
3629 progname, argv[0], optarg);
3633 foreign_mode = true;
3638 mode = strtoul(mode_opt, &end, 8);
3641 "%s %s: bad mode '%s'\n",
3642 progname, argv[0], mode_opt);
3645 previous_umask = umask(0);
3648 case LFS_LAYOUT_COPY:
3653 if (!migrate_mode) {
3655 "%s %s: -b|--block valid only for migrate command\n",
3659 migration_block = true;
3662 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3664 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3668 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3672 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3673 if (errno != 0 || *end != '\0'|| optarg == end ||
3674 lsa.lsa_stripe_count < -1 ||
3675 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3677 "%s %s: invalid stripe count '%s'\n",
3678 progname, argv[0], optarg);
3682 if (lsa.lsa_stripe_count == -1)
3683 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3687 migrate_mdt_param.fp_max_depth = 1;
3689 /* delete the default striping pattern */
3691 if (opc == SO_MIRROR_SPLIT) {
3694 "%s %s: -d cannot used with -f\n",
3698 mirror_flags |= MF_DESTROY;
3703 if (!migrate_mode) {
3705 "%s %s: -D|--non-direct is valid only for migrate command\n",
3709 migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3712 if (lsa.lsa_comp_end != 0) {
3713 result = comp_args_to_layout(lpp, &lsa, true);
3715 fprintf(stderr, "%s: invalid layout\n",
3720 setstripe_args_init_inherit(&lsa);
3723 if (arg_is_eof(optarg)) {
3724 lsa.lsa_comp_end = LUSTRE_EOF;
3726 result = llapi_parse_size(optarg,
3729 /* assume units of KB if too small */
3730 if (lsa.lsa_comp_end < 4096)
3731 lsa.lsa_comp_end *= 1024;
3733 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3735 "%s %s: invalid component end '%s'\n",
3736 progname, argv[0], optarg);
3742 if (!migrate_mode) {
3744 "--mdt-hash is valid only for migrate command\n");
3748 lsa.lsa_pattern = check_hashtype(optarg);
3749 if (lsa.lsa_pattern == 0) {
3751 "%s %s: bad stripe hash type '%s'\n",
3752 progname, argv[0], optarg);
3758 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3759 if (errno != 0 || *end != '\0' || optarg == end ||
3760 lsa.lsa_stripe_off < -1 ||
3761 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3763 "%s %s: invalid stripe offset '%s'\n",
3764 progname, argv[0], optarg);
3767 if (lsa.lsa_stripe_off == -1)
3768 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3771 comp_id = strtoul(optarg, &end, 0);
3772 if (*end != '\0' || comp_id == 0 ||
3773 comp_id > LCME_ID_MAX) {
3775 "%s %s: invalid component ID '%s'\n",
3776 progname, argv[0], optarg);
3781 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3783 "error: %s: invalid option: %s\n",
3784 progname, argv[optopt + 1]);
3787 if (opc == SO_MIRROR_EXTEND) {
3790 "error: %s: '-N' must exist in front of '%s'\n",
3791 progname, argv[optopt + 1]);
3794 last_mirror->m_file = optarg;
3795 last_mirror->m_count = 1;
3799 mirror_list = lfs_mirror_alloc();
3800 mirror_list->m_file = optarg;
3805 if (strcmp(argv[optind - 1], "mdt") == 0) {
3806 /* Can be only the first component */
3810 "error: 'mdt' layout can be only the first one\n");
3813 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3816 "error: 'mdt' layout size is too big\n");
3819 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3820 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
3821 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3824 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3830 if (!migrate_mode) {
3832 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3836 migrate_mdt_mode = true;
3837 lsa.lsa_nr_tgts = parse_targets(tgts,
3838 sizeof(tgts) / sizeof(__u32),
3839 lsa.lsa_nr_tgts, optarg, NULL);
3840 if (lsa.lsa_nr_tgts < 0) {
3842 "%s: invalid MDT target(s) '%s'\n",
3847 lsa.lsa_tgts = tgts;
3848 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3849 lsa.lsa_stripe_off = tgts[0];
3852 if (!migrate_mode) {
3854 "%s %s: -n|--non-block valid only for migrate command\n",
3858 migration_flags |= LLAPI_MIGRATION_NONBLOCK;
3861 if (opc == SO_SETSTRIPE) {
3862 opc = SO_MIRROR_CREATE;
3868 mirror_count = strtoul(optarg, &end, 0);
3869 if (errno != 0 || *end != '\0' ||
3870 mirror_count == 0 ||
3871 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3873 "error: %s: bad mirror count: %s\n",
3880 new_mirror = lfs_mirror_alloc();
3881 new_mirror->m_count = mirror_count;
3884 mirror_list = new_mirror;
3887 /* wrap up last mirror */
3888 if (!setstripe_args_specified(&lsa))
3889 last_mirror->m_inherit = true;
3890 if (lsa.lsa_comp_end == 0)
3891 lsa.lsa_comp_end = LUSTRE_EOF;
3893 result = comp_args_to_layout(lpp, &lsa, true);
3895 lfs_mirror_free(new_mirror);
3899 setstripe_args_init_inherit(&lsa);
3901 last_mirror->m_next = new_mirror;
3904 last_mirror = new_mirror;
3905 lpp = &last_mirror->m_layout;
3908 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3909 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3911 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3913 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3915 "%s %s: -o|--ost incompatible with DoM layout\n",
3920 * -o allows overstriping, and must note it because
3921 * parse_targets is shared with MDT striping, which
3922 * does not allow duplicates
3924 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3925 lsa.lsa_nr_tgts = parse_targets(tgts,
3926 sizeof(tgts) / sizeof(__u32),
3927 lsa.lsa_nr_tgts, optarg,
3929 if (lsa.lsa_nr_tgts < 0) {
3931 "%s %s: invalid OST target(s) '%s'\n",
3932 progname, argv[0], optarg);
3936 lsa.lsa_tgts = tgts;
3937 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3938 lsa.lsa_stripe_off = tgts[0];
3943 lsa.lsa_pool_name = optarg;
3945 if (strlen(lsa.lsa_pool_name) == 0 ||
3946 strncmp(lsa.lsa_pool_name, "none",
3947 LOV_MAXPOOLNAME) == 0)
3948 lsa.lsa_pool_name = NULL;
3951 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3953 /* assume units of KB if too small to be valid */
3954 if (lsa.lsa_stripe_size < 4096)
3955 lsa.lsa_stripe_size *= 1024;
3957 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3959 "%s %s: invalid stripe size '%s'\n",
3960 progname, argv[0], optarg);
3965 if (!migrate_mode) {
3967 "%s %s: -v|--verbose valid only for migrate command\n",
3971 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3972 migration_flags = LLAPI_MIGRATION_VERBOSE;
3982 result = llapi_parse_size(optarg,
3983 &lsa.lsa_extension_size,
3987 "%s %s: invalid extension size '%s'\n",
3988 progname, argv[0], optarg);
3992 lsa.lsa_extension_comp = true;
3995 fprintf(stderr, "%s: unrecognized option '%s'\n",
3996 progname, argv[optind - 1]);
4002 fname = argv[optind];
4004 if (optind == argc) {
4005 fprintf(stderr, "%s %s: FILE must be specified\n",
4010 /* lfs migrate $filename should keep the file's layout by default */
4011 if (migrate_mode && !layout && !from_yaml &&
4012 !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4015 if (xattr && !foreign_mode) {
4017 * only print a warning as this is harmless and will be ignored
4020 "%s %s: xattr has been specified for non-foreign layout\n",
4022 } else if (foreign_mode && !xattr) {
4024 "%s %s: xattr must be provided in foreign mode\n",
4029 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4030 comp_set || comp_id || delete || from_copy ||
4031 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4034 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4039 if (mirror_mode && mirror_count == 0) {
4041 "error: %s: --mirror-count|-N option is required\n",
4048 if (!setstripe_args_specified(&lsa))
4049 last_mirror->m_inherit = true;
4050 if (lsa.lsa_comp_end == 0)
4051 lsa.lsa_comp_end = LUSTRE_EOF;
4054 if (lsa.lsa_comp_end != 0) {
4055 result = comp_args_to_layout(lpp, &lsa, true);
4057 fprintf(stderr, "error: %s: invalid layout\n",
4064 if (mirror_flags & MF_NO_VERIFY) {
4065 if (opc != SO_MIRROR_EXTEND) {
4067 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4071 } else if (!has_m_file) {
4073 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4080 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4082 "%s %s: --component-set doesn't have component-id set\n",
4087 if ((delete + comp_set + comp_del + comp_add) > 1) {
4089 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4094 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4095 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4097 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4102 if ((comp_set || comp_del) &&
4103 (setstripe_args_specified(&lsa) || layout != NULL)) {
4105 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4110 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4112 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4117 if (comp_add || comp_del) {
4120 result = lstat(fname, &st);
4121 if (result == 0 && S_ISDIR(st.st_mode)) {
4123 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4130 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4139 "%s %s: option -E must be specified with --component-add\n",
4145 if (from_yaml && from_copy) {
4147 "%s: can't specify --yaml and --copy together\n",
4152 if ((from_yaml || from_copy) &&
4153 (setstripe_args_specified(&lsa) || layout != NULL)) {
4155 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4160 if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4162 "%s %s: options --non-block and --block are mutually exclusive\n",
4167 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4168 opc != SO_MIRROR_DELETE && comp_id != 0) {
4170 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4175 if (migrate_mdt_mode) {
4176 struct lmv_user_md *lmu;
4178 /* initialize migrate mdt parameters */
4179 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4180 LMV_USER_MAGIC_SPECIFIC));
4183 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4184 progname, argv[0], strerror(ENOMEM));
4188 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4189 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4190 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4192 "%s %s: migrate should specify MDT index\n",
4197 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4198 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4199 lmu->lum_hash_type = lsa.lsa_pattern;
4201 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4202 if (lsa.lsa_pool_name) {
4203 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4204 sizeof(lmu->lum_pool_name) - 1);
4205 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4207 if (lsa.lsa_nr_tgts > 1) {
4210 if (lsa.lsa_stripe_count > 0 &&
4211 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4212 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4214 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4215 progname, lsa.lsa_stripe_count,
4221 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4222 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4223 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4224 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4226 lmu->lum_magic = LMV_USER_MAGIC;
4229 migrate_mdt_param.fp_lmv_md = lmu;
4230 migrate_mdt_param.fp_migrate = 1;
4231 } else if (!layout) {
4232 if (lsa_args_stripe_count_check(&lsa))
4235 /* initialize stripe parameters */
4236 param = calloc(1, offsetof(typeof(*param),
4237 lsp_osts[lsa.lsa_nr_tgts]));
4240 "%s %s: cannot allocate memory for parameters: %s\n",
4241 progname, argv[0], strerror(ENOMEM));
4246 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4247 param->lsp_stripe_size = lsa.lsa_stripe_size;
4248 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4249 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4250 param->lsp_stripe_count = -1;
4252 param->lsp_stripe_count = lsa.lsa_stripe_count;
4254 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4255 param->lsp_stripe_offset = -1;
4257 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4258 param->lsp_stripe_pattern =
4259 llapi_pattern_to_lov(lsa.lsa_pattern);
4260 if (param->lsp_stripe_pattern == EINVAL) {
4261 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4266 param->lsp_pool = lsa.lsa_pool_name;
4267 param->lsp_is_specific = false;
4269 if (lsa.lsa_nr_tgts > 0) {
4270 param->lsp_is_specific = true;
4271 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4272 memcpy(param->lsp_osts, tgts,
4273 sizeof(*tgts) * lsa.lsa_nr_tgts);
4278 /* generate a layout from a YAML template */
4279 result = lfs_comp_create_from_yaml(template, &layout,
4283 "error: %s: can't create composite layout from template file %s\n",
4289 if (layout != NULL || mirror_list != NULL) {
4291 result = mirror_adjust_first_extents(mirror_list);
4293 result = layout_adjust_first_extent(fname, layout,
4295 if (result == -ENODATA)
4297 else if (result != 0) {
4298 fprintf(stderr, "error: %s: invalid layout\n",
4304 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4306 layout = llapi_layout_get_by_path(template ?: fname, 0);
4309 "%s: can't create composite layout from file %s: %s\n",
4310 progname, template ?: fname,
4317 if (migrate_mdt_mode) {
4318 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4319 } else if (migrate_mode) {
4320 result = lfs_migrate(fname, migration_flags, param,
4322 } else if (comp_set != 0) {
4323 result = lfs_component_set(fname, comp_id,
4326 lsa.lsa_comp_neg_flags);
4327 } else if (comp_del != 0) {
4328 result = lfs_component_del(fname, comp_id,
4330 lsa.lsa_comp_neg_flags);
4331 } else if (comp_add != 0) {
4332 result = lfs_component_add(fname, layout);
4333 } else if (opc == SO_MIRROR_CREATE) {
4334 result = mirror_create(fname, mirror_list);
4335 } else if (opc == SO_MIRROR_EXTEND) {
4336 result = mirror_extend(fname, mirror_list,
4338 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4339 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4341 "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4345 if (lsa.lsa_pool_name)
4346 mirror_flags |= MF_COMP_POOL;
4347 else if (mirror_id != 0)
4348 comp_id = mirror_id;
4350 mirror_flags |= MF_COMP_ID;
4351 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4353 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4357 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4359 has_m_file ? mirror_list->m_file :
4361 } else if (layout) {
4362 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4368 } else if (foreign_mode) {
4369 result = llapi_file_create_foreign(fname, mode, type,
4376 result = llapi_file_open_param(fname,
4385 /* Save the first error encountered. */
4393 umask(previous_umask);
4396 free(migrate_mdt_param.fp_lmv_md);
4397 llapi_layout_free(layout);
4398 lfs_mirror_list_free(mirror_list);
4403 llapi_layout_free(layout);
4404 lfs_mirror_list_free(mirror_list);
4408 static int lfs_poollist(int argc, char **argv)
4413 return llapi_poollist(argv[1]);
4416 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4417 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4422 char *endptr = "AD";
4427 else if (str[0] == '-')
4433 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4434 long long val = strtoll(timebuf, &endptr, 0);
4439 unit *= 52; /* 52 weeks + 1 day below */
4443 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4444 param->fp_time_margin *= (1 + unit / 52);
4445 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4447 case '\0': /* days are default unit if none used */
4460 /* don't need to multiply by 1 for seconds */
4463 "%s find: bad time string '%s': %s\n",
4464 progname, timebuf, strerror(EINVAL));
4468 if (param->fp_time_margin == 0 ||
4469 (*endptr && unit < param->fp_time_margin))
4470 param->fp_time_margin = unit;
4477 fprintf(stderr, "%s find: bad time '%s': too large\n",
4487 static int str2quotaid(__u32 *id, const char *arg)
4489 unsigned long int projid_tmp = 0;
4490 char *endptr = NULL;
4492 projid_tmp = strtoul(arg, &endptr, 10);
4493 if (*endptr != '\0')
4495 /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4496 if (projid_tmp >= UINT32_MAX)
4503 static int name2uid(unsigned int *id, const char *name)
4505 struct passwd *passwd;
4507 passwd = getpwnam(name);
4510 *id = passwd->pw_uid;
4515 static int name2gid(unsigned int *id, const char *name)
4517 struct group *group;
4519 group = getgrnam(name);
4522 *id = group->gr_gid;
4527 static inline int name2projid(unsigned int *id, const char *name)
4532 static int uid2name(char **name, unsigned int id)
4534 struct passwd *passwd;
4536 passwd = getpwuid(id);
4539 *name = passwd->pw_name;
4544 static inline int gid2name(char **name, unsigned int id)
4546 struct group *group;
4548 group = getgrgid(id);
4551 *name = group->gr_name;
4556 static int name2layout(__u32 *layout, char *name)
4558 char *ptr, *layout_name;
4561 for (ptr = name; ; ptr = NULL) {
4562 layout_name = strtok(ptr, ",");
4565 if (strcmp(layout_name, "released") == 0)
4566 *layout |= LOV_PATTERN_F_RELEASED;
4567 else if (strcmp(layout_name, "raid0") == 0)
4568 *layout |= LOV_PATTERN_RAID0;
4569 else if (strcmp(layout_name, "mdt") == 0)
4570 *layout |= LOV_PATTERN_MDT;
4571 else if (strcmp(layout_name, "overstriping") == 0)
4572 *layout |= LOV_PATTERN_OVERSTRIPING;
4579 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4582 int user, group, other;
4587 mode_t previous_flags;
4589 user = group = other = 0;
4609 user = group = other = 1;
4620 who = user || group || other;
4623 usermask = umask(0022);
4628 if (*input == '-' || *input == '+' || *input == '=')
4631 /* operation is required */
4634 /* get the flags in *outmode */
4637 previous_flags = (*outmode & 0700);
4638 perm |= user ? previous_flags : 0;
4639 perm |= group ? (previous_flags >> 3) : 0;
4640 perm |= other ? (previous_flags >> 6) : 0;
4644 previous_flags = (*outmode & 0070);
4645 perm |= user ? (previous_flags << 3) : 0;
4646 perm |= group ? previous_flags : 0;
4647 perm |= other ? (previous_flags >> 3) : 0;
4651 previous_flags = (*outmode & 0007);
4652 perm |= user ? (previous_flags << 6) : 0;
4653 perm |= group ? (previous_flags << 3) : 0;
4654 perm |= other ? previous_flags : 0;
4661 /* this part is optional,
4662 * if empty perm = 0 and *outmode is not modified
4669 perm |= user ? 0400 : 0;
4670 perm |= group ? 0040 : 0;
4671 perm |= other ? 0004 : 0;
4672 /* set read permission for uog except for umask's
4675 perm |= who ? 0 : (0444 & ~usermask);
4678 perm |= user ? 0200 : 0;
4679 perm |= group ? 0020 : 0;
4680 perm |= other ? 0002 : 0;
4681 /* set write permission for uog except for umask'
4684 perm |= who ? 0 : (0222 & ~usermask);
4687 perm |= user ? 0100 : 0;
4688 perm |= group ? 0010 : 0;
4689 perm |= other ? 0001 : 0;
4690 /* set execute permission for uog except for umask'
4693 perm |= who ? 0 : (0111 & ~usermask);
4697 * Adds execute permission to 'u', 'g' and/or 'g' if
4698 * specified and either 'u', 'g' or 'o' already has
4699 * execute permissions.
4701 if ((*outmode & 0111) != 0) {
4702 perm |= user ? 0100 : 0;
4703 perm |= group ? 0010 : 0;
4704 perm |= other ? 0001 : 0;
4705 perm |= !who ? 0111 : 0;
4709 /* s is ignored if o is given, but it's not an error */
4710 if (other && !group && !user)
4712 perm |= user ? S_ISUID : 0;
4713 perm |= group ? S_ISGID : 0;
4716 /* 't' should be used when 'a' is given
4719 perm |= (!who || all) ? S_ISVTX : 0;
4720 /* using ugo with t is not an error */
4731 /* uog flags should be only one character long */
4732 if (previous_flags && (*input != '\0' && *input != ','))
4737 /* remove the flags from outmode */
4741 /* add the flags to outmode */
4745 /* set the flags of outmode to perm */
4755 static int str2mode_t(const char *input, mode_t *outmode)
4762 if (*input >= '0' && *input <= '7') {
4763 /* parse octal representation */
4768 /* look for invalid digits in octal representation */
4769 while (isdigit(*iter))
4774 *outmode = strtoul(input, &end, 8);
4776 if (errno != 0 || *outmode > 07777) {
4781 } else if (*input == '8' || *input == '9') {
4782 /* error: invalid octal number */
4785 /* parse coma seperated list of symbolic representation */
4794 rc = parse_symbolic(input, outmode, &end);
4799 } while (*end == ',');
4807 static int lfs_find(int argc, char **argv)
4812 struct find_param param = {
4815 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4817 struct option long_opts[] = {
4818 { .val = 'A', .name = "atime", .has_arg = required_argument },
4819 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4820 { .val = 'B', .name = "btime", .has_arg = required_argument },
4821 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4822 { .val = LFS_COMP_COUNT_OPT,
4823 .name = "comp-count", .has_arg = required_argument },
4824 { .val = LFS_COMP_COUNT_OPT,
4825 .name = "component-count",
4826 .has_arg = required_argument },
4827 { .val = LFS_COMP_FLAGS_OPT,
4828 .name = "comp-flags", .has_arg = required_argument },
4829 { .val = LFS_COMP_FLAGS_OPT,
4830 .name = "component-flags",
4831 .has_arg = required_argument },
4832 { .val = LFS_COMP_START_OPT,
4833 .name = "comp-start", .has_arg = required_argument },
4834 { .val = LFS_COMP_START_OPT,
4835 .name = "component-start",
4836 .has_arg = required_argument },
4837 { .val = LFS_MIRROR_STATE_OPT,
4838 .name = "mirror-state", .has_arg = required_argument },
4839 { .val = LFS_NEWERXY_OPT,
4840 .name = "newer", .has_arg = required_argument},
4841 { .val = LFS_NEWERXY_OPT,
4842 .name = "neweraa", .has_arg = required_argument},
4843 { .val = LFS_NEWERXY_OPT,
4844 .name = "neweram", .has_arg = required_argument},
4845 { .val = LFS_NEWERXY_OPT,
4846 .name = "newerac", .has_arg = required_argument},
4847 { .val = LFS_NEWERXY_OPT,
4848 .name = "newerab", .has_arg = required_argument},
4849 { .val = LFS_NEWERXY_OPT,
4850 .name = "newerma", .has_arg = required_argument},
4851 { .val = LFS_NEWERXY_OPT,
4852 .name = "newermm", .has_arg = required_argument},
4853 { .val = LFS_NEWERXY_OPT,
4854 .name = "newermc", .has_arg = required_argument},
4855 { .val = LFS_NEWERXY_OPT,
4856 .name = "newermb", .has_arg = required_argument},
4857 { .val = LFS_NEWERXY_OPT,
4858 .name = "newerca", .has_arg = required_argument},
4859 { .val = LFS_NEWERXY_OPT,
4860 .name = "newercm", .has_arg = required_argument},
4861 { .val = LFS_NEWERXY_OPT,
4862 .name = "newercc", .has_arg = required_argument},
4863 { .val = LFS_NEWERXY_OPT,
4864 .name = "newercb", .has_arg = required_argument},
4865 { .val = LFS_NEWERXY_OPT,
4866 .name = "newerba", .has_arg = required_argument},
4867 { .val = LFS_NEWERXY_OPT,
4868 .name = "newerbm", .has_arg = required_argument},
4869 { .val = LFS_NEWERXY_OPT,
4870 .name = "newerbc", .has_arg = required_argument},
4871 { .val = LFS_NEWERXY_OPT,
4872 .name = "newerbb", .has_arg = required_argument},
4873 { .val = LFS_NEWERXY_OPT,
4874 .name = "newerBa", .has_arg = required_argument},
4875 { .val = LFS_NEWERXY_OPT,
4876 .name = "newerBm", .has_arg = required_argument},
4877 { .val = LFS_NEWERXY_OPT,
4878 .name = "newerBc", .has_arg = required_argument},
4879 { .val = LFS_NEWERXY_OPT,
4880 .name = "newerBB", .has_arg = required_argument},
4881 { .val = LFS_NEWERXY_OPT,
4882 .name = "newerat", .has_arg = required_argument},
4883 { .val = LFS_NEWERXY_OPT,
4884 .name = "newermt", .has_arg = required_argument},
4885 { .val = LFS_NEWERXY_OPT,
4886 .name = "newerct", .has_arg = required_argument},
4887 { .val = LFS_NEWERXY_OPT,
4888 .name = "newerbt", .has_arg = required_argument},
4889 { .val = LFS_NEWERXY_OPT,
4890 .name = "newerBt", .has_arg = required_argument},
4891 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4892 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4893 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4894 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4895 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4896 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4897 { .val = 'E', .name = "component-end",
4898 .has_arg = required_argument },
4899 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4900 { .val = LFS_LAYOUT_FOREIGN_OPT,
4901 .name = "foreign", .has_arg = optional_argument},
4902 { .val = 'g', .name = "gid", .has_arg = required_argument },
4903 { .val = 'G', .name = "group", .has_arg = required_argument },
4904 { .val = 'h', .name = "help", .has_arg = no_argument },
4905 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4906 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4907 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4908 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4909 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4910 { .val = 'L', .name = "layout", .has_arg = required_argument },
4911 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4912 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4913 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4914 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4915 { .val = 'n', .name = "name", .has_arg = required_argument },
4916 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4917 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4918 { .val = 'O', .name = "obd", .has_arg = required_argument },
4919 { .val = 'O', .name = "ost", .has_arg = required_argument },
4920 { .val = LFS_FIND_PERM,
4921 .name = "perm", .has_arg = required_argument },
4922 /* no short option for pool yet, can be 'p' after 2.18 */
4923 { .val = LFS_POOL_OPT,
4924 .name = "pool", .has_arg = required_argument },
4925 { .val = '0', .name = "print0", .has_arg = no_argument },
4926 { .val = 'P', .name = "print", .has_arg = no_argument },
4927 { .val = LFS_PRINTF_OPT,
4928 .name = "printf", .has_arg = required_argument },
4929 { .val = LFS_PROJID_OPT,
4930 .name = "projid", .has_arg = required_argument },
4931 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4932 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4933 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4934 { .val = 's', .name = "size", .has_arg = required_argument },
4935 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4936 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4937 { .val = 't', .name = "type", .has_arg = required_argument },
4938 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4939 { .val = 'u', .name = "uid", .has_arg = required_argument },
4940 { .val = 'U', .name = "user", .has_arg = required_argument },
4941 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4942 { .val = 'z', .name = "extension-size",
4943 .has_arg = required_argument },
4944 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4958 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4959 while ((c = getopt_long_only(argc, argv,
4960 "-0A:b:B:c:C:D:E:g:G:hH:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:z:",
4961 long_opts, &optidx)) >= 0) {
4966 /* '!' is part of option */
4968 * when getopt_long_only() finds a string which is not
4969 * an option nor a known option argument it returns 1
4970 * in that case if we already have found pathstart and pathend
4971 * (i.e. we have the list of pathnames),
4972 * the only supported value is "!"
4974 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4975 if (!isoption && pathend != -1) {
4977 "err: %s: filename|dirname must either precede options or follow options\n",
4982 if (!isoption && pathstart == -1)
4983 pathstart = optind - 1;
4984 if (isoption && pathstart != -1 && pathend == -1)
4985 pathend = optind - 2;
4992 * unknown; opt is "!" or path component,
4993 * checking done above.
4995 if (strcmp(optarg, "!") == 0)
4999 xtime = ¶m.fp_atime;
5000 xsign = ¶m.fp_asign;
5001 param.fp_exclude_atime = !!neg_opt;
5002 /* no break, this falls through to 'B' for btime */
5006 xtime = ¶m.fp_btime;
5007 xsign = ¶m.fp_bsign;
5008 param.fp_exclude_btime = !!neg_opt;
5010 /* no break, this falls through to 'C' for ctime */
5014 xtime = ¶m.fp_ctime;
5015 xsign = ¶m.fp_csign;
5016 param.fp_exclude_ctime = !!neg_opt;
5018 /* no break, this falls through to 'M' for mtime */
5022 xtime = ¶m.fp_mtime;
5023 xsign = ¶m.fp_msign;
5024 param.fp_exclude_mtime = !!neg_opt;
5026 rc = set_time(¶m, &t, xtime, optarg);
5027 if (rc == LONG_MAX) {
5035 if (optarg[0] == '+') {
5036 param.fp_blocks_sign = -1;
5038 } else if (optarg[0] == '-') {
5039 param.fp_blocks_sign = 1;
5043 param.fp_blocks_units = 1024;
5044 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5045 ¶m.fp_blocks_units, 0);
5047 fprintf(stderr, "error: bad blocks '%s'\n",
5051 param.fp_check_blocks = 1;
5052 param.fp_exclude_blocks = !!neg_opt;
5054 case LFS_COMP_COUNT_OPT:
5055 if (optarg[0] == '+') {
5056 param.fp_comp_count_sign = -1;
5058 } else if (optarg[0] == '-') {
5059 param.fp_comp_count_sign = 1;
5064 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5065 if (errno != 0 || *endptr != '\0' ||
5066 param.fp_comp_count > UINT32_MAX) {
5068 "error: bad component count '%s'\n",
5072 param.fp_check_comp_count = 1;
5073 param.fp_exclude_comp_count = !!neg_opt;
5075 case LFS_COMP_FLAGS_OPT:
5076 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5077 ¶m.fp_comp_neg_flags);
5080 "error: bad component flags '%s'\n",
5084 param.fp_check_comp_flags = 1;
5086 __u32 flags = param.fp_comp_neg_flags;
5088 param.fp_comp_neg_flags = param.fp_comp_flags;
5089 param.fp_comp_flags = flags;
5092 case LFS_COMP_START_OPT:
5093 if (optarg[0] == '+') {
5094 param.fp_comp_start_sign = -1;
5096 } else if (optarg[0] == '-') {
5097 param.fp_comp_start_sign = 1;
5101 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5102 ¶m.fp_comp_start_units, 0);
5105 "error: bad component start '%s'\n",
5109 param.fp_check_comp_start = 1;
5110 param.fp_exclude_comp_start = !!neg_opt;
5112 case LFS_MIRROR_STATE_OPT:
5113 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5114 ¶m.fp_mirror_neg_state);
5117 "error: bad mirrored file state '%s'\n",
5121 param.fp_check_mirror_state = 1;
5123 __u16 state = param.fp_mirror_neg_state;
5125 param.fp_mirror_neg_state =
5126 param.fp_mirror_state;
5127 param.fp_mirror_state = state;
5131 if (optarg[0] == '+') {
5132 param.fp_stripe_count_sign = -1;
5134 } else if (optarg[0] == '-') {
5135 param.fp_stripe_count_sign = 1;
5140 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5141 if (errno != 0 || *endptr != '\0' ||
5142 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5144 "error: bad stripe_count '%s'\n",
5149 param.fp_check_stripe_count = 1;
5150 param.fp_exclude_stripe_count = !!neg_opt;
5154 param.fp_max_depth = strtol(optarg, 0, 0);
5155 if (errno != 0 || param.fp_max_depth < 0) {
5157 "error: bad maxdepth '%s'\n",
5164 if (optarg[0] == '+') {
5165 param.fp_comp_end_sign = -1;
5167 } else if (optarg[0] == '-') {
5168 param.fp_comp_end_sign = 1;
5172 if (arg_is_eof(optarg)) {
5173 param.fp_comp_end = LUSTRE_EOF;
5174 param.fp_comp_end_units = 1;
5177 rc = llapi_parse_size(optarg,
5179 ¶m.fp_comp_end_units, 0);
5180 /* assume units of KB if too small */
5181 if (param.fp_comp_end < 4096)
5182 param.fp_comp_end *= 1024;
5186 "error: bad component end '%s'\n",
5190 param.fp_check_comp_end = 1;
5191 param.fp_exclude_comp_end = !!neg_opt;
5193 case LFS_LAYOUT_FOREIGN_OPT: {
5194 /* all types by default */
5195 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5198 /* check pure numeric */
5199 type = strtoul(optarg, &endptr, 0);
5202 type = check_foreign_type_name(optarg);
5203 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5205 "%s %s: unknown foreign type '%s'\n",
5210 } else if (type >= UINT32_MAX) {
5212 "%s %s: invalid foreign type '%s'\n",
5213 progname, argv[0], optarg);
5217 param.fp_foreign_type = type;
5218 param.fp_check_foreign = 1;
5219 param.fp_exclude_foreign = !!neg_opt;
5222 case LFS_NEWERXY_OPT: {
5228 time_t ref = time(NULL);
5230 /* no need to check bad options, they won't get here */
5231 if (strlen(long_opts[optidx].name) == 7) {
5232 x = long_opts[optidx].name[5];
5233 y = long_opts[optidx].name[6];
5237 static const char *const fmts[] = {
5238 "%Y-%m-%d %H:%M:%S",
5241 "%H:%M:%S", /* sometime today */
5250 for (i = 0; fmts[i] != NULL; i++) {
5253 /* Init for times relative to today */
5254 if (strncmp(fmts[i], "%H", 2) == 0)
5255 localtime_r(&ref, &tm);
5257 memset(&tm, 0, sizeof(tm));
5258 ptr = strptime(optarg, fmts[i], &tm);
5260 while (ptr && isspace(*ptr))
5262 if (ptr == optarg + strlen(optarg)) {
5270 "%s: invalid time '%s'\n",
5273 "supported formats are:\n ");
5274 for (i = 0; fmts[i] != NULL; i++)
5275 fprintf(stderr, "'%s', ",
5277 fprintf(stderr, "\n");
5283 } else if (y == 'b' || y == 'B') {
5286 rc = llapi_get_lum_file(optarg, NULL, &stx,
5288 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5289 if (!(stx.stx_mask & STATX_BTIME))
5294 "%s: get btime failed '%s': %s\n",
5300 ref = stx.stx_btime.tv_sec;
5302 struct stat statbuf;
5304 if (stat(optarg, &statbuf) < 0) {
5306 "%s: cannot stat file '%s': %s\n",
5315 ref = statbuf.st_atime;
5318 ref = statbuf.st_mtime;
5321 ref = statbuf.st_ctime;
5325 "%s: invalid Y argument: '%c'\n",
5334 xidx = NEWERXY_ATIME;
5337 xidx = NEWERXY_MTIME;
5340 xidx = NEWERXY_CTIME;
5344 xidx = NEWERXY_BTIME;
5348 "%s: invalid X argument: '%c'\n",
5355 newery = ¶m.fp_newery[xidx][negidx];
5361 *newery = *newery > ref ? ref : *newery;
5363 *newery = *newery > ref ? *newery : ref;
5365 param.fp_newerxy = 1;
5370 rc = name2gid(¶m.fp_gid, optarg);
5372 if (str2quotaid(¶m.fp_gid, optarg)) {
5374 "Group/GID: %s cannot be found.\n",
5380 param.fp_exclude_gid = !!neg_opt;
5381 param.fp_check_gid = 1;
5384 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5385 ¶m.fp_hash_exflags,
5386 ¶m.fp_hash_type);
5391 if (param.fp_hash_inflags || param.fp_hash_exflags)
5392 param.fp_check_hash_flag = 1;
5393 param.fp_exclude_hash_type = !!neg_opt;
5399 ret = name2layout(¶m.fp_layout, optarg);
5402 param.fp_exclude_layout = !!neg_opt;
5403 param.fp_check_layout = 1;
5407 rc = name2uid(¶m.fp_uid, optarg);
5409 if (str2quotaid(¶m.fp_uid, optarg)) {
5411 "User/UID: %s cannot be found.\n",
5417 param.fp_exclude_uid = !!neg_opt;
5418 param.fp_check_uid = 1;
5421 param.fp_pattern = (char *)optarg;
5422 param.fp_exclude_pattern = !!neg_opt;
5425 if (optarg[0] == '+') {
5426 param.fp_mirror_count_sign = -1;
5428 } else if (optarg[0] == '-') {
5429 param.fp_mirror_count_sign = 1;
5434 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5435 if (errno != 0 || *endptr != '\0' ||
5436 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5438 "error: bad mirror count '%s'\n",
5442 param.fp_check_mirror_count = 1;
5443 param.fp_exclude_mirror_count = !!neg_opt;
5448 char *buf, *token, *next, *p;
5452 buf = strdup(optarg);
5458 param.fp_exclude_obd = !!neg_opt;
5461 while (token && *token) {
5462 token = strchr(token, ',');
5469 param.fp_exclude_mdt = !!neg_opt;
5470 param.fp_num_alloc_mdts += len;
5471 tmp = realloc(param.fp_mdt_uuid,
5472 param.fp_num_alloc_mdts *
5473 sizeof(*param.fp_mdt_uuid));
5479 param.fp_mdt_uuid = tmp;
5481 param.fp_exclude_obd = !!neg_opt;
5482 param.fp_num_alloc_obds += len;
5483 tmp = realloc(param.fp_obd_uuid,
5484 param.fp_num_alloc_obds *
5485 sizeof(*param.fp_obd_uuid));
5491 param.fp_obd_uuid = tmp;
5493 for (token = buf; token && *token; token = next) {
5494 struct obd_uuid *puuid;
5498 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5501 ¶m.fp_obd_uuid[param.fp_num_obds++];
5503 p = strchr(token, ',');
5510 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5515 strncpy(puuid->uuid, token,
5516 sizeof(puuid->uuid));
5523 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5527 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5529 "Pool name %s is too long (max %d)\n",
5530 optarg, LOV_MAXPOOLNAME);
5535 * We do check for empty pool because empty pool
5536 * is used to find V1 LOV attributes
5538 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5539 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5540 param.fp_exclude_pool = !!neg_opt;
5541 param.fp_check_pool = 1;
5543 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5544 case 'p': /* want this for --pool, to match getstripe/find */
5546 "warning: -p deprecated, use --print0 or -0\n");
5549 param.fp_zero_end = 1;
5551 case 'P': /* we always print, this option is a no-op */
5553 case LFS_PRINTF_OPT:
5554 param.fp_format_printf_str = optarg;
5556 case LFS_PROJID_OPT:
5557 rc = name2projid(¶m.fp_projid, optarg);
5559 if (str2quotaid(¶m.fp_projid, optarg)) {
5561 "Invalid project ID: %s\n",
5567 param.fp_exclude_projid = !!neg_opt;
5568 param.fp_check_projid = 1;
5571 if (optarg[0] == '+') {
5572 param.fp_size_sign = -1;
5574 } else if (optarg[0] == '-') {
5575 param.fp_size_sign = 1;
5579 ret = llapi_parse_size(optarg, ¶m.fp_size,
5580 ¶m.fp_size_units, 0);
5582 fprintf(stderr, "error: bad file size '%s'\n",
5586 param.fp_check_size = 1;
5587 param.fp_exclude_size = !!neg_opt;
5590 if (optarg[0] == '+') {
5591 param.fp_stripe_size_sign = -1;
5593 } else if (optarg[0] == '-') {
5594 param.fp_stripe_size_sign = 1;
5598 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5599 ¶m.fp_stripe_size_units, 0);
5600 /* assume units of KB if too small to be valid */
5601 if (param.fp_stripe_size < 4096)
5602 param.fp_stripe_size *= 1024;
5604 fprintf(stderr, "error: bad stripe_size '%s'\n",
5608 param.fp_check_stripe_size = 1;
5609 param.fp_exclude_stripe_size = !!neg_opt;
5612 param.fp_exclude_type = !!neg_opt;
5613 switch (optarg[0]) {
5615 param.fp_type = S_IFBLK;
5618 param.fp_type = S_IFCHR;
5621 param.fp_type = S_IFDIR;
5624 param.fp_type = S_IFREG;
5627 param.fp_type = S_IFLNK;
5630 param.fp_type = S_IFIFO;
5633 param.fp_type = S_IFSOCK;
5636 fprintf(stderr, "%s: bad type '%s'\n",
5643 param.fp_exclude_perm = !!neg_opt;
5644 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5645 if (*optarg == '/') {
5646 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5648 } else if (*optarg == '-') {
5649 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5653 if (str2mode_t(optarg, ¶m.fp_perm)) {
5654 fprintf(stderr, "error: invalid mode '%s'\n",
5661 if (optarg[0] == '+') {
5662 param.fp_mdt_count_sign = -1;
5664 } else if (optarg[0] == '-') {
5665 param.fp_mdt_count_sign = 1;
5670 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5671 if (errno != 0 || *endptr != '\0' ||
5672 param.fp_mdt_count >= UINT32_MAX) {
5673 fprintf(stderr, "error: bad mdt_count '%s'\n",
5678 param.fp_check_mdt_count = 1;
5679 param.fp_exclude_mdt_count = !!neg_opt;
5682 if (optarg[0] == '+') {
5683 param.fp_ext_size_sign = -1;
5685 } else if (optarg[0] == '-') {
5686 param.fp_ext_size_sign = 1;
5690 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5691 ¶m.fp_ext_size_units, 0);
5693 fprintf(stderr, "error: bad ext-size '%s'\n",
5697 param.fp_ext_size /= SEL_UNIT_SIZE;
5698 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5699 param.fp_check_ext_size = 1;
5700 param.fp_exclude_ext_size = !!neg_opt;
5703 fprintf(stderr, "%s: unrecognized option '%s'\n",
5704 progname, argv[optind - 1]);
5710 if (!param.fp_verbose)
5711 param.fp_verbose = VERBOSE_DEFAULT;
5713 if (pathstart == -1) {
5714 fprintf(stderr, "error: %s: no filename|pathname\n",
5718 } else if (pathend == -1) {
5724 rc = llapi_find(argv[pathstart], ¶m);
5727 pathbad = pathstart;
5729 } while (++pathstart < pathend);
5732 fprintf(stderr, "%s: failed for '%s': %s\n",
5733 progname, argv[pathbad], strerror(-rc));
5736 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5737 free(param.fp_obd_uuid);
5739 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5740 free(param.fp_mdt_uuid);
5745 static int lfs_getstripe_internal(int argc, char **argv,
5746 struct find_param *param)
5748 struct option long_opts[] = {
5749 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5750 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5751 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5752 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5753 { .val = LFS_COMP_COUNT_OPT,
5754 .name = "comp-count", .has_arg = no_argument },
5755 { .val = LFS_COMP_COUNT_OPT,
5756 .name = "component-count", .has_arg = no_argument },
5757 { .val = LFS_COMP_FLAGS_OPT,
5758 .name = "comp-flags", .has_arg = optional_argument },
5759 { .val = LFS_COMP_FLAGS_OPT,
5760 .name = "component-flags", .has_arg = optional_argument },
5761 { .val = LFS_COMP_START_OPT,
5762 .name = "comp-start", .has_arg = optional_argument },
5763 { .val = LFS_COMP_START_OPT,
5764 .name = "component-start", .has_arg = optional_argument },
5765 { .val = LFS_MIRROR_INDEX_OPT,
5766 .name = "mirror-index", .has_arg = required_argument },
5767 { .val = LFS_MIRROR_ID_OPT,
5768 .name = "mirror-id", .has_arg = required_argument },
5769 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5770 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5771 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5772 { .val = 'd', .name = "directory", .has_arg = no_argument },
5773 { .val = 'D', .name = "default", .has_arg = no_argument },
5774 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5775 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5776 { .val = 'F', .name = "fid", .has_arg = no_argument },
5777 { .val = 'g', .name = "generation", .has_arg = no_argument },
5778 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5779 { .val = 'h', .name = "help", .has_arg = no_argument },
5780 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5781 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5782 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5783 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5784 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5785 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5786 { .val = 'L', .name = "layout", .has_arg = no_argument },
5787 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5788 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5789 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5790 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5791 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5792 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5793 { .val = 'O', .name = "obd", .has_arg = required_argument },
5794 { .val = 'O', .name = "ost", .has_arg = required_argument },
5795 { .val = 'p', .name = "pool", .has_arg = no_argument },
5796 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5797 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5798 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5799 { .val = 'R', .name = "raw", .has_arg = no_argument },
5800 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5801 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5802 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5803 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5804 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5805 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5806 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5807 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5808 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5809 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5810 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5814 int pathstart = -1, pathend = -1;
5818 while ((c = getopt_long(argc, argv,
5819 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5820 long_opts, NULL)) != -1) {
5824 /* '!' is part of option */
5825 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5826 if (!isoption && pathend != -1) {
5828 "error: %s: filename|dirname must either precede options or follow options\n",
5832 if (!isoption && pathstart == -1)
5833 pathstart = optind - 1;
5834 if (isoption && pathstart != -1 && pathend == -1)
5835 pathend = optind - 2;
5839 /* unknown: opt is "!" */
5840 if (strcmp(optarg, "!") == 0)
5844 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5845 param->fp_verbose |= VERBOSE_COUNT;
5846 param->fp_max_depth = 0;
5849 case LFS_COMP_COUNT_OPT:
5850 param->fp_verbose |= VERBOSE_COMP_COUNT;
5851 param->fp_max_depth = 0;
5853 case LFS_COMP_FLAGS_OPT:
5855 rc = comp_str2flags(optarg,
5856 ¶m->fp_comp_flags,
5857 ¶m->fp_comp_neg_flags);
5860 "error: %s bad component flags '%s'.\n",
5864 param->fp_check_comp_flags = 1;
5866 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5867 param->fp_max_depth = 0;
5870 case LFS_COMP_START_OPT:
5873 if (tmp[0] == '+') {
5874 param->fp_comp_start_sign = -1;
5876 } else if (tmp[0] == '-') {
5877 param->fp_comp_start_sign = 1;
5880 rc = llapi_parse_size(tmp,
5881 ¶m->fp_comp_start,
5882 ¶m->fp_comp_start_units, 0);
5885 "error: %s bad component start '%s'.\n",
5889 param->fp_check_comp_start = 1;
5891 param->fp_verbose |= VERBOSE_COMP_START;
5892 param->fp_max_depth = 0;
5895 case LFS_MIRROR_INDEX_OPT: {
5896 unsigned long int mirror_index;
5898 if (optarg[0] == '+') {
5899 param->fp_mirror_index_sign = -1;
5901 } else if (optarg[0] == '-') {
5902 param->fp_mirror_index_sign = 1;
5907 mirror_index = strtoul(optarg, &end, 0);
5908 if (errno != 0 || *end != '\0' ||
5909 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5910 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5912 "%s %s: invalid mirror index '%s'\n",
5913 progname, argv[0], optarg);
5917 param->fp_mirror_index = (__u16)mirror_index;
5919 if (param->fp_mirror_id != 0) {
5921 "%s %s: can't specify both mirror index and mirror ID\n",
5925 param->fp_check_mirror_index = 1;
5926 param->fp_exclude_mirror_index = !!neg_opt;
5929 case LFS_MIRROR_ID_OPT: {
5930 unsigned long int mirror_id;
5932 if (optarg[0] == '+') {
5933 param->fp_mirror_id_sign = -1;
5935 } else if (optarg[0] == '-') {
5936 param->fp_mirror_id_sign = 1;
5941 mirror_id = strtoul(optarg, &end, 0);
5942 if (errno != 0 || *end != '\0' ||
5943 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5944 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5946 "%s %s: invalid mirror ID '%s'\n",
5947 progname, argv[0], optarg);
5951 param->fp_mirror_id = (__u16)mirror_id;
5953 if (param->fp_mirror_index != 0) {
5955 "%s %s: can't specify both mirror index and mirror ID\n",
5959 param->fp_check_mirror_id = 1;
5960 param->fp_exclude_mirror_id = !!neg_opt;
5964 param->fp_max_depth = 0;
5967 param->fp_get_default_lmv = 1;
5972 if (tmp[0] == '+') {
5973 param->fp_comp_end_sign = -1;
5975 } else if (tmp[0] == '-') {
5976 param->fp_comp_end_sign = 1;
5980 if (arg_is_eof(tmp)) {
5981 param->fp_comp_end = LUSTRE_EOF;
5982 param->fp_comp_end_units = 1;
5985 rc = llapi_parse_size(tmp,
5986 ¶m->fp_comp_end,
5987 ¶m->fp_comp_end_units, 0);
5988 /* assume units of KB if too small */
5989 if (param->fp_comp_end < 4096)
5990 param->fp_comp_end *= 1024;
5994 "error: %s bad component end '%s'.\n",
5998 param->fp_check_comp_end = 1;
6000 param->fp_verbose |= VERBOSE_COMP_END;
6001 param->fp_max_depth = 0;
6005 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6006 param->fp_verbose |= VERBOSE_DFID;
6007 param->fp_max_depth = 0;
6011 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6012 param->fp_verbose |= VERBOSE_GENERATION;
6013 param->fp_max_depth = 0;
6017 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6018 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6019 param->fp_max_depth = 0;
6024 param->fp_comp_id = strtoul(optarg, &end, 0);
6025 if (*end != '\0' || param->fp_comp_id == 0 ||
6026 param->fp_comp_id > LCME_ID_MAX) {
6028 "error: %s bad component id '%s'\n",
6032 param->fp_check_comp_id = 1;
6034 param->fp_max_depth = 0;
6035 param->fp_verbose |= VERBOSE_COMP_ID;
6039 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6040 param->fp_verbose |= VERBOSE_PATTERN;
6041 param->fp_max_depth = 0;
6044 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6047 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6050 if (!(param->fp_verbose & VERBOSE_DETAIL))
6051 param->fp_max_depth = 0;
6052 param->fp_verbose |= VERBOSE_MDTINDEX;
6055 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6056 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6057 param->fp_max_depth = 0;
6061 if (param->fp_obd_uuid) {
6063 "error: %s: only one obduuid allowed",
6067 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6070 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6071 param->fp_verbose |= VERBOSE_POOL;
6072 param->fp_max_depth = 0;
6079 param->fp_recursive = 1;
6085 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6086 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6087 param->fp_max_depth = 0;
6091 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6097 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6098 param->fp_verbose |= VERBOSE_EXT_SIZE;
6099 param->fp_max_depth = 0;
6103 fprintf(stderr, "%s: unrecognized option '%s'\n",
6104 progname, argv[optind - 1]);
6110 if (pathstart == -1) {
6111 fprintf(stderr, "error: %s: no filename|pathname\n",
6114 } else if (pathend == -1) {
6122 if (param->fp_recursive)
6123 param->fp_max_depth = -1;
6124 else if (param->fp_verbose & VERBOSE_DETAIL)
6125 param->fp_max_depth = 1;
6127 if (!param->fp_verbose)
6128 param->fp_verbose = VERBOSE_DEFAULT;
6129 if (param->fp_quiet)
6130 param->fp_verbose = VERBOSE_OBJID;
6133 rc = llapi_getstripe(argv[pathstart], param);
6134 } while (++pathstart < pathend && !rc);
6137 fprintf(stderr, "error: %s failed for %s.\n",
6138 argv[0], argv[optind - 1]);
6142 static int lfs_tgts(int argc, char **argv)
6144 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6145 struct find_param param;
6146 int index = 0, rc = 0;
6151 if (argc == 2 && !realpath(argv[1], path)) {
6153 fprintf(stderr, "error: invalid path '%s': %s\n",
6154 argv[1], strerror(-rc));
6158 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6159 /* Check if we have a mount point */
6160 if (mntdir[0] == '\0')
6163 memset(¶m, 0, sizeof(param));
6164 if (!strcmp(argv[0], "mdts"))
6165 param.fp_get_lmv = 1;
6167 rc = llapi_ostlist(mntdir, ¶m);
6169 fprintf(stderr, "error: %s: failed on %s\n",
6172 if (path[0] != '\0')
6174 memset(mntdir, 0, PATH_MAX);
6180 static int lfs_getstripe(int argc, char **argv)
6182 struct find_param param = { 0 };
6184 param.fp_max_depth = 1;
6185 return lfs_getstripe_internal(argc, argv, ¶m);
6189 static int lfs_getdirstripe(int argc, char **argv)
6191 struct find_param param = { 0 };
6192 struct option long_opts[] = {
6193 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6194 { .val = 'D', .name = "default", .has_arg = no_argument },
6195 { .val = 'h', .name = "help", .has_arg = no_argument },
6196 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6197 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6198 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6199 { .val = 'O', .name = "obd", .has_arg = required_argument },
6200 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6201 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6202 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6203 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6204 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6205 { .val = LFS_INHERIT_RR_OPT,
6206 .name = "max-inherit-rr", .has_arg = no_argument },
6210 param.fp_get_lmv = 1;
6212 while ((c = getopt_long(argc, argv,
6213 "cDhHimO:rtTvXy", long_opts, NULL)) != -1) {
6217 param.fp_verbose |= VERBOSE_COUNT;
6220 param.fp_get_default_lmv = 1;
6222 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6225 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6229 param.fp_verbose |= VERBOSE_HASH_TYPE;
6234 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6237 if (param.fp_obd_uuid) {
6239 "%s: only one obduuid allowed",
6243 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6246 param.fp_recursive = 1;
6249 param.fp_verbose |= VERBOSE_DETAIL;
6252 param.fp_verbose |= VERBOSE_INHERIT;
6254 case LFS_INHERIT_RR_OPT:
6255 param.fp_verbose |= VERBOSE_INHERIT_RR;
6261 fprintf(stderr, "%s: unrecognized option '%s'\n",
6262 progname, argv[optind - 1]);
6272 if (param.fp_recursive)
6273 param.fp_max_depth = -1;
6275 if (!param.fp_verbose)
6276 param.fp_verbose = VERBOSE_DEFAULT;
6279 rc = llapi_getstripe(argv[optind], ¶m);
6280 } while (++optind < argc && !rc);
6283 fprintf(stderr, "error: %s failed for %s.\n",
6284 argv[0], argv[optind - 1]);
6289 MNTDF_INODES = 0x0001,
6290 MNTDF_COOKED = 0x0002,
6291 MNTDF_LAZY = 0x0004,
6292 MNTDF_VERBOSE = 0x0008,
6293 MNTDF_SHOW = 0x0010,
6294 MNTDF_DECIMAL = 0x0020,
6297 #define COOK(value, base) \
6300 while (value > base) { \
6308 #define CDF "%11llu"
6309 #define HDF "%8.1f%c"
6313 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6315 double avail, used, ratio = 0;
6318 avail = st->os_ffree;
6319 used = st->os_files - st->os_ffree;
6321 avail = st->os_bavail;
6322 used = st->os_blocks - st->os_bfree;
6324 if (avail + used > 0)
6325 ratio = used / (used + avail) * 100;
6327 /* Round up to match df(1) usage percentage */
6328 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6332 * This is to identify various problem states for "lfs df" if .osn_err = true,
6333 * so only show flags reflecting those states by default. Informational states
6334 * are only shown with "-v" and use lower-case names to distinguish them.
6335 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6337 static struct obd_statfs_state_names {
6338 enum obd_statfs_state osn_state;
6339 const char osn_name;
6342 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6343 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6344 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6345 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6346 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6347 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6348 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6349 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6350 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6353 static int showdf(char *mntdir, struct obd_statfs *stat,
6354 char *uuid, enum mntdf_flags flags,
6355 char *type, int index, int rc)
6357 long long avail, used, total;
6359 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6360 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6361 char tbuf[3 * sizeof(__u64)];
6362 char ubuf[3 * sizeof(__u64)];
6363 char abuf[3 * sizeof(__u64)];
6364 char rbuf[3 * sizeof(__u64)];
6371 if (flags & MNTDF_INODES) {
6372 avail = stat->os_ffree;
6373 used = stat->os_files - stat->os_ffree;
6374 total = stat->os_files;
6376 int shift = flags & MNTDF_COOKED ? 0 : 10;
6378 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6379 used = ((stat->os_blocks - stat->os_bfree) *
6380 stat->os_bsize) >> shift;
6381 total = (stat->os_blocks * stat->os_bsize) >> shift;
6384 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6386 if (flags & MNTDF_COOKED) {
6387 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6391 cook_val = (double)total;
6392 i = COOK(cook_val, base);
6394 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6397 snprintf(tbuf, sizeof(tbuf), CDF, total);
6399 cook_val = (double)used;
6400 i = COOK(cook_val, base);
6402 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6405 snprintf(ubuf, sizeof(ubuf), CDF, used);
6407 cook_val = (double)avail;
6408 i = COOK(cook_val, base);
6410 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6413 snprintf(abuf, sizeof(abuf), CDF, avail);
6415 snprintf(tbuf, sizeof(tbuf), CDF, total);
6416 snprintf(ubuf, sizeof(tbuf), CDF, used);
6417 snprintf(abuf, sizeof(tbuf), CDF, avail);
6420 sprintf(rbuf, RDF, ratio);
6421 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6422 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6424 printf("[%s:%d]", type, index);
6426 if (stat->os_state) {
6430 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6431 if (oss_names[i].osn_state & stat->os_state &&
6432 (oss_names[i].osn_err ||
6433 flags & MNTDF_VERBOSE))
6434 printf("%c", oss_names[i].osn_name);
6441 printf(UUF": inactive device\n", uuid);
6444 printf(UUF": %s\n", uuid, strerror(-rc));
6451 struct ll_stat_type {
6456 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6458 struct ll_statfs_data {
6460 struct obd_statfs sd_st;
6463 struct ll_statfs_buf {
6465 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6468 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6469 int ops, struct ll_statfs_buf *lsb)
6471 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6472 struct obd_uuid uuid_buf;
6473 char *poolname = NULL;
6474 struct ll_stat_type types[] = {
6475 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6476 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6477 { .st_name = NULL } };
6478 struct ll_stat_type *tp;
6479 __u64 ost_files = 0;
6480 __u64 ost_ffree = 0;
6488 poolname = strchr(pool, '.');
6490 if (strncmp(fsname, pool, strlen(fsname))) {
6491 fprintf(stderr, "filesystem name incorrect\n");
6499 fd = open(mntdir, O_RDONLY);
6502 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6507 if (flags & MNTDF_SHOW) {
6508 if (flags & MNTDF_INODES)
6509 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6510 "UUID", "Inodes", "IUsed", "IFree",
6511 "IUse%", "Mounted on");
6513 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6515 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6516 "Used", "Available", "Use%", "Mounted on");
6519 for (tp = types; tp->st_name != NULL; tp++) {
6520 bool have_ost = false;
6522 if (!(tp->st_op & ops))
6525 for (index = 0; ; index++) {
6526 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6527 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6528 type = flags & MNTDF_LAZY ?
6529 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6530 rc2 = llapi_obd_fstatfs(fd, type, index,
6531 &stat_buf, &uuid_buf);
6536 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6537 if (!(flags & MNTDF_VERBOSE))
6539 } else if (rc2 < 0 && rc == 0) {
6544 * If we have OSTs then don't report MDT block counts.
6545 * For MDT-only filesystems the expectation is that all
6546 * layouts have a DoM component. For filesystems with
6547 * OSTs, files are not necessarily going to store data
6548 * on MDTs, and MDT space is limited to a fraction of
6549 * OST space, so don't include it in the summary.
6551 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6558 if (poolname && tp->st_op == LL_STATFS_LOV &&
6559 llapi_search_ost(fsname, poolname,
6560 obd_uuid2str(&uuid_buf)) != 1)
6564 * the llapi_obd_fstatfs() call may have returned with
6565 * an error, but if it filled in uuid_buf we will at
6566 * lease use that to print out a message for that OBD.
6567 * If we didn't get anything in the uuid_buf, then fill
6568 * it in so that we can print an error message.
6570 if (uuid_buf.uuid[0] == '\0')
6571 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6572 "%s%04x", tp->st_name, index);
6574 lsb->sb_buf[lsb->sb_count].sd_index = index;
6575 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6578 if (flags & MNTDF_SHOW)
6579 showdf(mntdir, &stat_buf,
6580 obd_uuid2str(&uuid_buf), flags,
6581 tp->st_name, index, rc2);
6586 if (tp->st_op == LL_STATFS_LMV) {
6587 sum.os_ffree += stat_buf.os_ffree;
6588 sum.os_files += stat_buf.os_files;
6589 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6590 ost_files += stat_buf.os_files;
6591 ost_ffree += stat_buf.os_ffree;
6593 sum.os_blocks += stat_buf.os_blocks *
6595 sum.os_bfree += stat_buf.os_bfree *
6597 sum.os_bavail += stat_buf.os_bavail *
6605 * If we have _some_ OSTs, but don't have as many free objects on the
6606 * OST as inodes on the MDTs, reduce the reported number of inodes
6607 * to compensate, so that the "inodes in use" number is correct.
6608 * This should be kept in sync with ll_statfs_internal().
6610 if (ost_files && ost_ffree < sum.os_ffree) {
6611 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6612 sum.os_ffree = ost_ffree;
6614 if (flags & MNTDF_SHOW) {
6616 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6624 LAYOUT_INHERIT_UNSET = -2,
6628 static int lfs_setdirstripe(int argc, char **argv)
6631 struct lfs_setstripe_args lsa = { 0 };
6632 struct llapi_stripe_param *param = NULL;
6633 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6636 char *mode_opt = NULL;
6637 bool default_stripe = false;
6638 bool delete = false;
6639 bool foreign_mode = false;
6640 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6641 mode_t previous_mode = 0;
6643 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6644 int max_inherit = LAYOUT_INHERIT_UNSET;
6645 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6646 struct option long_opts[] = {
6647 { .val = 'c', .name = "count", .has_arg = required_argument },
6648 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6649 { .val = 'd', .name = "delete", .has_arg = no_argument },
6650 { .val = 'D', .name = "default", .has_arg = no_argument },
6651 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6652 { .val = LFS_LAYOUT_FLAGS_OPT,
6653 .name = "flags", .has_arg = required_argument },
6654 { .val = LFS_LAYOUT_FOREIGN_OPT,
6655 .name = "foreign", .has_arg = optional_argument},
6656 { .val = 'h', .name = "help", .has_arg = no_argument },
6657 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6658 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6659 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6660 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6662 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6663 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6664 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6666 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6667 { .val = 'i', .name = "index", .has_arg = required_argument },
6669 { .val = 'o', .name = "mode", .has_arg = required_argument },
6670 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6671 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6673 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6674 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6675 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
6676 { .val = LFS_INHERIT_RR_OPT,
6677 .name = "max-inherit-rr", .has_arg = required_argument},
6678 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6682 setstripe_args_init(&lsa);
6684 while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
6685 long_opts, NULL)) >= 0) {
6693 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6694 if (errno != 0 || *end != '\0' ||
6695 lsa.lsa_stripe_count < -1 ||
6696 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6698 "%s: invalid stripe count '%s'\n",
6705 default_stripe = true;
6708 default_stripe = true;
6710 case LFS_LAYOUT_FOREIGN_OPT:
6712 /* check pure numeric */
6713 type = strtoul(optarg, &end, 0);
6716 type = check_foreign_type_name(optarg);
6717 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6719 "%s %s: unknown foreign type '%s'\n",
6724 } else if (type >= UINT32_MAX) {
6726 "%s %s: invalid foreign type '%s'\n",
6727 progname, argv[0], optarg);
6731 foreign_mode = true;
6733 case LFS_LAYOUT_FLAGS_OPT:
6735 flags = strtoul(optarg, &end, 16);
6736 if (errno != 0 || *end != '\0' ||
6737 flags >= UINT32_MAX) {
6739 "%s %s: invalid hex flags '%s'\n",
6740 progname, argv[0], optarg);
6743 if (!foreign_mode) {
6745 "%s %s: hex flags must be specified with --foreign option\n",
6750 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6753 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6757 lsa.lsa_pattern = check_hashtype(optarg);
6758 if (lsa.lsa_pattern == 0) {
6760 "%s %s: bad stripe hash type '%s'\n",
6761 progname, argv[0], optarg);
6766 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6769 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6770 if (strcmp(argv[optind - 1], "--index") == 0)
6772 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6775 lsa.lsa_nr_tgts = parse_targets(mdts,
6776 sizeof(mdts) / sizeof(__u32),
6777 lsa.lsa_nr_tgts, optarg, NULL);
6778 if (lsa.lsa_nr_tgts < 0) {
6780 "%s %s: invalid MDT target(s) '%s'\n",
6781 progname, argv[0], optarg);
6785 lsa.lsa_tgts = mdts;
6786 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6787 lsa.lsa_stripe_off = mdts[0];
6789 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6792 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6802 max_inherit = strtol(optarg, &end, 10);
6803 if (errno != 0 || *end != '\0' || max_inherit < -2) {
6805 "%s %s: invalid max-inherit '%s'\n",
6806 progname, argv[0], optarg);
6809 if (max_inherit == 0) {
6810 max_inherit = LMV_INHERIT_NONE;
6811 } else if (max_inherit == -1) {
6812 max_inherit = LMV_INHERIT_UNLIMITED;
6813 } else if (max_inherit > LMV_INHERIT_MAX) {
6815 "%s %s: max-inherit %d exceeds maximum %u\n",
6816 progname, argv[0], max_inherit,
6821 case LFS_INHERIT_RR_OPT:
6822 if (!default_stripe) {
6824 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6829 max_inherit_rr = strtol(optarg, &end, 10);
6830 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6832 "%s %s: invalid max-inherit-rr '%s'\n",
6833 progname, argv[0], optarg);
6836 if (max_inherit_rr == 0) {
6837 max_inherit_rr = LMV_INHERIT_RR_NONE;
6838 } else if (max_inherit_rr == -1) {
6839 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6840 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6842 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6843 progname, argv[0], max_inherit_rr,
6844 LMV_INHERIT_RR_MAX);
6849 fprintf(stderr, "%s: unrecognized option '%s'\n",
6850 progname, argv[optind - 1]);
6857 if (optind == argc) {
6858 fprintf(stderr, "%s %s: DIR must be specified\n",
6863 if (xattr && !foreign_mode) {
6865 * only print a warning as this is armless and will be
6869 "%s %s: xattr has been specified for non-foreign layout\n",
6871 } else if (foreign_mode && !xattr) {
6873 "%s %s: xattr must be provided in foreign mode\n",
6878 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6879 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6881 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6886 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6887 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6888 /* if no parameters set, create directory on least-used MDTs */
6889 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
6890 lsa.lsa_stripe_count = 1;
6894 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6895 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6897 "%s %s: cannot specify -d with -c or -i options\n",
6903 mode = strtoul(mode_opt, &end, 8);
6906 "%s %s: bad MODE '%s'\n",
6907 progname, argv[0], mode_opt);
6910 previous_mode = umask(0);
6913 /* check max-inherit and warn user in some cases */
6914 if (default_stripe &&
6915 (lsa.lsa_stripe_count < 0 || lsa.lsa_stripe_count > 1)) {
6916 if (max_inherit == LMV_INHERIT_UNLIMITED)
6918 "%s %s: unrecommended max-inherit=-1 when default stripe-count=%lld\n",
6919 progname, argv[0], lsa.lsa_stripe_count);
6920 else if (max_inherit > LMV_INHERIT_DEFAULT_STRIPED + 2 &&
6921 max_inherit != LMV_INHERIT_NONE)
6923 "%s %s: unrecommended max-inherit=%d when default stripe-count=%lld\n",
6924 progname, argv[0], max_inherit,
6925 lsa.lsa_stripe_count);
6928 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6929 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6930 lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
6932 "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
6933 progname, argv[0], lsa.lsa_stripe_off);
6937 /* foreign LMV/dir case */
6939 if (argc > optind + 1) {
6941 "%s %s: cannot specify multiple foreign dirs\n",
6946 dname = argv[optind];
6947 result = llapi_dir_create_foreign(dname, mode, type, flags,
6951 "%s mkdir: can't create foreign dir '%s': %s\n",
6952 progname, dname, strerror(-result));
6957 * initialize stripe parameters, in case param is converted to specific,
6958 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6960 param = calloc(1, offsetof(typeof(*param),
6961 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6962 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6965 "%s %s: cannot allocate memory for parameters: %s\n",
6966 progname, argv[0], strerror(ENOMEM));
6970 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6971 if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
6972 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6973 lsa.lsa_stripe_count == 0))
6974 lsa.lsa_stripe_count = 1;
6975 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6976 param->lsp_stripe_count = lsa.lsa_stripe_count;
6977 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6978 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
6980 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6981 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6982 param->lsp_stripe_pattern = lsa.lsa_pattern;
6984 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6985 param->lsp_pool = lsa.lsa_pool_name;
6986 param->lsp_is_specific = false;
6987 if (max_inherit == LAYOUT_INHERIT_UNSET) {
6988 if (lsa.lsa_stripe_count == 0 || lsa.lsa_stripe_count == 1)
6989 max_inherit = LMV_INHERIT_DEFAULT_PLAIN;
6991 max_inherit = LMV_INHERIT_DEFAULT_STRIPED;
6993 param->lsp_max_inherit = max_inherit;
6994 if (default_stripe) {
6996 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
6997 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
6998 param->lsp_max_inherit_rr = max_inherit_rr;
7000 if (strcmp(argv[0], "mkdir") == 0)
7001 param->lsp_is_create = true;
7002 if (lsa.lsa_nr_tgts > 1) {
7003 if (lsa.lsa_stripe_count > 0 &&
7004 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
7005 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
7007 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
7008 argv[0], lsa.lsa_stripe_count,
7014 param->lsp_is_specific = true;
7015 param->lsp_stripe_count = lsa.lsa_nr_tgts;
7016 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
7019 dname = argv[optind];
7021 if (default_stripe) {
7022 result = llapi_dir_set_default_lmv(dname, param);
7025 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7026 progname, dname, strerror(-result));
7030 result = llapi_dir_create(dname, mode, param);
7033 "%s setdirstripe: cannot create dir '%s': %s\n",
7034 progname, dname, strerror(-result));
7035 } while (!result && (dname = argv[++optind]));
7038 umask(previous_mode);
7045 static int lfs_rmentry(int argc, char **argv)
7052 fprintf(stderr, "error: %s: missing dirname\n",
7058 dname = argv[index];
7060 result = llapi_direntry_remove(dname);
7063 "error: %s: remove dir entry '%s' failed\n",
7067 dname = argv[++index];
7072 static int lfs_unlink_foreign(int argc, char **argv)
7079 fprintf(stderr, "error: %s: missing pathname\n",
7086 while (name != NULL) {
7087 result = llapi_unlink_foreign(name);
7090 "error: %s: unlink foreign entry '%s' failed\n",
7094 name = argv[++index];
7099 static int lfs_mv(int argc, char **argv)
7101 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7102 struct find_param param = {
7109 struct option long_opts[] = {
7110 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7111 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7112 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7115 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7117 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7120 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7124 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7125 if (errno != 0 || *end != '\0' ||
7126 lmu.lum_stripe_offset >= UINT32_MAX) {
7127 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7133 param.fp_verbose = VERBOSE_DETAIL;
7136 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7137 progname, argv[optind - 1]);
7142 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7143 fprintf(stderr, "%s mv: MDT index must be specified\n",
7148 if (optind >= argc) {
7149 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7153 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7155 /* initialize migrate mdt parameters */
7156 param.fp_lmv_md = &lmu;
7157 param.fp_migrate = 1;
7158 rc = llapi_migrate_mdt(argv[optind], ¶m);
7160 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7161 progname, argv[optind], lmu.lum_stripe_offset,
7166 static int lfs_osts(int argc, char **argv)
7168 return lfs_tgts(argc, argv);
7171 static int lfs_mdts(int argc, char **argv)
7173 return lfs_tgts(argc, argv);
7176 static int lfs_df(int argc, char **argv)
7178 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7179 enum mntdf_flags flags = MNTDF_SHOW;
7180 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7181 int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7182 char fsname[PATH_MAX] = "", *pool_name = NULL;
7183 struct option long_opts[] = {
7184 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7185 { .val = 'H', .name = "si", .has_arg = no_argument },
7186 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7187 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7188 { .val = 'p', .name = "pool", .has_arg = required_argument },
7189 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7192 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7195 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7198 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7201 flags |= MNTDF_INODES;
7204 flags |= MNTDF_LAZY;
7210 flags |= MNTDF_VERBOSE;
7213 fprintf(stderr, "%s: unrecognized option '%s'\n",
7214 progname, argv[optind - 1]);
7219 /* Handle case where path is not specified */
7220 if (optind == argc) {
7221 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7222 /* Check if we have a mount point */
7223 if (mntdir[0] == '\0')
7226 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7227 if (rc || path[0] != '\0')
7230 fsname[0] = '\0'; /* avoid matching in next loop */
7231 mntdir[0] = '\0'; /* avoid matching in next loop */
7232 path[0] = '\0'; /* clean for next loop */
7237 /* Loop through all the remaining arguments. These are Lustre FS
7240 for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7243 fsname[0] = '\0'; /* start clean */
7244 mntdir[0] = '\0'; /* start clean */
7245 path[0] = '\0'; /* start clean */
7247 /* path does not exists at all */
7248 if (!realpath(argv[arg_idx], path)) {
7250 fprintf(stderr, "error: invalid path '%s': %s\n",
7251 argv[arg_idx], strerror(-rc));
7252 /* save first seen error */
7259 /* path exists but may not be a Lustre filesystem */
7260 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7261 /* Check if we have a mount point */
7262 if (mntdir[0] == '\0')
7265 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7266 if (rc || path[0] != '\0') {
7269 /* save first seen error */
7277 llapi_printf(LLAPI_MSG_ERROR,
7278 "%s:%s Not a Lustre filesystem\n",
7279 argv[0], argv[arg_idx]);
7280 /* save first seen error */
7289 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7290 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7294 if (opt_fsname == opt_instance) { /* both true or both false */
7295 rc = llapi_getname(mntdir, buf, buflen);
7296 } else if (opt_fsname) {
7298 * llapi_search_mounts() fills @buf with fsname, but that is not
7299 * called if explicit paths are specified on the command-line
7302 rc = llapi_get_fsname(mntdir, buf, buflen);
7303 } else /* if (opt_instance) */ {
7304 rc = llapi_get_instance(mntdir, buf, buflen);
7308 fprintf(stderr, "cannot get instance for '%s': %s\n",
7309 mntdir, strerror(-rc));
7314 printf("%s %s\n", buf, mntdir);
7316 printf("%s\n", buf);
7321 static int lfs_getname(int argc, char **argv)
7323 struct option long_opts[] = {
7324 { .val = 'h', .name = "help", .has_arg = no_argument },
7325 { .val = 'i', .name = "instance", .has_arg = no_argument },
7326 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7328 bool opt_instance = false, opt_fsname = false;
7329 char fsname[PATH_MAX] = "";
7332 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7335 opt_instance = true;
7341 fprintf(stderr, "%s: unrecognized option '%s'\n",
7342 progname, argv[optind - 1]);
7349 if (optind == argc) { /* no paths specified, get all paths. */
7350 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7353 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7354 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7355 opt_instance, opt_fsname, true);
7358 path[0] = fsname[0] = mntdir[0] = '\0';
7360 } else { /* paths specified, only attempt to search these. */
7363 /* if only one path is given, print only requested info */
7364 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7366 for (; optind < argc; optind++) {
7367 rc2 = print_instance(argv[optind], fsname,
7368 sizeof(fsname), opt_instance,
7369 opt_fsname, opt_mntdir);
7379 static int lfs_check(int argc, char **argv)
7381 char mntdir[PATH_MAX] = {'\0'};
7390 fprintf(stderr, "%s check: server type must be specified\n",
7395 obd_types[0] = obd_type1;
7396 obd_types[1] = obd_type2;
7397 obd_types[2] = obd_type3;
7399 if (strcmp(argv[1], "osts") == 0) {
7400 strcpy(obd_types[0], "osc");
7401 } else if (strcmp(argv[1], "mdts") == 0 ||
7402 strcmp(argv[1], "mds") == 0) {
7403 strcpy(obd_types[0], "mdc");
7404 } else if (strcmp(argv[1], "mgts") == 0) {
7405 strcpy(obd_types[0], "mgc");
7406 } else if (strcmp(argv[1], "all") == 0 ||
7407 strcmp(argv[1], "servers") == 0) {
7409 strcpy(obd_types[0], "osc");
7410 strcpy(obd_types[1], "mdc");
7411 strcpy(obd_types[2], "mgc");
7413 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7418 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
7419 if (rc < 0 || mntdir[0] == '\0') {
7421 "%s check: cannot find mounted Lustre filesystem: %s\n",
7422 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7426 rc = llapi_target_check(num_types, obd_types, mntdir);
7428 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7429 progname, argv[1], strerror(-rc));
7434 #ifdef HAVE_SYS_QUOTA_H
7435 #define ADD_OVERFLOW(a, b) \
7436 ((((a) + (b)) < (a)) ? \
7437 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7439 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7440 * returns the value or ULONG_MAX on integer overflow or incorrect format
7442 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7443 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7444 * 3. empty integer value is interpreted as 0
7446 static unsigned long str2sec(const char *timestr)
7448 const char spec[] = "smhdw";
7449 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7450 unsigned long val = 0;
7453 if (strpbrk(timestr, spec) == NULL) {
7455 * no specifiers inside the time string,
7456 * should treat it as an integer value
7458 val = strtoul(timestr, &tail, 10);
7459 return *tail ? ULONG_MAX : val;
7462 /* format string is XXwXXdXXhXXmXXs */
7468 v = strtoul(timestr, &tail, 10);
7469 if (v == ULONG_MAX || *tail == '\0')
7471 * value too large (ULONG_MAX or more)
7472 * or missing specifier
7476 ptr = strchr(spec, *tail);
7478 /* unknown specifier */
7483 /* check if product will overflow the type */
7484 if (!(v < ULONG_MAX / mult[ind]))
7487 ADD_OVERFLOW(val, mult[ind] * v);
7488 if (val == ULONG_MAX)
7500 #define ARG2ULL(nr, str, def_units) \
7502 unsigned long long limit, units = def_units; \
7505 rc = llapi_parse_size(str, &limit, &units, 1); \
7507 fprintf(stderr, "%s: invalid limit '%s'\n", \
7514 static inline int has_times_option(int argc, char **argv)
7518 for (i = 1; i < argc; i++)
7519 if (!strcmp(argv[i], "-t"))
7525 static inline int lfs_verify_poolarg(char *pool)
7527 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7529 "Pool name '%.*s' is longer than %d\n",
7530 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7536 /* special grace time, only notify the user when its quota is over soft limit
7537 * but doesn't block new writes until the hard limit is reached.
7539 #define NOTIFY_GRACE "notify"
7540 #define NOTIFY_GRACE_TIME LQUOTA_GRACE_MASK
7543 static inline __u64 lustre_stoqb(size_t space)
7545 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7548 #define lustre_stoqb toqb
7551 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7554 char *mnt, *obd_type = (char *)qctl->obd_type;
7555 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7556 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7557 struct option long_opts[] = {
7558 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7559 { .val = 'g', .name = "group", .has_arg = no_argument },
7560 { .val = 'h', .name = "help", .has_arg = no_argument },
7561 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7562 { .val = 'p', .name = "projid", .has_arg = no_argument },
7563 { .val = 't', .name = "times", .has_arg = no_argument },
7564 { .val = 'u', .name = "user", .has_arg = no_argument },
7565 { .val = LFS_POOL_OPT,
7566 .name = "pool", .has_arg = required_argument },
7570 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7571 qctl->qc_type = ALLQUOTA;
7573 while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7574 long_opts, NULL)) != -1) {
7585 if (qctl->qc_type != ALLQUOTA) {
7587 "%s: -u/g/p cannot be used more than once\n",
7591 qctl->qc_type = qtype;
7594 if (strncmp(optarg, NOTIFY_GRACE,
7595 strlen(NOTIFY_GRACE)) == 0) {
7596 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7598 dqi->dqi_bgrace = str2sec(optarg);
7599 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7601 "%s: bad block-grace: %s\n",
7606 dqb->dqb_valid |= QIF_BTIME;
7609 if (strncmp(optarg, NOTIFY_GRACE,
7610 strlen(NOTIFY_GRACE)) == 0) {
7611 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7613 dqi->dqi_igrace = str2sec(optarg);
7614 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7616 "%s: bad inode-grace: %s\n",
7621 dqb->dqb_valid |= QIF_ITIME;
7623 case 't': /* Yes, of course! */
7626 if (lfs_verify_poolarg(optarg))
7628 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7629 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7631 /* getopt prints error message for us when opterr != 0 */
7633 fprintf(stderr, "%s: unrecognized option '%s'\n",
7634 progname, argv[optind - 1]);
7641 if (qctl->qc_type == ALLQUOTA) {
7642 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7647 if (optind != argc - 1) {
7648 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7649 progname, argv[optind + 1]);
7654 rc = llapi_quotactl(mnt, qctl);
7657 fprintf(stderr, "%s %s ", obd_type,
7658 obd_uuid2str(&qctl->obd_uuid));
7659 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7666 #define BSLIMIT (1 << 0)
7667 #define BHLIMIT (1 << 1)
7668 #define ISLIMIT (1 << 2)
7669 #define IHLIMIT (1 << 3)
7671 int lfs_setquota(int argc, char **argv)
7674 struct if_quotactl *qctl;
7675 char *mnt, *obd_type;
7676 struct obd_dqblk *dqb;
7677 struct option long_opts[] = {
7678 { .val = 'b', .name = "block-softlimit",
7679 .has_arg = required_argument },
7680 { .val = 'B', .name = "block-hardlimit",
7681 .has_arg = required_argument },
7682 { .val = 'd', .name = "default", .has_arg = no_argument },
7683 { .val = LFS_SETQUOTA_DELETE,
7684 .name = "delete", .has_arg = no_argument },
7685 { .val = 'g', .name = "group", .has_arg = required_argument },
7686 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7687 { .val = 'h', .name = "help", .has_arg = no_argument },
7688 { .val = 'i', .name = "inode-softlimit",
7689 .has_arg = required_argument },
7690 { .val = 'I', .name = "inode-hardlimit",
7691 .has_arg = required_argument },
7692 { .val = 'p', .name = "projid", .has_arg = required_argument },
7693 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7694 { .val = 'u', .name = "user", .has_arg = required_argument },
7695 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7696 { .val = LFS_POOL_OPT,
7697 .name = "pool", .has_arg = required_argument },
7699 unsigned int limit_mask = 0;
7700 bool use_default = false;
7701 int qtype, qctl_len;
7703 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7704 qctl = malloc(qctl_len);
7708 memset(qctl, 0, qctl_len);
7709 obd_type = (char *)qctl->obd_type;
7710 dqb = &qctl->qc_dqblk;
7712 if (has_times_option(argc, argv)) {
7713 rc = lfs_setquota_times(argc, argv, qctl);
7717 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7718 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7719 * so it can be used as a marker that qc_type
7720 * isn't reinitialized from command line
7722 while ((c = getopt_long(argc, argv, "b:B:dDg:Ghi:I:p:Pu:U",
7723 long_opts, NULL)) != -1) {
7726 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7729 goto quota_type_def;
7732 rc = name2uid(&qctl->qc_id, optarg);
7735 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7738 goto quota_type_def;
7741 rc = name2gid(&qctl->qc_id, optarg);
7744 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7747 goto quota_type_def;
7750 rc = name2projid(&qctl->qc_id, optarg);
7753 if (str2quotaid(&qctl->qc_id, optarg)) {
7755 "%s setquota: invalid id '%s'\n",
7762 if (qctl->qc_id == 0) {
7764 "%s setquota: can't set quota for root usr/group/project.\n",
7771 if (qctl->qc_type != ALLQUOTA) {
7773 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7778 qctl->qc_type = qtype;
7780 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7782 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 15, 53, 0)
7783 fprintf(stderr, "'-d' deprecatd, use '-D' or '--default'\n");
7789 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7791 case LFS_SETQUOTA_DELETE:
7792 qctl->qc_cmd = LUSTRE_Q_DELETEQID;
7795 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7796 dqb->dqb_bsoftlimit >>= 10;
7797 limit_mask |= BSLIMIT;
7798 if (dqb->dqb_bsoftlimit &&
7799 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7801 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7803 (unsigned long long)dqb->dqb_bsoftlimit,
7807 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7808 dqb->dqb_bhardlimit >>= 10;
7809 limit_mask |= BHLIMIT;
7810 if (dqb->dqb_bhardlimit &&
7811 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7813 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7814 "See '%s help setquota' or Lustre manual for details\n",
7816 (unsigned long long)dqb->dqb_bhardlimit,
7820 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7821 limit_mask |= ISLIMIT;
7822 if (dqb->dqb_isoftlimit &&
7823 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7825 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7827 (unsigned long long)dqb->dqb_isoftlimit,
7831 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7832 limit_mask |= IHLIMIT;
7833 if (dqb->dqb_ihardlimit &&
7834 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7836 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7838 (unsigned long long)dqb->dqb_ihardlimit,
7842 if (lfs_verify_poolarg(optarg)) {
7846 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7847 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
7848 LUSTRE_Q_SETDEFAULT_POOL :
7849 LUSTRE_Q_SETQUOTAPOOL;
7853 "%s setquota: unrecognized option '%s'\n",
7854 progname, argv[optind - 1]);
7862 if (qctl->qc_type == ALLQUOTA) {
7864 "%s setquota: either -u or -g must be specified\n",
7870 if (!use_default && qctl->qc_cmd != LUSTRE_Q_DELETEQID &&
7873 "%s setquota: at least one limit must be specified\n",
7879 if ((use_default || qctl->qc_cmd == LUSTRE_Q_DELETEQID) &&
7882 "%s setquota: limits should not be specified when using default quota or deleting quota ID\n",
7888 if (use_default && qctl->qc_id == 0) {
7890 "%s setquota: can not set default quota for root user/group/project\n",
7896 if (qctl->qc_cmd == LUSTRE_Q_DELETEQID && qctl->qc_id == 0) {
7898 "%s setquota: can not delete root user/group/project\n",
7904 if (optind != argc - 1) {
7906 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7907 progname, argv[optind]);
7915 dqb->dqb_bhardlimit = 0;
7916 dqb->dqb_bsoftlimit = 0;
7917 dqb->dqb_ihardlimit = 0;
7918 dqb->dqb_isoftlimit = 0;
7921 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7922 /* do not set inode limits for Pool Quotas */
7923 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
7924 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
7925 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7926 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7927 /* sigh, we can't just set blimits/ilimits */
7928 struct if_quotactl *tmp_qctl;
7930 tmp_qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
7934 if (qctl->qc_cmd == LUSTRE_Q_SETQUOTAPOOL) {
7935 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
7936 strncpy(tmp_qctl->qc_poolname, qctl->qc_poolname,
7939 tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
7941 tmp_qctl->qc_type = qctl->qc_type;
7942 tmp_qctl->qc_id = qctl->qc_id;
7944 rc = llapi_quotactl(mnt, tmp_qctl);
7950 if (!(limit_mask & BHLIMIT))
7951 dqb->dqb_bhardlimit = tmp_qctl->qc_dqblk.dqb_bhardlimit;
7952 if (!(limit_mask & BSLIMIT))
7953 dqb->dqb_bsoftlimit = tmp_qctl->qc_dqblk.dqb_bsoftlimit;
7954 if (!(limit_mask & IHLIMIT))
7955 dqb->dqb_ihardlimit = tmp_qctl->qc_dqblk.dqb_ihardlimit;
7956 if (!(limit_mask & ISLIMIT))
7957 dqb->dqb_isoftlimit = tmp_qctl->qc_dqblk.dqb_isoftlimit;
7959 /* Keep grace times if we have got no softlimit arguments */
7960 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7961 dqb->dqb_valid |= QIF_BTIME;
7962 dqb->dqb_btime = tmp_qctl->qc_dqblk.dqb_btime;
7965 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7966 dqb->dqb_valid |= QIF_ITIME;
7967 dqb->dqb_itime = tmp_qctl->qc_dqblk.dqb_itime;
7972 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7973 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7975 rc = llapi_quotactl(mnt, qctl);
7979 "%s setquota: cannot quotactl '%s' '%s': %s\n",
7981 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7984 "%s setquota: quotactl failed: %s\n",
7985 progname, strerror(-rc));
7989 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7995 /* Converts seconds value into format string
7996 * result is returned in buf
7998 * 1. result is in descenting order: 1w2d3h4m5s
7999 * 2. zero fields are not filled (except for p. 3): 5d1s
8000 * 3. zero seconds value is presented as "0s"
8002 static char *__sec2str(time_t seconds, char *buf)
8004 const char spec[] = "smhdw";
8005 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
8010 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
8011 c = seconds / mult[i];
8013 if (c > 0 || (i == 0 && buf == tail))
8014 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
8023 static void sec2str(time_t seconds, char *buf, int rc)
8030 tail = __sec2str(seconds, tail);
8032 if (rc && tail - buf < 39) {
8038 static void diff2str(time_t seconds, char *buf, time_t now)
8043 if (seconds <= now) {
8044 strcpy(buf, "none");
8047 __sec2str(seconds - now, buf);
8050 static void print_quota_title(char *name, struct if_quotactl *qctl,
8051 bool human_readable, bool show_default)
8054 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
8055 printf("%15s %8s%8s%8s %8s%8s%8s\n",
8056 "Filesystem", "bquota", "blimit", "bgrace",
8057 "iquota", "ilimit", "igrace");
8059 printf("Disk quotas for %s %s (%cid %u):\n",
8060 qtype_name(qctl->qc_type), name,
8061 *qtype_name(qctl->qc_type), qctl->qc_id);
8062 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
8063 "Filesystem", human_readable ? "used" : "kbytes",
8064 "quota", "limit", "grace",
8065 "files", "quota", "limit", "grace");
8069 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8072 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8075 snprintf(buf, buflen, "%5.4gP",
8076 (double)num / ((__u64)1 << 40));
8078 snprintf(buf, buflen, "%5.4gT",
8079 (double)num / (1 << 30));
8081 snprintf(buf, buflen, "%5.4gG",
8082 (double)num / (1 << 20));
8084 snprintf(buf, buflen, "%5.4gM",
8085 (double)num / (1 << 10));
8087 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8091 #ifdef HAVE_NATIVE_CLIENT
8092 /* In the current Lustre implementation, the grace time is either the time
8093 * or the timestamp to be used after some quota ID exceeds the soft limt,
8094 * 48 bits should be enough, its high 16 bits can be used as quota flags.
8096 #define LQUOTA_GRACE_BITS 48
8097 #define LQUOTA_GRACE_MASK ((1ULL << LQUOTA_GRACE_BITS) - 1)
8098 #define LQUOTA_GRACE_MAX LQUOTA_GRACE_MASK
8099 #define LQUOTA_GRACE(t) (t & LQUOTA_GRACE_MASK)
8100 #define LQUOTA_FLAG(t) (t >> LQUOTA_GRACE_BITS)
8101 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8104 #define STRBUF_LEN 24
8105 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8106 int rc, bool h, bool show_default)
8112 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8113 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8114 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8115 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8116 int bover = 0, iover = 0;
8117 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8118 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8120 char strbuf[STRBUF_LEN];
8122 if (dqb->dqb_bhardlimit &&
8123 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8125 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8126 if (dqb->dqb_btime > now)
8132 if (dqb->dqb_ihardlimit &&
8133 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8135 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8136 if (dqb->dqb_itime > now)
8142 if (strlen(mnt) > 15)
8143 printf("%s\n%15s", mnt, "");
8145 printf("%15s", mnt);
8148 diff2str(dqb->dqb_btime, timebuf, now);
8149 else if (show_default)
8150 snprintf(timebuf, sizeof(timebuf), "%llu",
8151 (unsigned long long)dqb->dqb_btime);
8153 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8154 strbuf, sizeof(strbuf), h);
8155 if (rc == -EREMOTEIO)
8156 sprintf(numbuf[0], "%s*", strbuf);
8158 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8159 "%s" : "[%s]", strbuf);
8161 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8162 if (type == QC_GENERAL)
8163 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8164 "%s" : "[%s]", strbuf);
8166 sprintf(numbuf[1], "%s", "-");
8168 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8169 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8170 "%s" : "[%s]", strbuf);
8173 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8175 printf(" %7s%c %6s %7s %7s",
8176 numbuf[0], bover ? '*' : ' ', numbuf[1],
8177 numbuf[2], bover > 1 ? timebuf : "-");
8180 diff2str(dqb->dqb_itime, timebuf, now);
8181 else if (show_default)
8182 snprintf(timebuf, sizeof(timebuf), "%llu",
8183 (unsigned long long)dqb->dqb_itime);
8185 snprintf(numbuf[0], sizeof(numbuf),
8186 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8187 (uintmax_t)dqb->dqb_curinodes);
8189 if (type == QC_GENERAL)
8190 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8192 (uintmax_t)dqb->dqb_isoftlimit);
8194 sprintf(numbuf[1], "%s", "-");
8196 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8197 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8200 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8201 else if (type != QC_OSTIDX)
8202 printf(" %7s%c %6s %7s %7s",
8203 numbuf[0], iover ? '*' : ' ', numbuf[1],
8204 numbuf[2], iover > 1 ? timebuf : "-");
8206 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8208 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8209 qctl->qc_cmd == Q_GETOINFO) {
8213 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8214 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8216 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8217 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8218 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8220 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8222 printf("Block grace time: %s; Inode grace time: %s\n",
8223 bgtimebuf, igtimebuf);
8227 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8231 /* format is "lustre-OST0001" */
8232 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8234 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8239 *idx = strtoul(dash, &endp, 16);
8240 if (*idx > 0xffff) {
8241 fprintf(stderr, "wrong index %s\n", tgtname);
8248 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8249 bool h, __u64 *total)
8251 int rc = 0, rc1 = 0, count = 0, i = 0;
8252 char **list = NULL, *buffer = NULL;
8253 __u32 valid = qctl->qc_valid;
8255 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8258 /* Is it correct for the case OST0000, OST0002, OST0003 -
8259 * we will ask OST0001 that is absent and won't ask OST0003? */
8260 rc = llapi_get_obd_count(mnt, &count, is_mdt);
8262 fprintf(stderr, "can not get %s count: %s\n",
8263 is_mdt ? "mdt" : "ost", strerror(-rc));
8267 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8268 char fname[PATH_MAX];
8269 char fsname[LUSTRE_MAXFSNAME + 1];
8270 int bufsize = sizeof(struct obd_uuid) * count;
8272 rc = llapi_search_fsname(mnt, fsname);
8274 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8278 buffer = malloc(bufsize + sizeof(*list) * count);
8281 list = (char **)(buffer + bufsize);
8282 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8283 count = llapi_get_poolmembers(fname, list, count,
8289 for (i = 0; i < count; i++) {
8290 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8293 if (tgt_name2index(list[i], &index))
8295 qctl->qc_idx = index;
8300 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8301 rc = llapi_quotactl(mnt, qctl);
8303 /* It is remote client case. */
8304 if (rc == -EOPNOTSUPP) {
8311 fprintf(stderr, "quotactl %s%d failed.\n",
8312 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8316 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8317 qctl->qc_valid, 0, h, false);
8318 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8319 qctl->qc_dqblk.dqb_bhardlimit;
8324 qctl->qc_valid = valid;
8328 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8329 int verbose, int quiet, bool human_readable,
8332 int rc1 = 0, rc2 = 0, rc3 = 0;
8333 char *obd_type = (char *)qctl->obd_type;
8334 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8335 __u64 total_ialloc = 0, total_balloc = 0;
8336 bool use_default_for_blk = false;
8337 bool use_default_for_file = false;
8340 rc1 = llapi_quotactl(mnt, qctl);
8344 fprintf(stderr, "%s quotas are not enabled.\n",
8345 qtype_name(qctl->qc_type));
8348 fprintf(stderr, "Permission denied.\n");
8351 /* We already got error message. */
8354 fprintf(stderr, "Unexpected quotactl error: %s\n",
8359 if (!show_default && qctl->qc_id == 0) {
8360 qctl->qc_dqblk.dqb_bhardlimit = 0;
8361 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8362 qctl->qc_dqblk.dqb_ihardlimit = 0;
8363 qctl->qc_dqblk.dqb_isoftlimit = 0;
8364 qctl->qc_dqblk.dqb_btime = 0;
8365 qctl->qc_dqblk.dqb_itime = 0;
8366 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8369 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8370 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8371 use_default_for_blk = true;
8372 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8375 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8376 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8377 use_default_for_file = true;
8378 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8381 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8382 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8383 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8384 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8385 print_quota_title(name, qctl, human_readable, show_default);
8387 if (rc1 && *obd_type)
8388 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8390 if (qctl->qc_valid != QC_GENERAL)
8393 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8394 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8395 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8396 (QIF_LIMITS|QIF_USAGE));
8398 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8400 if (!show_default && verbose &&
8401 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8402 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8403 char strbuf[STRBUF_LEN];
8405 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8407 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8409 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8411 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8412 (uintmax_t)total_ialloc, strbuf);
8415 if (use_default_for_blk)
8416 printf("%cid %u is using default block quota setting\n",
8417 *qtype_name(qctl->qc_type), qctl->qc_id);
8419 if (use_default_for_file)
8420 printf("%cid %u is using default file quota setting\n",
8421 *qtype_name(qctl->qc_type), qctl->qc_id);
8423 if (rc1 || rc2 || rc3 || inacc)
8424 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8438 static int lfs_project(int argc, char **argv)
8440 int ret = 0, err = 0, c, i;
8441 struct project_handle_control phc = { 0 };
8442 enum lfs_project_ops_t op;
8445 phc.assign_projid = false;
8446 /* default action */
8447 op = LFS_PROJECT_LIST;
8449 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8452 if (op != LFS_PROJECT_LIST) {
8454 "%s: cannot specify '-c' '-C' '-s' together\n",
8459 op = LFS_PROJECT_CHECK;
8462 if (op != LFS_PROJECT_LIST) {
8464 "%s: cannot specify '-c' '-C' '-s' together\n",
8469 op = LFS_PROJECT_CLEAR;
8472 if (op != LFS_PROJECT_LIST) {
8474 "%s: cannot specify '-c' '-C' '-s' together\n",
8479 phc.set_inherit = true;
8480 op = LFS_PROJECT_SET;
8486 phc.keep_projid = true;
8489 phc.recursive = true;
8492 if (str2quotaid(&phc.projid, optarg)) {
8494 "Invalid project ID: %s\n",
8499 phc.assign_projid = true;
8503 phc.newline = false;
8506 fprintf(stderr, "%s: invalid option '%c'\n",
8512 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8513 op = LFS_PROJECT_SET;
8514 phc.set_projid = true;
8515 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8516 phc.set_projid = true;
8520 case LFS_PROJECT_CHECK:
8521 if (phc.keep_projid) {
8523 "%s: '-k' is useless together with '-c'\n",
8528 case LFS_PROJECT_CLEAR:
8531 "%s: '-0' is useless together with '-C'\n",
8535 if (phc.assign_projid) {
8537 "%s: '-p' is useless together with '-C'\n",
8542 case LFS_PROJECT_SET:
8545 "%s: '-0' is useless together with '-s'\n",
8549 if (phc.keep_projid) {
8551 "%s: '-k' is useless together with '-s'\n",
8559 "%s: '-0' is useless for list operations\n",
8569 fprintf(stderr, "%s: missing file or directory target(s)\n",
8574 for (i = 0; i < argc; i++) {
8576 case LFS_PROJECT_CHECK:
8577 err = lfs_project_check(argv[i], &phc);
8579 case LFS_PROJECT_LIST:
8580 err = lfs_project_list(argv[i], &phc);
8582 case LFS_PROJECT_CLEAR:
8583 err = lfs_project_clear(argv[i], &phc);
8585 case LFS_PROJECT_SET:
8586 err = lfs_project_set(argv[i], &phc);
8598 static int lfs_quota(int argc, char **argv)
8601 char *mnt, *name = NULL;
8602 struct if_quotactl *qctl;
8604 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8605 __u32 valid = QC_GENERAL, idx = 0;
8606 bool human_readable = false;
8607 bool show_default = false;
8609 bool show_pools = false;
8610 struct option long_opts[] = {
8611 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
8613 char **poollist = NULL;
8617 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8621 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8622 qctl->qc_type = ALLQUOTA;
8623 obd_uuid = (char *)qctl->obd_uuid.uuid;
8625 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8626 long_opts, NULL)) != -1) {
8629 show_default = true;
8634 show_default = true;
8639 show_default = true;
8643 if (qctl->qc_type != ALLQUOTA) {
8645 "%s quota: only one of -u, -g, or -p may be specified\n",
8650 qctl->qc_type = qtype;
8653 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8656 valid = qctl->qc_valid = QC_UUID;
8657 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8660 valid = qctl->qc_valid = QC_MDTIDX;
8661 idx = qctl->qc_idx = atoi(optarg);
8662 if (idx == 0 && *optarg != '0') {
8664 "%s quota: invalid MDT index '%s'\n",
8671 valid = qctl->qc_valid = QC_OSTIDX;
8672 idx = qctl->qc_idx = atoi(optarg);
8673 if (idx == 0 && *optarg != '0') {
8675 "%s quota: invalid OST index '%s'\n",
8688 human_readable = true;
8691 if ((!optarg) && (argv[optind] != NULL) &&
8692 (argv[optind][0] != '-') &&
8693 (argv[optind][0] != '/')) {
8694 optarg = argv[optind++];
8695 if (lfs_verify_poolarg(optarg)) {
8699 strncpy(qctl->qc_poolname, optarg,
8701 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
8702 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
8704 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8708 /* optarg is NULL */
8710 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8713 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8714 progname, argv[optind - 1]);
8720 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8721 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8722 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8723 qctl->qc_type == ALLQUOTA &&
8724 optind == argc - 1 && !show_default) {
8727 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8728 qctl->qc_type = qtype;
8729 qctl->qc_valid = valid;
8730 if (qtype == USRQUOTA) {
8731 qctl->qc_id = geteuid();
8732 rc = uid2name(&name, qctl->qc_id);
8734 qctl->qc_id = getegid();
8735 rc = gid2name(&name, qctl->qc_id);
8736 memset(&qctl->qc_dqblk, 0,
8737 sizeof(qctl->qc_dqblk));
8742 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8743 human_readable, show_default);
8748 /* lfs quota -u username /path/to/lustre/mount */
8749 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8750 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8751 /* options should be followed by u/g-name and mntpoint */
8752 if ((!show_default && optind + 2 != argc) ||
8753 (show_default && optind + 1 != argc) ||
8754 qctl->qc_type == ALLQUOTA) {
8756 "%s quota: name and mount point must be specified\n",
8762 if (!show_default) {
8763 name = argv[optind++];
8764 switch (qctl->qc_type) {
8766 rc = name2uid(&qctl->qc_id, name);
8769 rc = name2gid(&qctl->qc_id, name);
8772 rc = name2projid(&qctl->qc_id, name);
8779 qctl->qc_valid = QC_GENERAL;
8780 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8781 LUSTRE_Q_GETDEFAULT_POOL :
8782 LUSTRE_Q_GETDEFAULT;
8787 if (str2quotaid(&qctl->qc_id, name)) {
8788 fprintf(stderr, "%s quota: invalid id '%s'\n",
8794 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8795 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8806 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
8810 for (i = 0; i < poolcount; i++) {
8811 p = memchr(poollist[i], '.', MAXNAMLEN);
8813 fprintf(stderr, "bad string format %.*s\n",
8814 MAXNAMLEN, poollist[i]);
8819 printf("Quotas for pool: %s\n", p);
8820 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
8821 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8822 human_readable, show_default);
8829 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8830 human_readable, show_default);
8836 #endif /* HAVE_SYS_QUOTA_H! */
8838 static int flushctx_ioctl(char *mp)
8842 fd = open(mp, O_RDONLY);
8844 fprintf(stderr, "flushctx: error open %s: %s\n",
8845 mp, strerror(errno));
8849 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8851 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8852 mp, strerror(errno));
8858 static int lfs_flushctx(int argc, char **argv)
8860 int kdestroy = 0, reap = 0, c;
8861 char mntdir[PATH_MAX] = {'\0'};
8865 while ((c = getopt(argc, argv, "kr")) != -1) {
8875 "error: %s: option '-%c' unrecognized\n",
8882 rc = system("kdestroy > /dev/null");
8884 rc = WEXITSTATUS(rc);
8886 "error destroying tickets: %d, continuing\n",
8891 if (optind >= argc) {
8892 /* flush for all mounted lustre fs. */
8893 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8894 /* Check if we have a mount point */
8895 if (mntdir[0] == '\0')
8898 if (flushctx_ioctl(mntdir))
8901 mntdir[0] = '\0'; /* avoid matching in next loop */
8904 /* flush fs as specified */
8905 while (optind < argc) {
8906 if (flushctx_ioctl(argv[optind++]))
8912 rc = system("keyctl reap > /dev/null");
8914 rc = WEXITSTATUS(rc);
8915 fprintf(stderr, "error reaping keyring: %d\n", rc);
8922 static int lfs_changelog(int argc, char **argv)
8924 void *changelog_priv;
8925 struct changelog_rec *rec;
8926 long long startrec = 0, endrec = 0;
8928 struct option long_opts[] = {
8929 { .val = 'f', .name = "follow", .has_arg = no_argument },
8931 char short_opts[] = "f";
8934 while ((rc = getopt_long(argc, argv, short_opts,
8935 long_opts, NULL)) != -1) {
8942 "%s changelog: unrecognized option '%s'\n",
8943 progname, argv[optind - 1]);
8947 if (optind >= argc) {
8948 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8953 mdd = argv[optind++];
8954 if (argc > optind) {
8956 startrec = strtoll(argv[optind++], NULL, 10);
8957 if (errno != 0 || startrec < 0) {
8959 "%s changelog: bad startrec\n",
8965 if (argc > optind) {
8967 endrec = strtoll(argv[optind++], NULL, 10);
8968 if (errno != 0 || endrec < 0) {
8970 "%s changelog: bad endrec\n",
8976 rc = llapi_changelog_start(&changelog_priv,
8977 CHANGELOG_FLAG_BLOCK |
8978 CHANGELOG_FLAG_JOBID |
8979 CHANGELOG_FLAG_EXTRA_FLAGS |
8980 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8983 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8984 progname, strerror(errno = -rc));
8988 rc = llapi_changelog_set_xflags(changelog_priv,
8989 CHANGELOG_EXTRA_FLAG_UIDGID |
8990 CHANGELOG_EXTRA_FLAG_NID |
8991 CHANGELOG_EXTRA_FLAG_OMODE |
8992 CHANGELOG_EXTRA_FLAG_XATTR);
8995 "%s changelog: cannot set xflags for changelog: %s\n",
8996 progname, strerror(errno = -rc));
9000 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
9004 if (endrec && rec->cr_index > endrec) {
9005 llapi_changelog_free(&rec);
9008 if (rec->cr_index < startrec) {
9009 llapi_changelog_free(&rec);
9013 secs = rec->cr_time >> 30;
9014 gmtime_r(&secs, &ts);
9015 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
9016 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
9017 changelog_type2str(rec->cr_type),
9018 ts.tm_hour, ts.tm_min, ts.tm_sec,
9019 (int)(rec->cr_time & ((1 << 30) - 1)),
9020 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
9021 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
9023 if (rec->cr_flags & CLF_JOBID) {
9024 struct changelog_ext_jobid *jid =
9025 changelog_rec_jobid(rec);
9027 if (jid->cr_jobid[0] != '\0')
9028 printf(" j=%s", jid->cr_jobid);
9031 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
9032 struct changelog_ext_extra_flags *ef =
9033 changelog_rec_extra_flags(rec);
9035 printf(" ef=0x%llx",
9036 (unsigned long long)ef->cr_extra_flags);
9038 if (ef->cr_extra_flags & CLFE_UIDGID) {
9039 struct changelog_ext_uidgid *uidgid =
9040 changelog_rec_uidgid(rec);
9042 printf(" u=%llu:%llu",
9043 (unsigned long long)uidgid->cr_uid,
9044 (unsigned long long)uidgid->cr_gid);
9046 if (ef->cr_extra_flags & CLFE_NID) {
9047 struct changelog_ext_nid *nid =
9048 changelog_rec_nid(rec);
9051 libcfs_nid2str(nid->cr_nid));
9054 if (ef->cr_extra_flags & CLFE_OPEN) {
9055 struct changelog_ext_openmode *omd =
9056 changelog_rec_openmode(rec);
9057 char mode[] = "---";
9059 /* exec mode must be exclusive */
9060 if (omd->cr_openflags & MDS_FMODE_EXEC) {
9063 if (omd->cr_openflags & MDS_FMODE_READ)
9065 if (omd->cr_openflags &
9072 if (strcmp(mode, "---") != 0)
9073 printf(" m=%s", mode);
9076 if (ef->cr_extra_flags & CLFE_XATTR) {
9077 struct changelog_ext_xattr *xattr =
9078 changelog_rec_xattr(rec);
9080 if (xattr->cr_xattr[0] != '\0')
9081 printf(" x=%s", xattr->cr_xattr);
9085 if (!fid_is_zero(&rec->cr_pfid))
9086 printf(" p="DFID, PFID(&rec->cr_pfid));
9087 if (rec->cr_namelen)
9088 printf(" %.*s", rec->cr_namelen,
9089 changelog_rec_name(rec));
9091 if (rec->cr_flags & CLF_RENAME) {
9092 struct changelog_ext_rename *rnm =
9093 changelog_rec_rename(rec);
9095 if (!fid_is_zero(&rnm->cr_sfid))
9096 printf(" s="DFID" sp="DFID" %.*s",
9097 PFID(&rnm->cr_sfid),
9098 PFID(&rnm->cr_spfid),
9099 (int)changelog_rec_snamelen(rec),
9100 changelog_rec_sname(rec));
9104 llapi_changelog_free(&rec);
9107 llapi_changelog_fini(&changelog_priv);
9110 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9111 progname, strerror(errno = -rc));
9113 return (rc == 1 ? 0 : rc);
9116 static int lfs_changelog_clear(int argc, char **argv)
9125 endrec = strtoll(argv[3], NULL, 10);
9126 if (errno != 0 || endrec < 0) {
9128 "%s: bad endrec '%s'\n",
9133 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9136 fprintf(stderr, "%s: record out of range: %llu\n",
9138 else if (rc == -ENOENT)
9139 fprintf(stderr, "%s: no changelog user: %s\n",
9142 fprintf(stderr, "%s error: %s\n", argv[0],
9151 static void rstripc(char *str, int c)
9153 char *end = str + strlen(str);
9155 for (; str < end && end[-1] == c; --end)
9159 static int lfs_fid2path(int argc, char **argv)
9161 struct option long_opts[] = {
9162 { .val = 'c', .name = "cur", .has_arg = no_argument },
9163 { .val = 'c', .name = "current", .has_arg = no_argument },
9164 { .val = 'c', .name = "print-link", .has_arg = no_argument },
9165 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
9166 { .val = 'l', .name = "link", .has_arg = required_argument },
9168 char short_opts[] = "cfl:pr:";
9169 bool print_link = false;
9170 bool print_fid = false;
9172 char mnt_dir[PATH_MAX] = "";
9174 char *path_or_fsname;
9175 long long recno = -1;
9177 char *endptr = NULL;
9182 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9192 linkno = strtol(optarg, &endptr, 10);
9193 if (errno != 0 || *endptr != '\0' || linkno < 0) {
9195 "%s fid2path: invalid linkno '%s'\n",
9201 /* recno is something to do with changelogs
9202 * that was never implemented. We just pass it
9203 * through for the MDT to ignore.
9206 recno = strtoll(optarg, &endptr, 10);
9207 if (errno != 0 || *endptr != '\0' || recno < 0) {
9209 "%s fid2path: invalid recno '%s'\n",
9216 "%s fid2path: unrecognized option '%s'\n",
9217 progname, argv[optind - 1]);
9222 if (argc - optind < 2) {
9224 "Usage: %s fid2path FSNAME|ROOT FID...\n",
9229 path_or_fsname = argv[optind];
9231 if (*path_or_fsname == '/') {
9232 print_mnt_dir = true;
9233 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9235 print_mnt_dir = false;
9236 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9241 "%s fid2path: cannot resolve mount point for '%s': %s\n",
9242 progname, path_or_fsname, strerror(-rc));
9246 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9249 "%s fid2path: cannot open mount point for '%s': %s\n",
9250 progname, path_or_fsname, strerror(-rc));
9254 /* Strip trailing slashes from mnt_dir. */
9255 rstripc(mnt_dir + 1, '/');
9257 for (i = optind + 1; i < argc; i++) {
9258 const char *fid_str = argv[i];
9262 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9265 "%s fid2path: invalid FID '%s'\n",
9273 int linktmp = (linkno >= 0) ? linkno : 0;
9275 int oldtmp = linktmp;
9276 long long rectmp = recno;
9277 char path_buf[PATH_MAX];
9279 rc2 = llapi_fid2path_at(mnt_fd, &fid,
9280 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9283 "%s fid2path: cannot find %s %s: %s\n",
9284 progname, path_or_fsname, fid_str,
9292 printf("%s ", fid_str);
9295 printf("%d ", linktmp);
9297 /* You may think this looks wrong or weird (and it is!)
9298 * but we are actually trying to preserve the old quirky
9299 * behaviors (enforced by our old quirky tests!) that
9300 * make lfs so much fun to work on:
9302 * lustre 0x200000007:0x1:0x0 => "/"
9303 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9305 * Note that llapi_fid2path() returns "" for the root
9309 print_mnt_dir ? mnt_dir : "",
9310 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9314 /* specified linkno */
9317 if (oldtmp == linktmp)
9329 static int lfs_path2fid(int argc, char **argv)
9331 struct option long_opts[] = {
9332 { .val = 'p', .name = "parents", .has_arg = no_argument },
9335 const char short_opts[] = "p";
9336 const char *sep = "";
9339 bool show_parents = false;
9341 while ((rc = getopt_long(argc, argv, short_opts,
9342 long_opts, NULL)) != -1) {
9345 show_parents = true;
9349 "%s path2fid: unrecognized option '%s'\n",
9350 progname, argv[optind - 1]);
9355 if (optind > argc - 1) {
9356 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9359 } else if (optind < argc - 1) {
9364 for (path = argv + optind; *path != NULL; path++) {
9367 if (!show_parents) {
9368 err = llapi_path2fid(*path, &fid);
9370 printf("%s%s"DFID"\n",
9371 *sep != '\0' ? *path : "", sep,
9374 char name[NAME_MAX + 1];
9375 unsigned int linkno = 0;
9377 while ((err = llapi_path2parent(*path, linkno, &fid,
9378 name, sizeof(name))) == 0) {
9379 if (*sep != '\0' && linkno == 0)
9380 printf("%s%s", *path, sep);
9382 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9387 /* err == -ENODATA is end-of-loop */
9388 if (linkno > 0 && err == -ENODATA) {
9396 "%s path2fid: cannot get %sfid for '%s': %s\n",
9397 progname, show_parents ? "parent " : "", *path,
9409 #define MAX_ERRNO 4095
9410 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9412 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9416 rc = llapi_rmfid(device, fa);
9418 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9419 progname, strerror(-rc));
9423 for (k = 0; k < fa->fa_nr; k++) {
9424 rc2 = (__s32)fa->fa_fids[k].f_ver;
9425 if (!IS_ERR_VALUE(rc2))
9431 fa->fa_fids[k].f_ver = 0;
9432 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9433 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9439 static int lfs_rmfid(int argc, char **argv)
9441 char *fidstr, *device;
9442 int rc = 0, rc2, nr;
9443 struct fid_array *fa;
9445 if (optind > argc - 1) {
9446 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9450 device = argv[optind++];
9453 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9459 while (optind < argc) {
9462 fidstr = argv[optind++];
9463 while (*fidstr == '[')
9465 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9467 fprintf(stderr, "unrecognized FID: %s\n",
9472 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9473 /* start another batch */
9474 rc2 = lfs_rmfid_and_show_errors(device, fa);
9481 rc2 = lfs_rmfid_and_show_errors(device, fa);
9489 static int lfs_data_version(int argc, char **argv)
9491 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9499 fprintf(stderr, "%s: FILE must be specified\n",
9504 while ((c = getopt(argc, argv, "hnrw")) != -1) {
9507 data_version_flags = 0;
9510 data_version_flags |= LL_DV_RD_FLUSH;
9513 data_version_flags |= LL_DV_WR_FLUSH;
9517 "%s data_version: unrecognized option '%s'\n",
9518 progname, argv[optind - 1]);
9524 if (optind == argc) {
9525 fprintf(stderr, "%s data_version: FILE must be specified\n",
9530 path = argv[optind];
9531 fd = open(path, O_RDONLY);
9534 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9535 progname, path, strerror(-rc));
9539 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9542 "%s data_version: cannot get version for '%s': %s\n",
9543 progname, path, strerror(-rc));
9545 printf("%ju" "\n", (uintmax_t)data_version);
9551 static int lfs_hsm_state(int argc, char **argv)
9556 struct hsm_user_state hus;
9564 rc = llapi_hsm_state_get(path, &hus);
9566 fprintf(stderr, "can't get hsm state for %s: %s\n",
9567 path, strerror(errno = -rc));
9571 /* Display path name and status flags */
9572 printf("%s: (0x%08x)", path, hus.hus_states);
9574 if (hus.hus_states & HS_RELEASED)
9575 printf(" released");
9576 if (hus.hus_states & HS_EXISTS)
9578 if (hus.hus_states & HS_DIRTY)
9580 if (hus.hus_states & HS_ARCHIVED)
9581 printf(" archived");
9582 /* Display user-settable flags */
9583 if (hus.hus_states & HS_NORELEASE)
9584 printf(" never_release");
9585 if (hus.hus_states & HS_NOARCHIVE)
9586 printf(" never_archive");
9587 if (hus.hus_states & HS_LOST)
9588 printf(" lost_from_hsm");
9590 if (hus.hus_archive_id != 0)
9591 printf(", archive_id:%d", hus.hus_archive_id);
9594 } while (++i < argc);
9599 #define LFS_HSM_SET 0
9600 #define LFS_HSM_CLEAR 1
9603 * Generic function to set or clear HSM flags.
9604 * Used by hsm_set and hsm_clear.
9606 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9608 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9610 struct option long_opts[] = {
9611 { .val = 'A', .name = "archived", .has_arg = no_argument },
9612 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
9613 { .val = 'd', .name = "dirty", .has_arg = no_argument },
9614 { .val = 'e', .name = "exists", .has_arg = no_argument },
9615 { .val = 'h', .name = "help", .has_arg = no_argument },
9616 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
9617 { .val = 'l', .name = "lost", .has_arg = no_argument },
9618 { .val = 'r', .name = "norelease", .has_arg = no_argument },
9623 __u32 archive_id = 0;
9629 while ((c = getopt_long(argc, argv, "aAdehi:lr",
9630 long_opts, NULL)) != -1) {
9636 mask |= HS_NOARCHIVE;
9639 mask |= HS_ARCHIVED;
9642 mask |= HS_NORELEASE;
9652 archive_id = strtol(optarg, &end, 10);
9653 if (errno != 0 || *end != '\0' || archive_id < 0) {
9655 "%s: invalid archive_id: '%s'\n",
9661 fprintf(stderr, "%s: unrecognized option '%s'\n",
9662 progname, argv[optind - 1]);
9669 /* User should have specified a flag */
9673 while (optind < argc) {
9674 path = argv[optind];
9676 /* If mode == 0, this means we apply the mask. */
9677 if (mode == LFS_HSM_SET)
9678 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9680 rc = llapi_hsm_state_set(path, 0, mask, 0);
9683 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9684 path, strerror(errno = -rc));
9693 static int lfs_hsm_action(int argc, char **argv)
9698 struct hsm_current_action hca;
9699 struct hsm_extent he;
9700 enum hsm_user_action hua;
9701 enum hsm_progress_states hps;
9709 rc = llapi_hsm_current_action(path, &hca);
9711 fprintf(stderr, "can't get hsm action for %s: %s\n",
9712 path, strerror(errno = -rc));
9715 he = hca.hca_location;
9716 hua = hca.hca_action;
9717 hps = hca.hca_state;
9719 printf("%s: %s", path, hsm_user_action2name(hua));
9721 /* Skip file without action */
9722 if (hca.hca_action == HUA_NONE) {
9727 printf(" %s ", hsm_progress_state2name(hps));
9729 if ((hps == HPS_RUNNING) &&
9730 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9731 printf("(%llu bytes moved)\n",
9732 (unsigned long long)he.length);
9733 else if ((he.offset + he.length) == LUSTRE_EOF)
9734 printf("(from %llu to EOF)\n",
9735 (unsigned long long)he.offset);
9737 printf("(from %llu to %llu)\n",
9738 (unsigned long long)he.offset,
9739 (unsigned long long)(he.offset + he.length));
9741 } while (++i < argc);
9746 static int lfs_hsm_set(int argc, char **argv)
9748 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9751 static int lfs_hsm_clear(int argc, char **argv)
9753 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9757 * Check file state and return its fid, to be used by lfs_hsm_request().
9759 * \param[in] file Path to file to check
9760 * \param[in,out] fid Pointer to allocated lu_fid struct.
9761 * \param[in,out] last_dev Pointer to last device id used.
9763 * \return 0 on success.
9765 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9771 rc = lstat(file, &st);
9773 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9777 * Checking for regular file as archiving as posix copytool
9778 * rejects archiving files other than regular files
9780 if (!S_ISREG(st.st_mode)) {
9781 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9784 /* A request should be ... */
9785 if (*last_dev != st.st_dev && *last_dev != 0) {
9787 "All files should be on the same filesystem: %s\n",
9791 *last_dev = st.st_dev;
9793 rc = llapi_path2fid(file, fid);
9795 fprintf(stderr, "Cannot read FID of %s: %s\n",
9796 file, strerror(-rc));
9802 /* Fill an HSM HUR item with a given file name.
9804 * If mntpath is set, then the filename is actually a FID, and no
9805 * lookup on the filesystem will be performed.
9807 * \param[in] hur the user request to fill
9808 * \param[in] idx index of the item inside the HUR to fill
9809 * \param[in] mntpath mountpoint of Lustre
9810 * \param[in] fname filename (if mtnpath is NULL)
9811 * or FID (if mntpath is set)
9812 * \param[in] last_dev pointer to last device id used
9814 * \retval 0 on success
9815 * \retval CMD_HELP or a negative errno on error
9817 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9818 const char *mntpath, const char *fname,
9821 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9824 hui->hui_extent.length = -1;
9827 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9829 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9832 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9836 hur->hur_request.hr_itemcount++;
9841 static int lfs_hsm_request(int argc, char **argv, int action)
9843 struct option long_opts[] = {
9844 { .val = 'a', .name = "archive", .has_arg = required_argument },
9845 { .val = 'D', .name = "data", .has_arg = required_argument },
9846 { .val = 'h', .name = "help", .has_arg = no_argument },
9847 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9848 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9851 struct hsm_user_request *hur, *oldhur;
9856 char *filelist = NULL;
9857 char fullpath[PATH_MAX];
9858 char *opaque = NULL;
9862 int nbfile_alloc = 0;
9863 char *some_file = NULL;
9864 char *mntpath = NULL;
9870 while ((c = getopt_long(argc, argv, "a:D:hl:m:",
9871 long_opts, NULL)) != -1) {
9880 if (action != HUA_ARCHIVE &&
9881 action != HUA_REMOVE) {
9883 "error: -a is supported only when archiving or removing\n");
9886 archive_id = atoi(optarg);
9891 some_file = strdup(optarg);
9895 fprintf(stderr, "%s: unrecognized option '%s'\n",
9896 progname, argv[optind - 1]);
9903 /* All remaining args are files, so we have at least nbfile */
9904 nbfile = argc - optind;
9906 if ((nbfile == 0) && (!filelist))
9910 opaque_len = strlen(opaque);
9913 * Alloc the request structure with enough place to store all files
9914 * from command line.
9916 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9918 fprintf(stderr, "Cannot create the request: %s\n",
9922 nbfile_alloc = nbfile;
9924 hur->hur_request.hr_action = action;
9925 hur->hur_request.hr_archive_id = archive_id;
9926 hur->hur_request.hr_flags = 0;
9928 /* All remaining args are files, add them */
9929 if (nbfile != 0 && some_file == NULL)
9930 some_file = strdup(argv[optind]);
9932 for (i = 0; i < nbfile; i++) {
9933 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9939 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9941 /* If a filelist was specified, read the filelist from it. */
9943 fp = fopen(filelist, "r");
9945 fprintf(stderr, "Cannot read the file list %s: %s\n",
9946 filelist, strerror(errno));
9951 while ((rc = getline(&line, &len, fp)) != -1) {
9953 * If allocated buffer was too small, get something
9956 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9959 nbfile_alloc = nbfile_alloc * 2 + 1;
9961 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9965 "hsm: cannot allocate the request: %s\n",
9972 size = hur_len(oldhur);
9975 "hsm: cannot allocate %u files + %u bytes data\n",
9976 oldhur->hur_request.hr_itemcount,
9977 oldhur->hur_request.hr_data_len);
9984 memcpy(hur, oldhur, size);
9989 if (line[strlen(line) - 1] == '\n')
9990 line[strlen(line) - 1] = '\0';
9992 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9993 mntpath, line, &last_dev);
10009 /* If a --data was used, add it to the request */
10010 hur->hur_request.hr_data_len = opaque_len;
10012 memcpy(hur_data(hur), opaque, opaque_len);
10014 /* Send the HSM request */
10015 if (realpath(some_file, fullpath) == NULL) {
10016 fprintf(stderr, "Could not find path '%s': %s\n",
10017 some_file, strerror(errno));
10019 rc = llapi_hsm_request(fullpath, hur);
10021 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
10022 some_file, strerror(-rc));
10032 static int lfs_hsm_archive(int argc, char **argv)
10034 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
10037 static int lfs_hsm_restore(int argc, char **argv)
10039 return lfs_hsm_request(argc, argv, HUA_RESTORE);
10042 static int lfs_hsm_release(int argc, char **argv)
10044 return lfs_hsm_request(argc, argv, HUA_RELEASE);
10047 static int lfs_hsm_remove(int argc, char **argv)
10049 return lfs_hsm_request(argc, argv, HUA_REMOVE);
10052 static int lfs_hsm_cancel(int argc, char **argv)
10054 return lfs_hsm_request(argc, argv, HUA_CANCEL);
10057 static int lfs_swap_layouts(int argc, char **argv)
10062 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
10063 SWAP_LAYOUTS_KEEP_MTIME |
10064 SWAP_LAYOUTS_KEEP_ATIME);
10067 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
10069 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
10071 int lfs_get_mode(const char *string)
10073 enum lock_mode_user mode;
10075 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10076 if (lock_mode_names[mode] == NULL)
10078 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10085 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10087 enum lu_ladvise_type advice;
10090 advice < ARRAY_SIZE(ladvise_names); advice++) {
10091 if (ladvise_names[advice] == NULL)
10093 if (strcmp(string, ladvise_names[advice]) == 0)
10097 return LU_LADVISE_INVALID;
10100 static int lfs_ladvise(int argc, char **argv)
10102 struct option long_opts[] = {
10103 { .val = 'a', .name = "advice", .has_arg = required_argument },
10104 { .val = 'b', .name = "background", .has_arg = no_argument },
10105 { .val = 'e', .name = "end", .has_arg = required_argument },
10106 { .val = 'h', .name = "help", .has_arg = no_argument },
10107 { .val = 'l', .name = "length", .has_arg = required_argument },
10108 { .val = 'm', .name = "mode", .has_arg = required_argument },
10109 { .val = 's', .name = "start", .has_arg = required_argument },
10110 { .val = 'u', .name = "unset", .has_arg = no_argument },
10111 { .name = NULL } };
10112 struct llapi_lu_ladvise advice;
10113 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10114 unsigned long long start = 0;
10115 unsigned long long end = LUSTRE_EOF;
10116 unsigned long long length = 0;
10117 unsigned long long size_units;
10118 unsigned long long flags = 0;
10124 while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10125 long_opts, NULL)) != -1) {
10128 advice_type = lfs_get_ladvice(optarg);
10129 if (advice_type == LU_LADVISE_INVALID) {
10131 "%s: invalid advice type '%s'\n",
10133 fprintf(stderr, "Valid types:");
10135 for (advice_type = 0;
10136 advice_type < ARRAY_SIZE(ladvise_names);
10138 if (ladvise_names[advice_type] == NULL)
10140 fprintf(stderr, " %s",
10141 ladvise_names[advice_type]);
10143 fprintf(stderr, "\n");
10156 rc = llapi_parse_size(optarg, &end,
10159 fprintf(stderr, "%s: bad end offset '%s'\n",
10166 rc = llapi_parse_size(optarg, &start,
10170 "%s: bad start offset '%s'\n",
10177 rc = llapi_parse_size(optarg, &length,
10180 fprintf(stderr, "%s: bad length '%s'\n",
10186 mode = lfs_get_mode(optarg);
10189 "%s: bad mode '%s', valid modes are READ or WRITE\n",
10195 fprintf(stderr, "%s: unrecognized option '%s'\n",
10196 progname, argv[optind - 1]);
10203 if (advice_type == LU_LADVISE_INVALID) {
10204 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10205 fprintf(stderr, "Valid types:");
10206 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10208 if (ladvise_names[advice_type] == NULL)
10210 fprintf(stderr, " %s", ladvise_names[advice_type]);
10212 fprintf(stderr, "\n");
10216 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10218 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10223 if (argc <= optind) {
10224 fprintf(stderr, "%s: please give one or more file names\n",
10229 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10230 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10235 if (end == LUSTRE_EOF && length != 0)
10236 end = start + length;
10238 if (end <= start) {
10239 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10240 argv[0], start, end);
10244 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10245 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10250 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10251 fprintf(stderr, "%s: mode is required with lockahead\n",
10256 while (optind < argc) {
10259 path = argv[optind++];
10261 fd = open(path, O_RDONLY);
10263 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10264 argv[0], path, strerror(errno));
10269 advice.lla_start = start;
10270 advice.lla_end = end;
10271 advice.lla_advice = advice_type;
10272 advice.lla_value1 = 0;
10273 advice.lla_value2 = 0;
10274 advice.lla_value3 = 0;
10275 advice.lla_value4 = 0;
10276 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10277 advice.lla_lockahead_mode = mode;
10278 advice.lla_peradvice_flags = flags;
10281 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10285 "%s: cannot give advice '%s' to file '%s': %s\n",
10286 argv[0], ladvise_names[advice_type],
10287 path, strerror(errno));
10293 if (rc == 0 && rc2 < 0)
10299 static const char *const heat_names[] = LU_HEAT_NAMES;
10301 static int lfs_heat_get(int argc, char **argv)
10303 struct lu_heat *heat;
10312 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10314 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10319 while (optind < argc) {
10320 path = argv[optind++];
10322 fd = open(path, O_RDONLY);
10324 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10325 argv[0], path, strerror(errno));
10330 heat->lh_count = OBD_HEAT_COUNT;
10331 rc2 = llapi_heat_get(fd, heat);
10335 "%s: cannot get heat of file '%s': %s\n",
10336 argv[0], path, strerror(errno));
10340 printf("flags: %x\n", heat->lh_flags);
10341 for (i = 0; i < heat->lh_count; i++)
10342 printf("%s: %llu\n", heat_names[i],
10343 (unsigned long long)heat->lh_heat[i]);
10345 if (rc == 0 && rc2 < 0)
10353 static int lfs_heat_set(int argc, char **argv)
10355 struct option long_opts[] = {
10356 { .val = 'c', .name = "clear", .has_arg = no_argument },
10357 { .val = 'h', .name = "help", .has_arg = no_argument },
10358 { .val = 'o', .name = "off", .has_arg = no_argument },
10359 { .val = 'O', .name = "on", .has_arg = no_argument },
10360 { .name = NULL } };
10361 enum lu_heat_flag flags = 0;
10371 while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
10374 flags |= LU_HEAT_FLAG_CLEAR;
10377 flags |= LU_HEAT_FLAG_CLEAR;
10378 flags |= LU_HEAT_FLAG_OFF;
10381 flags &= ~LU_HEAT_FLAG_OFF;
10384 fprintf(stderr, "%s: unrecognized option '%s'\n",
10385 progname, argv[optind - 1]);
10392 if (argc <= optind) {
10393 fprintf(stderr, "%s: please give one or more file names\n",
10398 while (optind < argc) {
10399 path = argv[optind++];
10401 fd = open(path, O_RDONLY);
10403 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10404 argv[0], path, strerror(errno));
10409 rc2 = llapi_heat_set(fd, flags);
10413 "%s: cannot setflags heat of file '%s': %s\n",
10414 argv[0], path, strerror(errno));
10418 if (rc == 0 && rc2 < 0)
10425 * The input string contains a comma delimited list of component ids and
10426 * ranges, for example "1,2-4,7".
10428 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10430 bool end_of_loop = false;
10438 while (!end_of_loop) {
10442 char *endptr = NULL;
10445 ptr = strchrnul(arg, ',');
10446 end_of_loop = *ptr == '\0';
10449 start_index = strtol(arg, &endptr, 0);
10450 if (endptr == arg) /* no data at all */
10452 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10454 if (start_index < 0)
10457 end_index = start_index;
10458 if (*endptr == '-') {
10459 end_index = strtol(endptr + 1, &endptr, 0);
10460 if (*endptr != '\0')
10462 if (end_index < start_index)
10466 for (i = start_index; i <= end_index && size > 0; i++) {
10469 /* remove duplicate */
10470 for (j = 0; j < nr; j++) {
10474 if (j == nr) { /* no duplicate */
10480 if (size == 0 && i < end_index)
10487 if (!end_of_loop && ptr)
10490 return rc < 0 ? rc : nr;
10494 * struct verify_mirror_id - Mirror id to be verified.
10495 * @mirror_id: A specified mirror id.
10496 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10498 struct verify_mirror_id {
10504 * compare_mirror_ids() - Compare mirror ids.
10505 * @layout: Mirror component list.
10506 * @cbdata: Callback data in verify_mirror_id structure.
10508 * This is a callback function called by llapi_layout_comp_iterate()
10509 * to compare the specified mirror id with the one in the current
10510 * component of @layout. If they are the same, then the specified
10511 * mirror id is valid.
10513 * Return: a negative error code on failure or
10514 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10515 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
10518 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10520 struct verify_mirror_id *mirror_id_cbdata =
10521 (struct verify_mirror_id *)cbdata;
10522 uint32_t mirror_id;
10525 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10529 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10530 progname, strerror(errno));
10534 if (mirror_id_cbdata->mirror_id == mirror_id) {
10535 mirror_id_cbdata->is_valid_id = true;
10536 return LLAPI_LAYOUT_ITER_STOP;
10539 return LLAPI_LAYOUT_ITER_CONT;
10543 * verify_mirror_ids() - Verify specified mirror ids.
10544 * @fname: Mirrored file name.
10545 * @mirror_ids: Specified mirror ids to be verified.
10546 * @ids_nr: Number of specified mirror ids.
10548 * This function verifies that specified @mirror_ids are valid
10549 * in the mirrored file @fname.
10551 * Return: 0 on success or a negative error code on failure.
10554 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10556 struct llapi_layout *layout = NULL;
10557 struct verify_mirror_id mirror_id_cbdata = { 0 };
10559 uint32_t flr_state;
10568 if (stat(fname, &stbuf) < 0) {
10569 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10570 progname, fname, strerror(errno));
10575 if (!S_ISREG(stbuf.st_mode)) {
10576 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10582 fd = open(fname, O_DIRECT | O_RDONLY);
10584 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10585 progname, fname, strerror(errno));
10590 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10592 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10593 progname, fname, strerror(errno));
10597 layout = llapi_layout_get_by_fd(fd, 0);
10599 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10600 progname, fname, strerror(errno));
10602 llapi_lease_release(fd);
10606 rc = llapi_layout_flags_get(layout, &flr_state);
10608 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10609 progname, fname, strerror(errno));
10614 flr_state &= LCM_FL_FLR_MASK;
10615 switch (flr_state) {
10618 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10619 progname, fname, llapi_layout_flags_string(flr_state));
10626 for (i = 0; i < ids_nr; i++) {
10627 mirror_id_cbdata.mirror_id = mirror_ids[i];
10628 mirror_id_cbdata.is_valid_id = false;
10630 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10631 &mirror_id_cbdata);
10635 "%s: '%s' failed to verify mirror id: %u.\n",
10636 progname, fname, mirror_ids[i]);
10640 if (!mirror_id_cbdata.is_valid_id) {
10643 "%s: '%s' invalid specified mirror id: %u.\n",
10644 progname, fname, mirror_ids[i]);
10650 llapi_layout_free(layout);
10651 llapi_lease_release(fd);
10659 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10660 __u16 *mirror_ids, int ids_nr)
10662 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10663 struct llapi_layout *layout;
10665 uint32_t flr_state;
10674 if (stat(fname, &stbuf) < 0) {
10675 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10676 progname, fname, strerror(errno));
10680 if (!S_ISREG(stbuf.st_mode)) {
10681 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10687 /* Allow mirror resync even without the key on encrypted files */
10688 fd = open(fname, O_DIRECT | O_RDWR | O_FILE_ENC);
10690 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10691 progname, fname, strerror(errno));
10696 layout = llapi_layout_get_by_fd(fd, 0);
10698 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10699 progname, fname, strerror(errno));
10704 rc = llapi_layout_flags_get(layout, &flr_state);
10706 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10707 progname, fname, strerror(errno));
10712 flr_state &= LCM_FL_FLR_MASK;
10713 if (flr_state == LCM_FL_NONE) {
10715 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10720 /* get stale component info */
10721 comp_size = llapi_mirror_find_stale(layout, comp_array,
10722 ARRAY_SIZE(comp_array),
10723 mirror_ids, ids_nr);
10724 if (comp_size <= 0) {
10729 ioc->lil_mode = LL_LEASE_WRLCK;
10730 ioc->lil_flags = LL_LEASE_RESYNC;
10731 rc = llapi_lease_set(fd, ioc);
10733 if (rc == -EALREADY)
10737 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10738 progname, fname, strerror(-rc));
10742 /* get the read range [start, end) */
10743 start = comp_array[0].lrc_start;
10744 end = comp_array[0].lrc_end;
10745 for (idx = 1; idx < comp_size; idx++) {
10746 if (comp_array[idx].lrc_start < start)
10747 start = comp_array[idx].lrc_start;
10748 if (end < comp_array[idx].lrc_end)
10749 end = comp_array[idx].lrc_end;
10752 rc = llapi_lease_check(fd);
10753 if (rc != LL_LEASE_WRLCK) {
10754 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10759 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10762 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10763 progname, fname, strerror(-rc));
10765 rc = migrate_set_timestamps(fd, &stbuf);
10767 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
10768 progname, fname, strerror(-rc));
10772 /* need to do the lease unlock even resync fails */
10773 ioc->lil_mode = LL_LEASE_UNLCK;
10774 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10775 ioc->lil_count = 0;
10776 for (idx = 0; idx < comp_size; idx++) {
10777 if (comp_array[idx].lrc_synced) {
10778 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10783 rc2 = llapi_lease_set(fd, ioc);
10785 * llapi_lease_set returns lease mode when it request to unlock
10789 /* rc2 == 0 means lost lease lock */
10790 if (rc2 == 0 && rc == 0)
10794 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10796 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10798 llapi_lease_release(fd);
10803 llapi_layout_free(layout);
10810 static inline int lfs_mirror_resync(int argc, char **argv)
10812 struct option long_opts[] = {
10813 { .val = 'h', .name = "help", .has_arg = no_argument },
10814 { .val = 'o', .name = "only", .has_arg = required_argument },
10815 { .name = NULL } };
10816 struct ll_ioc_lease *ioc = NULL;
10817 __u16 mirror_ids[128] = { 0 };
10822 while ((c = getopt_long(argc, argv, "ho:", long_opts, NULL)) >= 0) {
10825 rc = parse_mirror_ids(mirror_ids,
10826 sizeof(mirror_ids) / sizeof(__u16),
10830 "%s: bad mirror ids '%s'.\n",
10837 fprintf(stderr, "%s: unrecognized option '%s'\n",
10838 progname, argv[optind - 1]);
10846 if (argc == optind) {
10847 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10852 if (ids_nr > 0 && argc > optind + 1) {
10854 "%s: option '--only' cannot be used upon multiple files.\n",
10861 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10866 /* set the lease on the file */
10867 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10869 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10870 argv[0], strerror(errno));
10875 for (; optind < argc; optind++) {
10876 rc = lfs_mirror_resync_file(argv[optind], ioc,
10877 mirror_ids, ids_nr);
10878 /* ignore previous file's error, continue with next file */
10881 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10889 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10891 struct llapi_layout *layout;
10894 layout = llapi_layout_get_by_fd(fd, 0);
10896 fprintf(stderr, "could not get layout.\n");
10900 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10902 fprintf(stderr, "failed to iterate layout\n");
10903 llapi_layout_free(layout);
10906 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10907 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10908 llapi_layout_free(layout);
10912 llapi_layout_free(layout);
10918 * Check whether two files are the same file
10919 * \retval 0 same file
10920 * \retval 1 not the same file
10921 * \retval <0 error code
10923 static inline int check_same_file(int fd, const char *f2)
10925 struct stat stbuf1;
10926 struct stat stbuf2;
10928 if (fstat(fd, &stbuf1) < 0)
10931 if (stat(f2, &stbuf2) < 0)
10934 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10935 stbuf1.st_ino == stbuf2.st_ino)
10941 static inline int lfs_mirror_read(int argc, char **argv)
10944 __u16 mirror_id = 0;
10945 const char *outfile = NULL;
10951 const size_t buflen = 4 << 20;
10953 struct option long_opts[] = {
10954 { .val = 'h', .name = "help", .has_arg = no_argument },
10955 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10956 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10957 { .name = NULL } };
10959 while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
10964 unsigned long int id;
10967 id = strtoul(optarg, &end, 0);
10968 if (errno != 0 || *end != '\0' || id == 0 ||
10971 "%s %s: invalid mirror ID '%s'\n",
10972 progname, argv[0], optarg);
10976 mirror_id = (__u16)id;
10983 fprintf(stderr, "%s: unrecognized option '%s'\n",
10984 progname, argv[optind - 1]);
10991 if (argc == optind) {
10992 fprintf(stderr, "%s %s: no mirrored file provided\n",
10993 progname, argv[0]);
10995 } else if (argc > optind + 1) {
10996 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11000 if (mirror_id == 0) {
11001 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11002 progname, argv[0]);
11006 /* open mirror file */
11007 fname = argv[optind];
11008 fd = open(fname, O_DIRECT | O_RDONLY);
11010 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11011 progname, argv[0], fname, strerror(errno));
11015 /* verify mirror id */
11016 rc = verify_mirror_id_by_fd(fd, mirror_id);
11019 "%s %s: cannot find mirror with ID %u in '%s'\n",
11020 progname, argv[0], mirror_id, fname);
11024 /* open output file - O_EXCL ensures output is not the same as input */
11026 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
11028 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
11029 progname, argv[0], outfile, strerror(errno));
11034 outfd = STDOUT_FILENO;
11037 /* allocate buffer */
11038 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
11040 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11041 progname, argv[0], rc);
11047 ssize_t bytes_read;
11048 ssize_t written = 0;
11050 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
11051 if (bytes_read < 0) {
11054 "%s %s: fail to read data from mirror %u: %s\n",
11055 progname, argv[0], mirror_id, strerror(-rc));
11060 if (bytes_read == 0)
11063 while (written < bytes_read) {
11066 written2 = write(outfd, buf + written,
11067 bytes_read - written);
11068 if (written2 < 0) {
11070 "%s %s: fail to write %s: %s\n",
11071 progname, argv[0], outfile ? : "STDOUT",
11076 written += written2;
11079 if (written != bytes_read) {
11081 "%s %s: written %ld bytes does not match with %ld read.\n",
11082 progname, argv[0], written, bytes_read);
11104 static inline int lfs_mirror_write(int argc, char **argv)
11107 __u16 mirror_id = 0;
11108 const char *inputfile = NULL;
11114 const size_t buflen = 4 << 20;
11116 size_t page_size = sysconf(_SC_PAGESIZE);
11117 struct ll_ioc_lease_id ioc;
11118 struct option long_opts[] = {
11119 { .val = 'h', .name = "help", .has_arg = no_argument },
11120 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
11121 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11122 { .name = NULL } };
11124 while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11129 unsigned long int id;
11132 id = strtoul(optarg, &end, 0);
11133 if (errno != 0 || *end != '\0' || id == 0 ||
11136 "%s %s: invalid mirror ID '%s'\n",
11137 progname, argv[0], optarg);
11141 mirror_id = (__u16)id;
11145 inputfile = optarg;
11148 fprintf(stderr, "%s: unrecognized option '%s'\n",
11149 progname, argv[optind - 1]);
11156 if (argc == optind) {
11157 fprintf(stderr, "%s %s: no mirrored file provided\n",
11158 progname, argv[0]);
11160 } else if (argc > optind + 1) {
11161 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11165 if (mirror_id == 0) {
11166 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11167 progname, argv[0]);
11171 /* open mirror file */
11172 fname = argv[optind];
11173 fd = open(fname, O_DIRECT | O_WRONLY);
11175 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11176 progname, argv[0], fname, strerror(errno));
11180 /* verify mirror id */
11181 rc = verify_mirror_id_by_fd(fd, mirror_id);
11184 "%s %s: cannot find mirror with ID %u in '%s'\n",
11185 progname, argv[0], mirror_id, fname);
11189 /* open input file */
11191 rc = check_same_file(fd, inputfile);
11194 "%s %s: input file cannot be the mirrored file\n",
11195 progname, argv[0]);
11201 inputfd = open(inputfile, O_RDONLY, 0644);
11203 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11204 progname, argv[0], inputfile, strerror(errno));
11209 inputfd = STDIN_FILENO;
11212 /* allocate buffer */
11213 rc = posix_memalign(&buf, page_size, buflen);
11215 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11216 progname, argv[0], rc);
11217 goto close_inputfd;
11220 /* prepare target mirror components instantiation */
11221 ioc.lil_mode = LL_LEASE_WRLCK;
11222 ioc.lil_flags = LL_LEASE_RESYNC;
11223 ioc.lil_mirror_id = mirror_id;
11224 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11227 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11228 progname, argv[0], fname, strerror(errno));
11234 ssize_t bytes_read;
11238 rc = llapi_lease_check(fd);
11239 if (rc != LL_LEASE_WRLCK) {
11240 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11241 progname, argv[0], fname);
11245 bytes_read = read(inputfd, buf, buflen);
11246 if (bytes_read < 0) {
11249 "%s %s: fail to read data from '%s': %s\n",
11250 progname, argv[0], inputfile ? : "STDIN",
11257 if (bytes_read == 0)
11260 /* round up to page align to make direct IO happy. */
11261 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11263 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11268 "%s %s: fail to write to mirror %u: %s\n",
11269 progname, argv[0], mirror_id,
11277 if (pos & (page_size - 1)) {
11278 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11283 ioc.lil_mode = LL_LEASE_UNLCK;
11284 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11286 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11291 "%s %s: release lease lock of '%s' failed: %s\n",
11292 progname, argv[0], fname, strerror(errno));
11309 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11311 struct llapi_layout *layout;
11312 struct collect_ids_data cid = { .cid_ids = ids,
11314 .cid_exclude = exclude_id, };
11317 layout = llapi_layout_get_by_fd(fd, 0);
11319 fprintf(stderr, "could not get layout\n");
11323 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11325 fprintf(stderr, "failed to iterate layout\n");
11326 llapi_layout_free(layout);
11330 llapi_layout_free(layout);
11332 return cid.cid_count;
11335 #ifndef MIRROR_ID_NEG
11336 #define MIRROR_ID_NEG 0x8000
11339 static inline int lfs_mirror_copy(int argc, char **argv)
11342 __u16 read_mirror_id = 0;
11343 __u16 ids[128] = { 0 };
11345 struct llapi_layout *layout = NULL;
11346 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11353 struct ll_ioc_lease *ioc = NULL;
11354 struct ll_ioc_lease_id *resync_ioc;
11355 struct option long_opts[] = {
11356 { .val = 'h', .name = "help", .has_arg = no_argument },
11357 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
11358 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
11359 { .name = NULL } };
11360 char cmd[PATH_MAX];
11362 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11364 while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
11369 unsigned long int id;
11372 id = strtoul(optarg, &end, 0);
11373 if (errno != 0 || *end != '\0' || id == 0 ||
11376 "%s: invalid read mirror ID '%s'\n",
11381 read_mirror_id = (__u16)id;
11385 if (!strcmp(optarg, "-1")) {
11386 /* specify all other mirrors */
11387 ids[0] = (__u16)-1;
11390 count = parse_mirror_ids((__u16 *)ids,
11398 fprintf(stderr, "%s: unrecognized option '%s'\n",
11399 progname, argv[optind - 1]);
11406 if (argc == optind) {
11407 fprintf(stderr, "%s %s: no mirrored file provided\n",
11408 progname, argv[0]);
11410 } else if (argc > optind + 1) {
11411 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11415 if (read_mirror_id == 0) {
11417 "%s %s: no valid read mirror ID %d is provided\n",
11418 progname, argv[0], read_mirror_id);
11424 "%s %s: no write mirror ID is provided\n",
11425 progname, argv[0]);
11429 for (i = 0; i < count; i++) {
11430 if (read_mirror_id == ids[i]) {
11432 "%s %s: read and write mirror ID cannot be the same\n",
11433 progname, argv[0]);
11438 /* open mirror file */
11439 fname = argv[optind];
11441 fd = open(fname, O_DIRECT | O_RDWR);
11443 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11444 progname, argv[0], fname, strerror(errno));
11448 /* write to all other mirrors */
11449 if (ids[0] == (__u16)-1) {
11450 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11454 "%s %s: failed to get other mirror ids in '%s': %d\n",
11455 progname, argv[0], fname, rc);
11460 /* verify mirror id */
11461 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11464 "%s %s: cannot find mirror with ID %u in '%s'\n",
11465 progname, argv[0], read_mirror_id, fname);
11469 for (i = 0; i < count; i++) {
11470 rc = verify_mirror_id_by_fd(fd, ids[i]);
11473 "%s %s: cannot find mirror with ID %u in '%s'\n",
11474 progname, argv[0], ids[i], fname);
11479 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11482 "%s %s: cannot alloc comp id array for ioc: %s\n",
11483 progname, argv[0], strerror(errno));
11488 /* get stale component info */
11489 layout = llapi_layout_get_by_fd(fd, 0);
11491 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11492 progname, argv[0], fname, strerror(errno));
11496 comp_size = llapi_mirror_find_stale(layout, comp_array,
11497 ARRAY_SIZE(comp_array),
11499 llapi_layout_free(layout);
11500 if (comp_size < 0) {
11505 /* prepare target mirror components instantiation */
11506 resync_ioc = (struct ll_ioc_lease_id *)ioc;
11507 resync_ioc->lil_mode = LL_LEASE_WRLCK;
11508 resync_ioc->lil_flags = LL_LEASE_RESYNC;
11510 resync_ioc->lil_mirror_id = ids[0];
11512 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11513 rc = llapi_lease_set(fd, ioc);
11516 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11517 progname, argv[0], fname, strerror(errno));
11521 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11524 fprintf(stderr, "%s %s: copy error: %d\n",
11525 progname, argv[0], rc);
11529 fprintf(stdout, "mirror copied successfully: ");
11530 for (i = 0; i < copied; i++)
11531 fprintf(stdout, "%d ", ids[i]);
11532 fprintf(stdout, "\n");
11534 ioc->lil_mode = LL_LEASE_UNLCK;
11535 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11536 ioc->lil_count = 0;
11537 for (i = 0; i < comp_size; i++) {
11540 for (j = 0; j < copied; j++) {
11541 if (comp_array[i].lrc_mirror_id != ids[j])
11544 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11548 rc = llapi_lease_set(fd, ioc);
11553 "%s %s: release lease lock of '%s' failed: %s\n",
11554 progname, argv[0], fname, strerror(errno));
11569 * struct verify_chunk - Mirror chunk to be verified.
11570 * @chunk: [start, end) of the chunk.
11571 * @mirror_count: Number of mirror ids in @mirror_id array.
11572 * @mirror_id: Array of valid mirror ids that cover the chunk.
11574 struct verify_chunk {
11575 struct lu_extent chunk;
11576 unsigned int mirror_count;
11577 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11581 * print_chunks() - Print chunk information.
11582 * @fname: Mirrored file name.
11583 * @chunks: Array of chunks.
11584 * @chunk_count: Number of chunks in @chunks array.
11586 * This function prints [start, end) of each chunk in @chunks
11587 * for mirrored file @fname, and also prints the valid mirror ids
11588 * that cover the chunk.
11593 void print_chunks(const char *fname, struct verify_chunk *chunks,
11599 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11600 for (i = 0; i < chunk_count; i++) {
11601 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11603 if (chunks[i].mirror_count == 0)
11604 fprintf(stdout, "\t[");
11606 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11607 for (j = 1; j < chunks[i].mirror_count; j++)
11608 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11610 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11612 fprintf(stdout, "\n");
11616 * print_checksums() - Print CRC-32 checksum values.
11617 * @chunk: A chunk and its corresponding valid mirror ids.
11618 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
11620 * This function prints CRC-32 checksum values on @chunk for
11621 * each valid mirror that covers it.
11626 void print_checksums(struct verify_chunk *chunk, unsigned long *crc,
11627 unsigned long long pos, unsigned long long len)
11632 "CRC-32 checksum value for chunk "DEXT":\n", pos, pos + len);
11633 for (i = 0; i < chunk->mirror_count; i++)
11634 fprintf(stdout, "Mirror %u:\t%#lx\n",
11635 chunk->mirror_id[i], crc[i]);
11636 fprintf(stdout, "\n");
11640 * filter_mirror_id() - Filter specified mirror ids.
11641 * @chunks: Array of chunks.
11642 * @chunk_count: Number of chunks in @chunks array.
11643 * @mirror_ids: Specified mirror ids to be verified.
11644 * @ids_nr: Number of specified mirror ids.
11646 * This function scans valid mirror ids that cover each chunk in @chunks
11647 * and filters specified mirror ids.
11652 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11653 __u16 *mirror_ids, int ids_nr)
11658 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11659 unsigned int valid_count = 0;
11661 for (i = 0; i < chunk_count; i++) {
11662 if (chunks[i].mirror_count == 0)
11666 for (j = 0; j < ids_nr; j++) {
11667 for (k = 0; k < chunks[i].mirror_count; k++) {
11668 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11669 valid_id[valid_count] = mirror_ids[j];
11676 memcpy(chunks[i].mirror_id, valid_id,
11677 sizeof(__u16) * valid_count);
11678 chunks[i].mirror_count = valid_count;
11683 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11684 * @layout: Mirror component list.
11685 * @chunks: Array of chunks.
11686 * @chunks_size: Array size of @chunks.
11688 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11689 * to find out chunk segments and store them in @chunks array.
11691 * The @mirror_id array in each element of @chunks will store the valid
11692 * mirror ids that cover the chunk. If a mirror component covering the
11693 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11694 * will not be stored into the @mirror_id array, and the chunk for that
11695 * mirror will not be verified.
11697 * The @mirror_count in each element of @chunks will store the number of
11698 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11699 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11700 * indicates the chunk is valid in only one mirror. In both cases, the
11701 * chunk will not be verified.
11703 * Here is an example:
11705 * 0 1M 2M 3M 4M EOF
11706 * +------+-------------+--------------------+
11707 * | | | S | mirror1
11708 * +------+------+------+------+-------------+
11709 * | | S | S | | mirror2
11710 * +-------------+------+------+-------------+
11712 * prepared @chunks array will contain 5 elements:
11713 * (([0, 1M), [1, 2], 2),
11714 * ([1M, 2M), [1, 2], 2),
11715 * ([2M, 3M), [1], 1),
11716 * ([3M, 4M], [], 0),
11717 * ([4M, EOF), [2], 1))
11719 * Return: the actual array size of @chunks on success
11720 * or a negative error code on failure.
11723 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11724 struct verify_chunk *chunks,
11725 size_t chunks_size)
11729 uint32_t mirror_id;
11735 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11738 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11741 "%s: move to the first layout component: %s.\n",
11742 progname, strerror(errno));
11748 chunks[idx].chunk.e_end = LUSTRE_EOF;
11750 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11753 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11754 progname, strerror(errno));
11758 if (start > chunks[idx].chunk.e_start ||
11759 end <= chunks[idx].chunk.e_start)
11762 if (end < chunks[idx].chunk.e_end)
11763 chunks[idx].chunk.e_end = end;
11765 rc = llapi_layout_comp_flags_get(layout, &flags);
11768 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11769 progname, strerror(errno));
11773 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11776 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11779 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11780 progname, strerror(errno));
11784 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11786 "%s: mirror_id array is too small.\n",
11791 chunks[idx].mirror_id[i] = mirror_id;
11795 rc = llapi_layout_comp_use(layout,
11796 LLAPI_LAYOUT_COMP_USE_NEXT);
11799 "%s: move to the next layout component: %s.\n",
11800 progname, strerror(errno));
11803 } /* loop through all components */
11805 chunks[idx].mirror_count = i;
11807 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11811 if (idx >= chunks_size) {
11812 fprintf(stderr, "%s: chunks array is too small.\n",
11818 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11822 return rc < 0 ? rc : idx + 1;
11826 * lfs_mirror_verify_chunk() - Verify a chunk.
11827 * @fd: File descriptor of the mirrored file.
11828 * @file_size: Size of the mirrored file.
11829 * @chunk: A chunk and its corresponding valid mirror ids.
11830 * @verbose: Verbose mode.
11832 * This function verifies a @chunk contains exactly the same data
11833 * ammong the mirrors that cover it.
11835 * If @verbose is specified, then the function will print where the
11836 * differences are if the data do not match. Otherwise, it will
11837 * just return an error in that case.
11839 * Return: 0 on success or a negative error code on failure.
11842 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11843 struct verify_chunk *chunk, int verbose)
11845 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11847 size_t page_size = sysconf(_SC_PAGESIZE);
11848 ssize_t bytes_read;
11849 ssize_t bytes_done;
11853 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11857 if (file_size == 0)
11860 rc = posix_memalign(&buf, page_size, buflen);
11861 if (rc) /* error code is returned directly */
11865 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11866 PEXT(&chunk->chunk));
11867 for (i = 0; i < chunk->mirror_count; i++)
11868 fprintf(stdout, " %u", chunk->mirror_id[i]);
11869 fprintf(stdout, "\n");
11873 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11874 pos = chunk->chunk.e_start;
11875 while (bytes_done < count) {
11876 /* compute initial CRC-32 checksum */
11877 crc = crc32(0L, Z_NULL, 0);
11878 memset(crc_array, 0, sizeof(crc_array));
11881 for (i = 0; i < chunk->mirror_count; i++) {
11882 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11884 if (bytes_read < 0) {
11887 "%s: failed to read data from mirror %u: %s.\n",
11888 progname, chunk->mirror_id[i],
11893 /* compute new CRC-32 checksum */
11894 crc_array[i] = crc32(crc, buf, bytes_read);
11898 print_checksums(chunk, crc_array, pos, buflen);
11900 /* compare CRC-32 checksum values */
11901 for (i = 1; i < chunk->mirror_count; i++) {
11902 if (crc_array[i] != crc_array[0]) {
11906 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11907 progname, PEXT(&chunk->chunk),
11908 chunk->mirror_id[0],
11909 chunk->mirror_id[i]);
11914 bytes_done += bytes_read;
11917 if (verbose > 1 && rc == 0) {
11918 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11919 PEXT(&chunk->chunk));
11920 for (i = 0; i < chunk->mirror_count; i++)
11921 fprintf(stdout, " %u", chunk->mirror_id[i]);
11922 fprintf(stdout, " PASS\n\n");
11931 * lfs_mirror_verify_file() - Verify a mirrored file.
11932 * @fname: Mirrored file name.
11933 * @mirror_ids: Specified mirror ids to be verified.
11934 * @ids_nr: Number of specified mirror ids.
11935 * @verbose: Verbose mode.
11937 * This function verifies that each SYNC mirror of a mirrored file
11938 * specified by @fname contains exactly the same data.
11940 * If @mirror_ids is specified, then the function will verify the
11941 * mirrors specified by @mirror_ids contain exactly the same data.
11943 * If @verbose is specified, then the function will print where the
11944 * differences are if the data do not match. Otherwise, it will
11945 * just return an error in that case.
11947 * Return: 0 on success or a negative error code on failure.
11950 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11953 struct verify_chunk chunks_array[1024] = { };
11954 struct llapi_layout *layout = NULL;
11956 uint32_t flr_state;
11958 int chunk_count = 0;
11964 if (stat(fname, &stbuf) < 0) {
11965 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11966 progname, fname, strerror(errno));
11971 if (!S_ISREG(stbuf.st_mode)) {
11972 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11978 if (stbuf.st_size == 0) {
11980 fprintf(stdout, "%s: '%s' file size is 0.\n",
11986 /* Allow mirror verify even without the key on encrypted files */
11987 fd = open(fname, O_DIRECT | O_RDONLY | O_FILE_ENC);
11989 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11990 progname, fname, strerror(errno));
11995 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11997 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11998 progname, fname, strerror(errno));
12002 layout = llapi_layout_get_by_fd(fd, 0);
12004 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
12005 progname, fname, strerror(errno));
12007 llapi_lease_release(fd);
12011 rc = llapi_layout_flags_get(layout, &flr_state);
12013 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
12014 progname, fname, strerror(errno));
12019 flr_state &= LCM_FL_FLR_MASK;
12020 switch (flr_state) {
12023 fprintf(stderr, "%s: '%s' file state error: %s.\n",
12024 progname, fname, llapi_layout_flags_string(flr_state));
12030 /* find out mirror chunks to be verified */
12031 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
12032 ARRAY_SIZE(chunks_array));
12033 if (chunk_count < 0) {
12039 /* filter specified mirror ids */
12040 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
12043 print_chunks(fname, chunks_array, chunk_count);
12045 for (idx = 0; idx < chunk_count; idx++) {
12046 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
12049 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
12051 PEXT(&chunks_array[idx].chunk),
12052 (unsigned long long)stbuf.st_size);
12056 if (chunks_array[idx].mirror_count == 0) {
12058 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
12060 PEXT(&chunks_array[idx].chunk));
12062 fprintf(stderr, "skipped\n");
12066 fprintf(stderr, "failed\n");
12070 if (chunks_array[idx].mirror_count == 1) {
12073 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12075 PEXT(&chunks_array[idx].chunk),
12076 chunks_array[idx].mirror_id[0]);
12080 rc = llapi_lease_check(fd);
12081 if (rc != LL_LEASE_RDLCK) {
12082 fprintf(stderr, "%s: '%s' lost lease lock.\n",
12087 /* verify one chunk */
12088 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12089 &chunks_array[idx], verbose);
12103 llapi_layout_free(layout);
12104 llapi_lease_release(fd);
12112 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12113 * @argc: The count of lfs mirror verify command line arguments.
12114 * @argv: Array of strings for lfs mirror verify command line arguments.
12116 * This function parses lfs mirror verify command and verifies the
12117 * specified mirrored file(s).
12119 * Return: 0 on success or a negative error code on failure.
12121 static inline int lfs_mirror_verify(int argc, char **argv)
12123 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12129 char cmd[PATH_MAX];
12131 struct option long_opts[] = {
12132 { .val = 'h', .name = "help", .has_arg = no_argument },
12133 { .val = 'o', .name = "only", .has_arg = required_argument },
12134 { .val = 'v', .name = "verbose", .has_arg = no_argument },
12135 { .name = NULL } };
12137 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12139 while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12142 rc = parse_mirror_ids(mirror_ids,
12143 ARRAY_SIZE(mirror_ids),
12147 "%s: bad mirror ids '%s'.\n",
12154 "%s: at least 2 mirror ids needed with '--only' option.\n",
12164 fprintf(stderr, "%s: unrecognized option '%s'\n",
12165 progname, argv[optind - 1]);
12173 if (argc == optind) {
12174 fprintf(stderr, "%s: no file name given.\n", progname);
12179 if (ids_nr > 0 && argc > optind + 1) {
12181 "%s: '--only' cannot be used upon multiple files.\n",
12188 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12194 for (; optind < argc; optind++) {
12195 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12205 * lfs_mirror() - Parse and execute lfs mirror commands.
12206 * @argc: The count of lfs mirror command line arguments.
12207 * @argv: Array of strings for lfs mirror command line arguments.
12209 * This function parses lfs mirror commands and performs the
12210 * corresponding functions specified in mirror_cmdlist[].
12212 * Return: 0 on success or an error code on failure.
12214 static int lfs_mirror(int argc, char **argv)
12216 char cmd[PATH_MAX];
12219 setlinebuf(stdout);
12221 Parser_init("lfs-mirror > ", mirror_cmdlist);
12223 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12225 program_invocation_short_name = cmd;
12227 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12229 rc = Parser_commands();
12231 return rc < 0 ? -rc : rc;
12234 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12236 #if __BYTE_ORDER == __BIG_ENDIAN
12237 __swab16s(&attrs->lsa_valid);
12238 __swab64s(&attrs->lsa_size);
12239 __swab64s(&attrs->lsa_blocks);
12243 enum lfs_som_type {
12244 LFS_SOM_SIZE = 0x1,
12245 LFS_SOM_BLOCKS = 0x2,
12246 LFS_SOM_FLAGS = 0x4,
12247 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12251 static int lfs_getsom(int argc, char **argv)
12254 struct lustre_som_attrs *attrs;
12255 char buf[sizeof(*attrs) + 64];
12256 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12259 while ((c = getopt(argc, argv, "bfhs")) != -1) {
12262 type = LFS_SOM_BLOCKS;
12265 type = LFS_SOM_FLAGS;
12268 type = LFS_SOM_SIZE;
12271 fprintf(stderr, "%s: unrecognized option '%s'\n",
12272 progname, argv[optind - 1]);
12283 fprintf(stderr, "%s: %s\n",
12284 progname, argc == 0 ? "miss file target" :
12285 "input more than 2 files");
12290 attrs = (void *)buf;
12291 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12294 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12295 argv[0], strerror(errno), errno);
12299 lustre_som_swab(attrs);
12302 case LFS_SOM_ATTR_ALL:
12303 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12304 path, (unsigned long long)attrs->lsa_size,
12305 (unsigned long long)attrs->lsa_blocks,
12309 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12311 case LFS_SOM_BLOCKS:
12312 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12314 case LFS_SOM_FLAGS:
12315 printf("%x\n", attrs->lsa_valid);
12318 fprintf(stderr, "%s: unknown option\n", progname);
12326 * lfs_mirror_list_commands() - List lfs mirror commands.
12327 * @argc: The count of command line arguments.
12328 * @argv: Array of strings for command line arguments.
12330 * This function lists lfs mirror commands defined in mirror_cmdlist[].
12332 * Return: 0 on success.
12334 static int lfs_mirror_list_commands(int argc, char **argv)
12336 char buffer[81] = "";
12338 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12344 static int lfs_pcc_attach(int argc, char **argv)
12346 struct option long_opts[] = {
12347 { .val = 'h', .name = "help", .has_arg = no_argument },
12348 { .val = 'i', .name = "id", .has_arg = required_argument },
12349 { .name = NULL } };
12352 __u32 archive_id = 0;
12355 char fullpath[PATH_MAX];
12356 enum lu_pcc_type type = LU_PCC_READWRITE;
12359 while ((c = getopt_long(argc, argv, "hi:",
12360 long_opts, NULL)) != -1) {
12364 archive_id = strtoul(optarg, &end, 0);
12365 if (errno != 0 || *end != '\0' ||
12366 archive_id == 0 || archive_id > UINT32_MAX) {
12368 "error: %s: bad archive ID '%s'\n",
12374 fprintf(stderr, "%s: unrecognized option '%s'\n",
12375 progname, argv[optind - 1]);
12382 if (archive_id == 0) {
12383 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12387 if (argc <= optind) {
12388 fprintf(stderr, "%s: must specify one or more file names\n",
12393 while (optind < argc) {
12396 path = argv[optind++];
12397 if (!realpath(path, fullpath)) {
12398 fprintf(stderr, "%s: could not find path '%s': %s\n",
12399 argv[0], path, strerror(errno));
12405 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12408 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12409 argv[0], path, archive_id, strerror(-rc2));
12417 static int lfs_pcc_attach_fid(int argc, char **argv)
12419 struct option long_opts[] = {
12420 { .val = 'h', .name = "help", .has_arg = no_argument },
12421 { .val = 'i', .name = "id", .has_arg = required_argument },
12422 { .val = 'm', .name = "mnt", .has_arg = required_argument },
12423 { .name = NULL } };
12426 __u32 archive_id = 0;
12428 const char *mntpath = NULL;
12429 const char *fidstr;
12430 enum lu_pcc_type type = LU_PCC_READWRITE;
12433 while ((c = getopt_long(argc, argv, "hi:m:",
12434 long_opts, NULL)) != -1) {
12438 archive_id = strtoul(optarg, &end, 0);
12439 if (errno != 0 || *end != '\0' ||
12440 archive_id > UINT32_MAX) {
12442 "error: %s: bad archive ID '%s'\n",
12451 fprintf(stderr, "%s: unrecognized option '%s'\n",
12452 progname, argv[optind - 1]);
12459 if (archive_id == 0) {
12460 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12465 fprintf(stderr, "%s: must specify Lustre mount point\n",
12470 if (argc <= optind) {
12471 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12475 while (optind < argc) {
12478 fidstr = argv[optind++];
12480 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12484 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12485 argv[0], fidstr, mntpath, archive_id,
12488 if (rc == 0 && rc2 < 0)
12494 static int lfs_pcc_detach(int argc, char **argv)
12496 struct option long_opts[] = {
12497 { .val = 'h', .name = "help", .has_arg = no_argument },
12498 { .val = 'k', .name = "keep", .has_arg = no_argument },
12499 { .name = NULL } };
12503 char fullpath[PATH_MAX];
12504 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12507 while ((c = getopt_long(argc, argv, "hk",
12508 long_opts, NULL)) != -1) {
12511 detach_opt = PCC_DETACH_OPT_NONE;
12514 fprintf(stderr, "%s: unrecognized option '%s'\n",
12515 progname, argv[optind - 1]);
12522 while (optind < argc) {
12525 path = argv[optind++];
12526 if (!realpath(path, fullpath)) {
12527 fprintf(stderr, "%s: could not find path '%s': %s\n",
12528 argv[0], path, strerror(errno));
12534 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12538 "%s: cannot detach '%s' from PCC: %s\n",
12539 argv[0], path, strerror(errno));
12547 static int lfs_pcc_detach_fid(int argc, char **argv)
12549 struct option long_opts[] = {
12550 { .val = 'h', .name = "help", .has_arg = no_argument },
12551 { .val = 'k', .name = "keep", .has_arg = no_argument },
12552 { .name = NULL } };
12556 const char *mntpath;
12557 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12560 while ((c = getopt_long(argc, argv, "hk",
12561 long_opts, NULL)) != -1) {
12564 detach_opt = PCC_DETACH_OPT_NONE;
12567 fprintf(stderr, "%s: unrecognized option '%s'\n",
12568 progname, argv[optind - 1]);
12575 mntpath = argv[optind++];
12577 while (optind < argc) {
12580 fid = argv[optind++];
12582 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12585 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12586 argv[0], fid, mntpath, strerror(-rc2));
12594 static int lfs_pcc_state(int argc, char **argv)
12598 char fullpath[PATH_MAX];
12599 struct lu_pcc_state state;
12604 fprintf(stderr, "%s: must specify one or more file names\n",
12609 while (optind < argc) {
12612 path = argv[optind++];
12613 if (!realpath(path, fullpath)) {
12614 fprintf(stderr, "%s: could not find path '%s': %s\n",
12615 argv[0], path, strerror(errno));
12621 rc2 = llapi_pcc_state_get(fullpath, &state);
12626 "%s: cannot get PCC state of '%s': %s\n",
12627 argv[0], path, strerror(-rc2));
12631 printf("file: %s", path);
12632 printf(", type: %s", pcc_type2string(state.pccs_type));
12633 if (state.pccs_type == LU_PCC_NONE &&
12634 state.pccs_open_count == 0) {
12639 printf(", PCC file: %s", state.pccs_path);
12640 printf(", user number: %u", state.pccs_open_count);
12641 printf(", flags: %x", state.pccs_flags);
12648 * lfs_pcc_list_commands() - List lfs pcc commands.
12649 * @argc: The count of command line arguments.
12650 * @argv: Array of strings for command line arguments.
12652 * This function lists lfs pcc commands defined in pcc_cmdlist[].
12654 * Return: 0 on success.
12656 static int lfs_pcc_list_commands(int argc, char **argv)
12658 char buffer[81] = "";
12660 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12667 * lfs_pcc() - Parse and execute lfs pcc commands.
12668 * @argc: The count of lfs pcc command line arguments.
12669 * @argv: Array of strings for lfs pcc command line arguments.
12671 * This function parses lfs pcc commands and performs the
12672 * corresponding functions specified in pcc_cmdlist[].
12674 * Return: 0 on success or an error code on failure.
12676 static int lfs_pcc(int argc, char **argv)
12678 char cmd[PATH_MAX];
12681 setlinebuf(stdout);
12683 Parser_init("lfs-pcc > ", pcc_cmdlist);
12685 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12687 program_invocation_short_name = cmd;
12689 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12691 rc = Parser_commands();
12693 return rc < 0 ? -rc : rc;
12696 static int lfs_list_commands(int argc, char **argv)
12698 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12700 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12705 int main(int argc, char **argv)
12709 /* Ensure that liblustreapi constructor has run */
12710 if (!llapi_liblustreapi_initialized())
12711 fprintf(stderr, "liblustreapi was not properly initialized\n");
12713 setlinebuf(stdout);
12716 Parser_init("lfs > ", cmdlist);
12718 progname = program_invocation_short_name; /* Used in error messages */
12720 llapi_set_command_name(argv[1]);
12721 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12722 llapi_clear_command_name();
12724 rc = Parser_commands();
12727 return rc < 0 ? -rc : rc;
12730 #ifdef _LUSTRE_IDL_H_
12731 /* Everything we need here should be included by lustreapi.h. */
12732 # error "lfs should not depend on lustre_idl.h"
12733 #endif /* _LUSTRE_IDL_H_ */