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,
138 struct llapi_stripe_param *param,
139 struct llapi_layout *layout);
141 struct pool_to_id_cbdata {
145 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
146 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
148 enum setstripe_origin {
158 static int lfs_setstripe_internal(int argc, char **argv,
159 enum setstripe_origin opc);
161 static inline int lfs_setstripe(int argc, char **argv)
163 return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
166 static inline int lfs_setstripe_migrate(int argc, char **argv)
168 return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
171 static inline int lfs_mirror_create(int argc, char **argv)
173 return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
176 static inline int lfs_mirror_extend(int argc, char **argv)
178 return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
181 static inline int lfs_mirror_split(int argc, char **argv)
183 return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
186 static inline int lfs_mirror_delete(int argc, char **argv)
188 return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
191 /* Setstripe and migrate share mostly the same parameters */
192 #define SSM_CMD_COMMON(cmd) \
193 "usage: "cmd" [--component-end|-E COMP_END]\n" \
194 " [--copy=LUSTRE_SRC]\n" \
195 " [--extension-size|--ext-size|-z SIZE]\n" \
196 " [--help|-h] [--layout|-L PATTERN]\n" \
197 " [--layout|-L PATTERN]\n" \
198 " [--mirror-count|-N[MIRROR_COUNT]]\n" \
199 " [--ost|-o OST_INDICES]\n" \
200 " [--overstripe-count|-C STRIPE_COUNT]\n" \
201 " [--pool|-p POOL_NAME]\n" \
202 " [--stripe-count|-c STRIPE_COUNT]\n" \
203 " [--stripe-index|-i START_OST_IDX]\n" \
204 " [--stripe-size|-S STRIPE_SIZE]\n" \
205 " [--yaml|-y YAML_TEMPLATE_FILE]\n"
207 #define MIRROR_EXTEND_USAGE \
208 " {--mirror-count|-N[MIRROR_COUNT]}\n" \
209 " [SETSTRIPE_OPTIONS|-f|--file VICTIM_FILE]\n" \
212 #define SETSTRIPE_USAGE \
213 SSM_CMD_COMMON("setstripe") \
214 MIRROR_EXTEND_USAGE \
215 " DIRECTORY|FILENAME\n"
217 #define MIGRATE_USAGE \
218 SSM_CMD_COMMON("migrate ") \
219 " [--block|-b] [--non-block|-n]\n" \
220 " [--non-direct|-D] [--verbose|-v]\n" \
223 #define SETDIRSTRIPE_USAGE \
224 " [--mdt-count|-c stripe_count>\n" \
225 " [--help|-h] [--mdt-hash|-H mdt_hash]\n" \
226 " [--mdt-index|-i mdt_index[,mdt_index,...]\n" \
227 " [--default|-D] [--mode|-o mode]\n" \
228 " [--max-inherit|-X max_inherit]\n" \
229 " [--max-inherit-rr max_inherit_rr] <dir>\n" \
230 "To create dir with a foreign (free format) layout :\n" \
231 "setdirstripe|mkdir --foreign[=FOREIGN_TYPE] -x|-xattr STRING " \
232 " [--mode|-o MODE] [--flags HEX] DIRECTORY\n"
235 * command_t mirror_cmdlist - lfs mirror commands.
237 command_t mirror_cmdlist[] = {
238 { .pc_name = "create", .pc_func = lfs_mirror_create,
239 .pc_help = "Create a mirrored file.\n"
240 "usage: lfs mirror create --mirror-count|-N[MIRROR_COUNT]\n"
241 " [SETSTRIPE_OPTIONS] ... FILENAME|DIRECTORY ...\n" },
242 { .pc_name = "delete", .pc_func = lfs_mirror_delete,
243 .pc_help = "Delete a mirror from a file.\n"
244 "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
245 "\t --component-id|--comp-id|-I COMP_ID |\n"
246 "\t -p <pool>} MIRRORED_FILE ...\n"
248 { .pc_name = "extend", .pc_func = lfs_mirror_extend,
249 .pc_help = "Extend a mirrored file.\n"
250 "usage: lfs mirror extend "
251 "{--mirror-count|-N[MIRROR_COUNT]} [--no-verify] "
252 "[SETSTRIPE_OPTIONS|-f VICTIM_FILE] ... FILENAME ...\n" },
253 { .pc_name = "split", .pc_func = lfs_mirror_split,
254 .pc_help = "Split a mirrored file.\n"
255 "usage: lfs mirror split {--mirror-id MIRROR_ID |\n"
256 "\t --component-id|-I COMP_ID|-p POOL} [--destroy|-d]\n"
257 "\t [-f NEW_FILE] MIRRORED_FILE ...\n" },
258 { .pc_name = "read", .pc_func = lfs_mirror_read,
259 .pc_help = "Read the content of a specified mirror of a file.\n"
260 "usage: lfs mirror read {--mirror-id|-N MIRROR_ID}\n"
261 "\t\t[--outfile|-o <output_file>] <mirrored_file>\n" },
262 { .pc_name = "write", .pc_func = lfs_mirror_write,
263 .pc_help = "Write to a specified mirror of a file.\n"
264 "usage: lfs mirror write {--mirror-id|-N MIRROR_ID}\n"
265 "\t\t[--inputfile|-i <input_file>] <mirrored_file>\n" },
266 { .pc_name = "copy", .pc_func = lfs_mirror_copy,
267 .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
268 "usage: lfs mirror copy {--read-mirror|-i MIRROR_ID0}\n"
269 "\t\t{--write-mirror|-o MIRROR_ID1[,...]} <mirrored_file>\n" },
270 { .pc_name = "resync", .pc_func = lfs_mirror_resync,
271 .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
272 "usage: lfs mirror resync [--only MIRROR_ID[,...]>]\n"
273 "\t\t<mirrored_file> [<mirrored_file2>...]\n" },
274 { .pc_name = "verify", .pc_func = lfs_mirror_verify,
275 .pc_help = "Verify mirrored file(s).\n"
276 "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
277 "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
278 { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
279 .pc_help = "list commands supported by lfs mirror"},
280 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
281 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
282 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
287 * command_t pcc_cmdlist - lfs pcc commands.
289 command_t pcc_cmdlist[] = {
290 { .pc_name = "attach", .pc_func = lfs_pcc_attach,
291 .pc_help = "Attach given files to the Persistent Client Cache.\n"
292 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
293 "\t-i: archive id for RW-PCC\n" },
294 { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
295 .pc_help = "Attach given files into PCC by FID(s).\n"
296 "usage: lfs pcc attach_id {--id|-i NUM} {--mnt|-m MOUNTPOINT} FID ...\n"
297 "\t-i: archive id for RW-PCC\n"
298 "\t-m: Lustre mount point\n" },
299 { .pc_name = "state", .pc_func = lfs_pcc_state,
300 .pc_help = "Display the PCC state for given files.\n"
301 "usage: lfs pcc state <file> ...\n" },
302 { .pc_name = "detach", .pc_func = lfs_pcc_detach,
303 .pc_help = "Detach given files from the Persistent Client Cache.\n"
304 "usage: lfs pcc detach <file> ...\n" },
305 { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
306 .pc_help = "Detach given files from PCC by FID(s).\n"
307 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
308 { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
309 .pc_help = "list commands supported by lfs pcc"},
310 { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
311 { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
312 { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
316 /* all available commands */
317 command_t cmdlist[] = {
318 {"setstripe", lfs_setstripe, 0,
319 "To create a file with specified striping/composite layout, or\n"
320 "create/replace the default layout on an existing directory:\n"
321 SSM_CMD_COMMON("setstripe")
323 " <directory|filename>\n"
325 "To add component(s) to an existing composite file:\n"
326 SSM_CMD_COMMON("setstripe --component-add")
327 "To totally delete the default striping from an existing directory:\n"
328 "usage: setstripe [--delete|-d] <directory>\n"
330 "To create a mirrored file or set s default mirror layout on a directory:\n"
331 "usage: setstripe {--mirror-count|-N}[MIRROR_COUNT] [SETSTRIPE_OPTIONS] <directory|filename>\n"
333 "To delete the last component(s) from an existing composite file\n"
334 "(note that this will also delete any data in those components):\n"
335 "usage: setstripe --component-del [--component-id|-I COMP_ID]\n"
336 " [--component-flags|-F COMP_FLAGS]\n"
338 "\tCOMP_ID: Unique component ID to delete\n"
339 "\tCOMP_FLAGS: 'init' indicating all instantiated components\n"
340 "\t '^init' indicating all uninstantiated components\n"
341 "\t-I and -F cannot be specified at the same time\n"
343 "To set or clear flags on a specific component\n"
344 "(note that this command can only be applied to mirrored files:\n"
345 "usage: setstripe --comp-set {-I COMP_ID|--comp-flags=COMP_FLAGS}\n"
348 "To create a file with a foreign (free format) layout:\n"
349 "usage: setstripe --foreign[=FOREIGN_TYPE]\n"
350 " --xattr|-x LAYOUT_STRING [--flags HEX]\n"
351 " [--mode MODE] <filename>\n"},
352 {"getstripe", lfs_getstripe, 0,
353 "To list the layout pattern for a given file or files in a\n"
354 "directory or recursively for all files in a directory tree.\n"
355 "usage: getstripe [--ost|-O UUID] [--quiet|-q] [--verbose|-v]\n"
356 " [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
357 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
358 " [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
359 " [--layout|-L] [--generation|-g] [--yaml|-y]\n"
360 " [--help|-h] [--component-id|-I[=COMP_ID]]\n"
361 " [--component-flags[=COMP_FLAGS]]\n"
362 " [--component-count]\n"
363 " [--extension-size|--ext-size|-z]\n"
364 " [--component-start[=[+-]COMP_START]]\n"
365 " [--component-end[=[+-]COMP_END]|-E[[+-]comp_end]]\n"
366 " [[!] --mirror-index=[+-]INDEX |\n"
367 " [!] --mirror-id=[+-]MIRROR_ID] [--mirror-count|-N]\n"
368 " <directory|filename> ..."},
369 {"setdirstripe", lfs_setdirstripe, 0,
370 "Create striped directory on specified MDT, same as mkdir.\n"
371 "May be restricted to root or group users, depending on settings.\n"
372 "usage: setdirstripe [OPTION] <directory>\n"
374 {"getdirstripe", lfs_getdirstripe, 0,
375 "To list the layout pattern info for a given directory\n"
376 "or recursively for all directories in a directory tree.\n"
377 "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
378 " [--help|-h] [--mdt-hash|-H] [--obd|-O UUID]\n"
379 " [--recursive|-r] [--yaml|-y]\n"
380 " [--verbose|-v] [--default|-D]\n"
381 " [--max-inherit|-X]\n"
382 " [--max-inherit-rr] <dir> ..."},
383 {"mkdir", lfs_setdirstripe, 0,
384 "Create striped directory on specified MDT, same as setdirstripe.\n"
385 "usage: mkdir [OPTION] <directory>\n"
387 {"rm_entry", lfs_rmentry, 0,
388 "To remove the name entry of the remote directory. Note: This\n"
389 "command will only delete the name entry, i.e. the remote directory\n"
390 "will become inaccessable after this command. This can only be done\n"
391 "by the administrator\n"
392 "usage: rm_entry <dir>\n"},
393 {"unlink_foreign", lfs_unlink_foreign, 0,
394 "To remove the foreign file/dir.\n"
395 "Note: This is for files/dirs prevented to be removed using\n"
396 "unlink/rmdir, but works also for regular ones\n"
397 "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
398 {"pool_list", lfs_poollist, 0,
399 "List pools or pool OSTs\n"
400 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
401 {"find", lfs_find, 0,
402 "find files matching given attributes recursively in directory tree.\n"
403 "usage: find <directory|filename> ...\n"
404 " [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
405 " [[!] --mtime|-M [+-]N[smhdwy]]\n"
406 " [[!] --btime|--Btime|-B [+-]N[smhdwy]] [--help|-h]\n"
407 " [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
408 " [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
409 " [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
410 " [[!] --perm [/-]mode] [[!] --pool <pool>] [--print|-P]\n"
411 " [--print0|-0] [[!] --projid <projid>]\n"
412 " [[!] --size|-s [+-]N[bkMGTPE]]\n"
413 " [[!] --stripe-count|-c [+-]<stripes>]\n"
414 " [[!] --stripe-index|-i <index,...>]\n"
415 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
416 " [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
417 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
418 " [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
419 " [[!] --layout|-L released,raid0,mdt]\n"
420 " [[!] --foreign[=<foreign_type>]]\n"
421 " [[!] --component-count [+-]<comp_cnt>]\n"
422 " [[!] --component-start [+-]N[kMGTPE]]\n"
423 " [[!] --component-end|-E [+-]N[kMGTPE]]\n"
424 " [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
425 " [[!] --mirror-count|-N [+-]<n>]\n"
426 " [[!] --mirror-state <[^]state>]\n"
427 " [[!] --mdt-count|-T [+-]<stripes>]\n"
428 " [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
429 " [[!] --mdt-index|-m <uuid|index,...>]\n"
430 "\t !: used before an option indicates 'NOT' requested attribute\n"
431 "\t -: used before a value indicates less than requested value\n"
432 "\t +: used before a value indicates more than requested value\n"
433 "\t ^: used before a flag indicates to exclude it\n"},
434 {"check", lfs_check, 0,
435 "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
436 "or all the servers (MGTs, MDTs and OSTs).\n"
437 "usage: check {mgts|osts|mdts|all}"},
438 {"osts", lfs_osts, 0, "list OSTs connected to client "
439 "[for specified path only]\n" "usage: osts [path]"},
440 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
441 "[for specified path only]\n" "usage: mdts [path]"},
443 "report filesystem disk space usage or inodes usage "
444 "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
445 "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
446 " [--pool|-p <fsname>[.<pool>]] [path]"},
447 {"getname", lfs_getname, 0,
448 "list instances and specified mount points [for specified path only]\n"
449 "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
450 #ifdef HAVE_SYS_QUOTA_H
451 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
452 "usage: setquota {-u|-g|-p} UNAME|UID|GNAME|GID|PROJID\n"
453 " -b BLOCK_SOFTLIMIT -B BLOCK_HARDLIMIT\n"
454 " -i INODE_SOFTLIMIT -I INODE_HARDLIMIT <filesystem>\n"
455 " setquota {-u|--user USER|UID|-g|--group GROUP|GID|-p|--projid PROJID}\n"
456 " [--block-softlimit BLOCK_SOFTLIMIT]\n"
457 " [--block-hardlimit BLOCK_HARDLIMIT]\n"
458 " [--inode-softlimit INODE_SOFTLIMIT]\n"
459 " [--inode-hardlimit INODE_HARDLIMIT] <filesystem>\n"
460 " setquota [-t] {-h|--help|-u|--user|-g|--group|-p|--projid}\n"
461 " [--block-grace 'notify'|BLOCK_GRACE]\n"
462 " [--inode-grace 'notify'|INODE_GRACE] <filesystem>\n"
463 " setquota {-U|-G|-P}\n"
464 " -b BLOCK_SOFTLIMIT -B BLOCK_HARDLIMIT\n"
465 " -i INODE_SOFTLIMIT -I INODE_HARDLIMIT <filesystem>\n"
466 " setquota {-U|--default-usr|-G|--default-grp|-P|--default-prj}\n"
467 " [--block-softlimit BLOCK_SOFTLIMIT]\n"
468 " [--block-hardlimit BLOCK_HARDLIMIT]\n"
469 " [--inode-softlimit INODE_SOFTLIMIT]\n"
470 " [--inode-hardlimit INODE_HARDLIMIT] <filesystem>\n"
471 " setquota {-u|-g|-p} UNAME|UID|GNAME|GID|PROJID\n"
473 " -b can be used instead of --block-softlimit/--block-grace\n"
474 " -B can be used instead of --block-hardlimit\n"
475 " -i can be used instead of --inode-softlimit/--inode-grace\n"
476 " -I can be used instead of --inode-hardlimit\n"
477 " -d can be used instead of --default\n\n"
478 "Note: The total quota space will be split into many qunits and\n"
479 " balanced over all server targets, the minimal qunit size is\n"
480 " 1M bytes for block space and 1K inodes for inode space.\n\n"
481 " The maximum quota grace time is 2^48 - 1 seconds.\n\n"
482 " Quota space rebalancing process will stop when this mininum\n"
483 " value is reached. As a result, quota exceeded can be returned\n"
484 " while many targets still have 1MB or 1K inodes of spare\n"
486 " When setting the grace time, 'notify' can be used as grace to\n"
487 " be notified after the quota is over soft limit but prevents\n"
488 " the soft limit from becoming the hard limit."},
489 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
490 "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
491 " [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID] <filesystem>\n"
492 " quota [-o OBD_UUID|-i MDT_IDX|-I OST_IDX] -t {-u|-g|-p} <filesystem>\n"
493 " quota [-q] [-v] [h] {-U|-G|-P} <filesystem>"},
494 {"project", lfs_project, 0,
495 "Change or list project attribute for specified file or directory.\n"
496 "usage: project [-d|-r] <file|directory...>\n"
497 " list project ID and flags on file(s) or directories\n"
498 " project [-p id] [-s] [-r] <file|directory...>\n"
499 " set project ID and/or inherit flag for specified file(s) or directories\n"
500 " project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
501 " check project ID and flags on file(s) or directories, print outliers\n"
502 " project -C [-r] [-k] <file|directory...>\n"
503 " clear the project inherit flag and ID on the file or directory\n"
506 {"flushctx", lfs_flushctx, 0,
507 "Flush security context for current user.\n"
508 "usage: flushctx [-k] [-r] [mountpoint...]"},
509 {"changelog", lfs_changelog, 0,
510 "Show the metadata changes on an MDT."
511 "\nusage: changelog <mdtname> [startrec [endrec]]"},
512 {"changelog_clear", lfs_changelog_clear, 0,
513 "Indicate that old changelog records up to <endrec> are no longer of "
514 "interest to consumer <id>, allowing the system to free up space.\n"
515 "An <endrec> of 0 means all records.\n"
516 "usage: changelog_clear <mdtname> <id> <endrec>"},
517 {"fid2path", lfs_fid2path, 0,
518 "Resolve the full path(s) for given FID(s). For a specific hardlink "
519 "specify link number <linkno>.\n"
520 "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
521 "<fsname|root> <fid>..."},
522 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
523 "usage: path2fid [--parents] <path> ..."},
524 {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
525 "usage: rmfid <fsname|rootpath> <fid> ..."},
526 {"data_version", lfs_data_version, 0, "Display file data version for "
527 "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
528 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
529 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
530 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
531 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
532 "[--archived] [--lost] [--archive-id NUM] <file> ..."},
533 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
535 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
536 "[--archived] [--lost] <file> ..."},
537 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
538 "given files.\n" "usage: hsm_action <file> ..."},
539 {"hsm_archive", lfs_hsm_archive, 0,
540 "Archive file to external storage.\n"
541 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
543 {"hsm_restore", lfs_hsm_restore, 0,
544 "Restore file from external storage.\n"
545 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
546 {"hsm_release", lfs_hsm_release, 0,
547 "Release files from Lustre.\n"
548 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
549 {"hsm_remove", lfs_hsm_remove, 0,
550 "Remove file copy from external storage.\n"
551 "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
553 " (FILE [FILE ...] | "
554 "--mntpath MOUNTPATH FID [FID ...])\n"
556 "Note: To remove an archived copy of a file already deleted from a "
558 "--mntpath option and a list of FIDs must be specified"
560 {"hsm_cancel", lfs_hsm_cancel, 0,
561 "Cancel requests related to specified files.\n"
562 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
563 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
564 "usage: swap_layouts <path1> <path2>"},
565 {"migrate", lfs_setstripe_migrate, 0,
566 "migrate directories and their inodes between MDTs.\n"
567 "usage: migrate [--mdt-count|-c STRIPE_COUNT]\n"
568 " [--mdt-hash|-H HASH_TYPE]\n"
569 " [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
572 "migrate file objects from one OST layout to another\n"
573 "(may be not safe with concurent writes).\n"
576 "To move directories between MDTs. This command is deprecated, "
577 "use \"migrate\" instead.\n"
578 "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
580 {"ladvise", lfs_ladvise, 0,
581 "Provide servers with advice about access patterns for a file.\n"
582 "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
583 " [--background|-b] [--unset|-u]\n\n"
584 " {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
585 " {[--mode|-m [READ,WRITE]}\n"
587 {"mirror", lfs_mirror, mirror_cmdlist,
588 "lfs commands used to manage files with mirrored components:\n"
589 "lfs mirror create - create a mirrored file or directory\n"
590 "lfs mirror extend - add mirror(s) to an existing file\n"
591 "lfs mirror split - split a mirror from an existing mirrored file\n"
592 "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
593 "lfs mirror read - read a mirror content of a mirrored file\n"
594 "lfs mirror write - write to a mirror of a mirrored file\n"
595 "lfs mirror copy - copy a mirror to other mirror(s) of a file\n"
596 "lfs mirror verify - verify mirrored file(s)\n"},
597 {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
598 "usage: getsom [-s] [-b] [-f] <path>\n"
599 "\t-s: Only show the size value of the SOM data for a given file\n"
600 "\t-b: Only show the blocks value of the SOM data for a given file\n"
601 "\t-f: Only show the flags value of the SOM data for a given file\n"},
602 {"heat_get", lfs_heat_get, 0,
603 "To get heat of files.\n"
604 "usage: heat_get <file> ...\n"},
605 {"heat_set", lfs_heat_set, 0,
606 "To set heat flags of files.\n"
607 "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
608 "\t--clear|-c: Clear file heat for given files\n"
609 "\t--off|-o: Turn off file heat for given files\n"
610 "\t--on|-O: Turn on file heat for given files\n"},
611 {"pcc", lfs_pcc, pcc_cmdlist,
612 "lfs commands used to interact with PCC features:\n"
613 "lfs pcc attach - attach given files to Persistent Client Cache\n"
614 "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
615 "lfs pcc state - display the PCC state for given files\n"
616 "lfs pcc detach - detach given files from Persistent Client Cache\n"
617 "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
618 {"help", Parser_help, 0, "help"},
619 {"exit", Parser_quit, 0, "quit"},
620 {"quit", Parser_quit, 0, "quit"},
621 {"--version", Parser_version, 0,
622 "output build version of the utility and exit"},
623 {"--list-commands", lfs_list_commands, 0,
624 "list commands supported by the utility and exit"},
628 static int check_hashtype(const char *hashtype)
630 int type_num = atoi(hashtype);
633 /* numeric hash type */
634 if (hashtype && strlen(hashtype) == 1 &&
635 (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
637 /* string hash type */
638 for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
639 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
645 static uint32_t check_foreign_type_name(const char *foreign_type_name)
649 for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
650 if (!lu_foreign_types[i].lft_name)
652 if (strcmp(foreign_type_name,
653 lu_foreign_types[i].lft_name) == 0)
654 return lu_foreign_types[i].lft_type;
657 return LU_FOREIGN_TYPE_UNKNOWN;
660 static const char *error_loc = "syserror";
663 MIGRATION_NONBLOCK = 0x0001,
664 MIGRATION_MIRROR = 0x0002,
665 MIGRATION_NONDIRECT = 0x0004,
666 MIGRATION_VERBOSE = 0x0008,
670 migrate_open_files(const char *name, __u64 migration_flags,
671 const struct llapi_stripe_param *param,
672 struct llapi_layout *layout, int *fd_src, int *fd_tgt)
679 char parent[PATH_MAX];
680 char volatile_file[PATH_MAX];
686 if (!param && !layout) {
687 error_loc = "layout information";
691 /* search for file directory pathname */
692 if (strlen(name) > sizeof(parent) - 1) {
693 error_loc = "source file name";
697 strncpy(parent, name, sizeof(parent));
698 ptr = strrchr(parent, '/');
700 if (!getcwd(parent, sizeof(parent))) {
701 error_loc = "getcwd";
705 if (ptr == parent) /* leading '/' */
710 /* open file, direct io */
711 /* even if the file is only read, WR mode is nedeed to allow
714 rflags = O_RDWR | O_NOATIME;
715 if (!(migration_flags & MIGRATION_NONDIRECT))
717 fd = open(name, rflags);
720 error_loc = "cannot open source file";
724 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
726 error_loc = "cannot get MDT index";
731 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
732 mode_t open_mode = S_IRUSR | S_IWUSR;
734 random_value = random();
735 rc = snprintf(volatile_file, sizeof(volatile_file),
736 "%s/%s:%.4X:%.4X:fd=%.2d", parent,
737 LUSTRE_VOLATILE_HDR, mdt_index,
739 if (rc >= sizeof(volatile_file)) {
744 /* create, open a volatile file, use caching (ie no directio) */
746 /* Returns -1 and sets errno on error: */
747 fdv = llapi_layout_file_open(volatile_file, open_flags,
752 /* Does the right thing on error: */
753 fdv = llapi_file_open_param(volatile_file, open_flags,
756 } while (fdv < 0 && (rc = fdv) == -EEXIST);
759 error_loc = "cannot create volatile file";
764 * In case the MDT does not support creation of volatile files
765 * we should try to unlink it.
767 (void)unlink(volatile_file);
770 * Not-owner (root?) special case.
771 * Need to set owner/group of volatile file like original.
772 * This will allow to pass related check during layout_swap.
777 error_loc = "cannot stat source file";
781 rc = fstat(fdv, &stv);
784 error_loc = "cannot stat volatile";
788 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
789 rc = fchown(fdv, st.st_uid, st.st_gid);
792 error_loc = "cannot change ownwership of volatile";
811 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
813 struct llapi_layout *layout;
814 size_t buf_size = 4 * 1024 * 1024;
818 size_t page_size = sysconf(_SC_PAGESIZE);
822 layout = llapi_layout_get_by_fd(fd_src, 0);
824 uint64_t stripe_size;
826 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
828 buf_size = stripe_size;
830 llapi_layout_free(layout);
833 /* Use a page-aligned buffer for direct I/O */
834 rc = posix_memalign(&buf, page_size, buf_size);
838 sparse = llapi_file_is_sparse(fd_src);
840 rc = ftruncate(fd_dst, pos);
849 size_t to_read, to_write;
852 if (sparse && pos >= data_end) {
855 data_off = llapi_data_seek(fd_src, pos, &data_size);
857 /* Non-fatal, switch to full copy */
861 /* hole at the end of file, truncate up to it */
863 rc = ftruncate(fd_dst, data_off);
867 pos = data_off & ~(page_size - 1);
868 data_end = data_off + data_size;
869 to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
870 to_read = MIN(to_read, buf_size);
876 rc = check_file(fd_src);
881 rsize = pread(fd_src, buf, to_read, pos);
891 while (to_write > 0) {
894 written = pwrite(fd_dst, buf, to_write, pos);
902 if (rc || rsize < to_read)
910 /* Try to avoid page cache pollution after migration. */
911 (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
912 (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
918 static int migrate_set_timestamps(int fd, const struct stat *st)
920 struct timeval tv[2] = {
921 {.tv_sec = st->st_atime},
922 {.tv_sec = st->st_mtime}
925 return futimes(fd, tv);
928 static int migrate_block(int fd, int fdv)
938 error_loc = "cannot stat source file";
942 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
944 error_loc = "cannot get dataversion";
953 * The grouplock blocks all concurrent accesses to the file.
954 * It has to be taken after llapi_get_data_version as it would
957 rc = llapi_group_lock(fd, gid);
959 error_loc = "cannot get group lock";
963 rc = migrate_copy_data(fd, fdv, NULL);
965 error_loc = "data copy failed";
969 /* Make sure we keep original atime/mtime values */
970 rc = migrate_set_timestamps(fdv, &st);
972 error_loc = "set target file timestamp failed";
978 * for a migration we need to check data version on file did
981 * Pass in gid=0 since we already own grouplock.
983 rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
984 SWAP_LAYOUTS_CHECK_DV1);
986 error_loc = "file changed";
989 error_loc = "cannot swap layout";
994 rc2 = llapi_group_unlock(fd, gid);
995 if (rc2 < 0 && rc == 0) {
996 error_loc = "unlock group lock";
1004 * Internal helper for migrate_copy_data(). Check lease and report error if
1007 * \param[in] fd File descriptor on which to check the lease.
1009 * \retval 0 Migration can keep on going.
1010 * \retval -errno Error occurred, abort migration.
1012 static int check_lease(int fd)
1016 rc = llapi_lease_check(fd);
1018 return 0; /* llapi_check_lease returns > 0 on success. */
1023 static int migrate_nonblock(int fd, int fdv)
1030 rc = fstat(fd, &st);
1032 error_loc = "cannot stat source file";
1036 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1038 error_loc = "cannot get data version";
1042 rc = migrate_copy_data(fd, fdv, check_lease);
1044 error_loc = "data copy failed";
1048 rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1050 error_loc = "cannot get data version";
1056 error_loc = "source file changed";
1060 /* Make sure we keep original atime/mtime values */
1061 rc = migrate_set_timestamps(fdv, &st);
1063 error_loc = "set target file timestamp failed";
1070 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1072 struct pool_to_id_cbdata data = { .pool = pool };
1073 struct llapi_layout *layout = NULL;
1076 layout = llapi_layout_get_by_path(fname, 0);
1079 "error %s: file '%s' couldn't get layout: rc=%d\n",
1080 progname, fname, errno);
1084 rc = llapi_layout_sanity(layout, fname, false, true);
1086 llapi_layout_sanity_perror(errno);
1089 rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1098 llapi_layout_free(layout);
1102 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1103 __u32 flags, __u32 neg_flags)
1106 __u32 flags_array[2];
1113 "error %s: neither component id nor pool is specified\n",
1117 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1123 ids[count] = comp_id;
1124 flags_array[count] = flags;
1129 if (neg_flags & LCME_FL_STALE) {
1131 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1136 ids[count] = comp_id;
1137 flags_array[count] = neg_flags | LCME_FL_NEG;
1141 rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1143 if (errno == EUCLEAN) {
1146 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1147 progname, comp_id, fname);
1150 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1151 progname, comp_id, fname, flags, neg_flags);
1158 static int lfs_component_del(char *fname, __u32 comp_id,
1159 __u32 flags, __u32 neg_flags)
1163 if (flags && neg_flags)
1166 if (!flags && neg_flags)
1167 flags = neg_flags | LCME_FL_NEG;
1169 if ((flags && comp_id) || (!flags && !comp_id))
1173 if (flags & ~LCME_KNOWN_FLAGS) {
1175 "%s setstripe: unknown flags %#x\n",
1179 } else if (comp_id > LCME_ID_MAX) {
1180 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1185 rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1188 "%s setstripe: cannot delete component %#x from '%s': %s\n",
1189 progname, comp_id, fname, strerror(errno));
1193 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1200 rc = llapi_layout_file_comp_add(fname, layout);
1202 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1203 fname, strerror(errno));
1207 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1208 struct llapi_layout *layout)
1216 fd = lstat(fname, &st);
1217 if (fd == 0 && S_ISDIR(st.st_mode))
1218 open_flags = O_DIRECTORY | O_RDONLY;
1220 fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1222 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1223 S_ISDIR(st.st_mode) ?
1224 "set default composite layout for" :
1225 "create composite file",
1226 fname, strerror(errno));
1230 static int lfs_migrate(char *name, __u64 migration_flags,
1231 struct llapi_stripe_param *param,
1232 struct llapi_layout *layout)
1234 struct llapi_layout *existing;
1235 uint64_t dom_new, dom_cur;
1240 rc = migrate_open_files(name, migration_flags, param, layout,
1245 rc = llapi_layout_dom_size(layout, &dom_new);
1247 error_loc = "cannot get new layout DoM size";
1250 /* special case for migration to DOM layout*/
1251 existing = llapi_layout_get_by_fd(fd, 0);
1253 error_loc = "cannot get existing layout";
1257 rc = llapi_layout_dom_size(existing, &dom_cur);
1259 error_loc = "cannot get current layout DoM size";
1264 * if file has DoM layout already then migration is possible to
1265 * the new layout with the same DoM component via swap layout,
1266 * if new layout used bigger DOM size, then mirroring is used
1268 if (dom_new > dom_cur) {
1269 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1272 error_loc = "cannot migrate to DOM layout";
1276 if (!(migration_flags & MIGRATION_NONBLOCK)) {
1278 * Blocking mode (forced if servers do not support file lease).
1279 * It is also the default mode, since we cannot distinguish
1280 * between a broken lease and a server that does not support
1281 * atomic swap/close (LU-6785)
1283 rc = migrate_block(fd, fdv);
1287 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1289 error_loc = "cannot get lease";
1293 rc = migrate_nonblock(fd, fdv);
1295 llapi_lease_release(fd);
1300 * Atomically put lease, swap layouts and close.
1301 * for a migration we need to check data version on file did
1304 rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1306 error_loc = "cannot swap layout";
1318 fprintf(stderr, "error: %s: %s: %s: %s\n",
1319 progname, name, error_loc, strerror(-rc));
1320 else if (migration_flags & MIGRATION_VERBOSE)
1321 printf("%s\n", name);
1326 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1329 char *dup_string = NULL;
1335 if (!string || !string[0])
1338 dup_string = strdup(string);
1340 llapi_printf(LLAPI_MSG_ERROR,
1341 "%s: insufficient memory\n",
1346 for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1350 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1351 __u32 comp_flag = comp_flags_table[i].cfn_flag;
1352 const char *comp_name = comp_flags_table[i].cfn_name;
1354 if (strcmp(name, comp_name) == 0) {
1355 *flags |= comp_flag;
1357 } else if (strncmp(name, "^", 1) == 0 &&
1358 strcmp(name + 1, comp_name) == 0) {
1359 *neg_flags |= comp_flag;
1364 llapi_printf(LLAPI_MSG_ERROR,
1365 "%s: component flag '%s' not supported\n",
1372 if (!*flags && !*neg_flags)
1375 /* don't allow to set and exclude the same flag */
1376 if (*flags & *neg_flags)
1384 static int mdthash_input(char *string, __u32 *inflags,
1385 __u32 *exflags, __u32 *type)
1392 {"migrating", LMV_HASH_FLAG_MIGRATION},
1393 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1394 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1403 for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1407 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1408 if (strcmp(name, mhflist[i].name) == 0 ||
1409 name[0] == mhflist[i].name[0]) {
1410 *inflags |= mhflist[i].flag;
1412 } else if (name[0] == '^' &&
1413 (strcmp(name + 1, mhflist[i].name) == 0 ||
1414 name[1] == mhflist[i].name[0])) {
1415 *exflags |= mhflist[i].flag;
1420 i = check_hashtype(name);
1425 llapi_printf(LLAPI_MSG_ERROR,
1426 "%s: invalid mdt_hash value '%s'\n",
1432 /* don't allow to include and exclude the same flag */
1433 if (*inflags & *exflags) {
1434 llapi_printf(LLAPI_MSG_ERROR,
1435 "%s: include and exclude same flag '%s'\n",
1443 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1451 if (strncmp(string, "^", 1) == 0) {
1452 *neg_state = llapi_layout_string_flags(string + 1);
1453 if (*neg_state != 0)
1456 *state = llapi_layout_string_flags(string);
1461 llapi_printf(LLAPI_MSG_ERROR,
1462 "%s: mirrored file state '%s' not supported\n",
1468 * struct mirror_args - Command-line arguments for mirror(s).
1469 * @m_count: Number of mirrors to be created with this layout.
1470 * @m_flags: Mirror level flags, only 'prefer' is supported.
1471 * @m_layout: Mirror layout.
1472 * @m_file: A victim file. Its layout will be split and used as a mirror.
1473 * @m_next: Point to the next node of the list.
1475 * Command-line arguments for mirror(s) will be parsed and stored in
1476 * a linked list that consists of this structure.
1478 struct mirror_args {
1481 struct llapi_layout *m_layout;
1483 struct mirror_args *m_next;
1488 * enum mirror_flags - Flags for extending a mirrored file.
1489 * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1490 * in case the victim file(s) contains the same data as the
1491 * original mirrored file.
1492 * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1493 * @MF_COMP_ID: specified component id instead of mirror id
1495 * Flags for extending a mirrored file.
1505 * mirror_create_sanity_check() - Check mirror list.
1506 * @list: A linked list that stores the mirror arguments.
1508 * This function does a sanity check on @list for creating
1511 * Return: 0 on success or a negative error code on failure.
1513 static int mirror_create_sanity_check(const char *fname,
1514 struct mirror_args *list,
1518 bool has_m_file = false;
1519 bool has_m_layout = false;
1524 if (fname && check_fname) {
1525 struct llapi_layout *layout;
1527 layout = llapi_layout_get_by_path(fname, 0);
1530 "error: %s: file '%s' couldn't get layout\n",
1535 rc = llapi_layout_sanity(layout, fname, false, true);
1537 llapi_layout_free(layout);
1540 llapi_layout_sanity_perror(rc);
1548 llapi_layout_free(list->m_layout);
1551 llapi_layout_get_by_path(list->m_file, 0);
1552 if (!list->m_layout) {
1554 "error: %s: file '%s' has no layout\n",
1555 progname, list->m_file);
1559 has_m_layout = true;
1560 if (!list->m_layout) {
1561 fprintf(stderr, "error: %s: no mirror layout\n",
1567 rc = llapi_layout_sanity(list->m_layout, fname, false, true);
1569 llapi_layout_sanity_perror(rc);
1573 list = list->m_next;
1576 if (has_m_file && has_m_layout) {
1578 "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1586 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1588 __u32 mirror_flags = *(__u32 *)cbdata;
1592 rc = llapi_layout_comp_flags_get(layout, &flags);
1597 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1602 return LLAPI_LAYOUT_ITER_CONT;
1606 * mirror_create() - Create a mirrored file.
1607 * @fname: The file to be created.
1608 * @mirror_list: A linked list that stores the mirror arguments.
1610 * This function creates a mirrored file @fname with the mirror(s)
1611 * from @mirror_list.
1613 * Return: 0 on success or a negative error code on failure.
1615 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1617 struct llapi_layout *layout = NULL;
1618 struct mirror_args *cur_mirror = NULL;
1619 uint16_t mirror_count = 0;
1623 rc = mirror_create_sanity_check(fname, mirror_list, false);
1627 cur_mirror = mirror_list;
1628 while (cur_mirror) {
1629 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1631 &cur_mirror->m_flags);
1634 fprintf(stderr, "%s: failed to set mirror flags\n",
1639 for (i = 0; i < cur_mirror->m_count; i++) {
1640 rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1644 "error: %s: merge layout failed: %s\n",
1645 progname, strerror(errno));
1649 mirror_count += cur_mirror->m_count;
1650 cur_mirror = cur_mirror->m_next;
1654 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1658 rc = llapi_layout_mirror_count_set(layout, mirror_count);
1661 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1662 progname, strerror(errno));
1666 rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1674 llapi_layout_free(layout);
1679 * Compare files and check lease on @fd.
1681 * \retval bytes number of bytes are the same
1683 static ssize_t mirror_file_compare(int fd, int fdv)
1685 const size_t buflen = 4 * 1024 * 1024; /* 4M */
1687 ssize_t bytes_done = 0;
1688 ssize_t bytes_read = 0;
1690 buf = malloc(buflen * 2);
1695 if (!llapi_lease_check(fd)) {
1696 bytes_done = -EBUSY;
1700 bytes_read = read(fd, buf, buflen);
1701 if (bytes_read <= 0)
1704 if (bytes_read != read(fdv, buf + buflen, buflen))
1708 * XXX: should compute the checksum on each buffer and then
1709 * compare checksum to avoid cache collision
1711 if (memcmp(buf, buf + buflen, bytes_read))
1714 bytes_done += bytes_read;
1722 static int mirror_extend_file(const char *fname, const char *victim_file,
1723 enum mirror_flags mirror_flags)
1728 struct stat stbuf_v;
1729 struct ll_ioc_lease *data = NULL;
1732 fd = open(fname, O_RDWR);
1734 error_loc = "open source file";
1739 fdv = open(victim_file, O_RDWR);
1741 error_loc = "open target file";
1746 if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1747 error_loc = "stat source or target file";
1752 if (stbuf.st_dev != stbuf_v.st_dev) {
1753 error_loc = "stat source and target file";
1758 /* mirrors should be of the same size */
1759 if (stbuf.st_size != stbuf_v.st_size) {
1760 error_loc = "file sizes don't match";
1765 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1767 error_loc = "cannot get lease";
1771 if (!(mirror_flags & MF_NO_VERIFY)) {
1773 /* mirrors should have the same contents */
1774 ret = mirror_file_compare(fd, fdv);
1775 if (ret != stbuf.st_size) {
1776 error_loc = "file busy or contents don't match";
1777 rc = ret < 0 ? ret : -EINVAL;
1782 /* Get rid of caching pages from clients */
1783 rc = llapi_file_flush(fd);
1785 error_loc = "cannot get data version";
1789 rc = llapi_file_flush(fdv);
1791 error_loc = "cannot get data version";
1795 rc = migrate_set_timestamps(fd, &stbuf);
1797 error_loc = "cannot set source file timestamp";
1801 /* Atomically put lease, merge layouts and close. */
1802 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1804 error_loc = "memory allocation";
1807 data->lil_mode = LL_LEASE_UNLCK;
1808 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1809 data->lil_count = 1;
1810 data->lil_ids[0] = fdv;
1811 rc = llapi_lease_set(fd, data);
1813 error_loc = "cannot merge layout";
1815 } else if (rc == 0) {
1817 error_loc = "lost lease lock";
1830 (void) unlink(victim_file);
1832 fprintf(stderr, "error: %s: %s: %s: %s\n",
1833 progname, fname, error_loc, strerror(-rc));
1837 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1838 bool inherit, uint32_t flags)
1840 struct llapi_layout *f_layout = NULL;
1841 struct ll_ioc_lease *data = NULL;
1848 f_layout = llapi_layout_get_by_path(name, 0);
1851 fprintf(stderr, "%s: cannot get layout\n", progname);
1854 rc = llapi_layout_get_last_init_comp(f_layout);
1856 fprintf(stderr, "%s: cannot get the last init comp\n",
1860 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1863 "%s: cannot inherit from the last init comp\n",
1868 llapi_layout_comp_flags_set(m_layout, flags);
1869 rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1874 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1876 error_loc = "cannot get lease";
1880 rc = fstat(fd, &st);
1882 error_loc = "cannot stat source file";
1886 rc = migrate_nonblock(fd, fdv);
1888 llapi_lease_release(fd);
1892 rc = migrate_set_timestamps(fd, &st);
1894 error_loc = "cannot set source file timestamp";
1898 /* Atomically put lease, merge layouts and close. */
1899 data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1901 error_loc = "memory allocation";
1904 data->lil_mode = LL_LEASE_UNLCK;
1905 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1906 data->lil_count = 1;
1907 data->lil_ids[0] = fdv;
1908 rc = llapi_lease_set(fd, data);
1910 error_loc = "cannot merge layout";
1912 } else if (rc == 0) {
1914 error_loc = "lost lease lock";
1927 fprintf(stderr, "error: %s: %s: %s: %s\n",
1928 progname, name, error_loc, strerror(-rc));
1932 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1933 enum mirror_flags mirror_flags)
1937 while (mirror_list) {
1938 if (mirror_list->m_file) {
1939 rc = mirror_extend_file(fname, mirror_list->m_file,
1942 __u32 mirror_count = mirror_list->m_count;
1944 while (mirror_count > 0) {
1945 rc = mirror_extend_layout(fname,
1946 mirror_list->m_layout,
1947 mirror_list->m_inherit,
1948 mirror_list->m_flags);
1958 mirror_list = mirror_list->m_next;
1964 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1969 rc = llapi_layout_mirror_id_get(layout, &id);
1973 if ((__u16)id == *(__u16 *)cbdata)
1974 return LLAPI_LAYOUT_ITER_STOP;
1976 return LLAPI_LAYOUT_ITER_CONT;
1979 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1984 rc = llapi_layout_comp_id_get(layout, &id);
1988 if (id == *(__u32 *)cbdata)
1989 return LLAPI_LAYOUT_ITER_STOP;
1991 return LLAPI_LAYOUT_ITER_CONT;
1994 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
1996 char buf[LOV_MAXPOOLNAME + 1];
1997 struct pool_to_id_cbdata *d = (void *)cbdata;
2001 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2004 if (strcmp(d->pool, buf))
2005 return LLAPI_LAYOUT_ITER_CONT;
2007 rc = llapi_layout_mirror_id_get(layout, &id);
2012 return LLAPI_LAYOUT_ITER_STOP;
2015 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2017 char buf[LOV_MAXPOOLNAME + 1];
2018 struct pool_to_id_cbdata *d = (void *)cbdata;
2022 rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2025 if (strcmp(d->pool, buf))
2026 return LLAPI_LAYOUT_ITER_CONT;
2028 rc = llapi_layout_comp_id_get(layout, &id);
2033 return LLAPI_LAYOUT_ITER_STOP;
2036 struct collect_ids_data {
2042 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2044 struct collect_ids_data *cid = cbdata;
2048 rc = llapi_layout_mirror_id_get(layout, &id);
2052 if ((__u16)id != cid->cid_exclude) {
2055 for (i = 0; i < cid->cid_count; i++) {
2056 /* already collected the mirror id */
2057 if (id == cid->cid_ids[i])
2058 return LLAPI_LAYOUT_ITER_CONT;
2060 cid->cid_ids[cid->cid_count] = id;
2064 return LLAPI_LAYOUT_ITER_CONT;
2068 * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2069 * @mirror_id: Mirror id to be checked.
2070 * @layout: Mirror component list.
2072 * This function checks if a mirror with specified @mirror_id is the last
2073 * non-stale mirror of a layout @layout.
2075 * Return: true or false.
2078 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2080 __u16 mirror_ids[128] = { 0 };
2081 struct collect_ids_data cid = { .cid_ids = mirror_ids,
2083 .cid_exclude = mirror_id, };
2086 llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2088 for (i = 0; i < cid.cid_count; i++) {
2089 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2092 comp_size = llapi_mirror_find_stale(layout, comp_array,
2093 ARRAY_SIZE(comp_array),
2102 static int mirror_split(const char *fname, __u32 id, const char *pool,
2103 enum mirror_flags mflags, const char *victim_file)
2105 struct llapi_layout *layout;
2106 char parent[PATH_MAX];
2107 char victim[PATH_MAX];
2108 int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2110 struct ll_ioc_lease *data;
2111 uint16_t mirror_count;
2115 bool purge = true; /* delete mirror by setting fdv=fd */
2119 if (victim_file && (strcmp(fname, victim_file) == 0)) {
2121 "error %s: the source file '%s' and -f file are the same\n",
2126 /* check fname contains mirror with mirror_id/comp_id */
2127 layout = llapi_layout_get_by_path(fname, 0);
2130 "error %s: file '%s' couldn't get layout\n",
2135 rc = llapi_layout_sanity(layout, fname, false, true);
2137 llapi_layout_sanity_perror(rc);
2141 rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2144 "error %s: file '%s' couldn't get mirror count\n",
2148 if (mirror_count < 2) {
2150 "error %s: file '%s' has %d component, cannot split\n",
2151 progname, fname, mirror_count);
2155 if (mflags & MF_COMP_POOL) {
2156 struct pool_to_id_cbdata data = { .pool = pool };
2158 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2160 mirror_id = data.id;
2161 } else if (mflags & MF_COMP_ID) {
2162 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2163 mirror_id = mirror_id_of(id);
2165 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2169 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2172 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2173 if (mflags & MF_COMP_POOL) {
2175 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2176 progname, fname, pool);
2178 } else if (mflags & MF_COMP_ID) {
2180 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2181 progname, fname, id);
2185 "error %s: file '%s' does not contain mirror with id %u\n",
2186 progname, fname, id);
2191 fd = open(fname, O_RDWR);
2194 "error %s: open file '%s' failed: %s\n",
2195 progname, fname, strerror(errno));
2199 /* get victim file directory pathname */
2200 if (strlen(fname) > sizeof(parent) - 1) {
2201 fprintf(stderr, "error %s: file name of '%s' too long\n",
2206 strncpy(parent, fname, sizeof(parent));
2207 ptr = strrchr(parent, '/');
2209 if (!getcwd(parent, sizeof(parent))) {
2210 fprintf(stderr, "error %s: getcwd failed: %s\n",
2211 progname, strerror(errno));
2221 rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2223 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2228 rc = llapi_file_is_encrypted(fd);
2230 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2231 progname, fname, rc);
2238 /* use a temp file to store the splitted layout */
2239 if (mflags & MF_DESTROY) {
2240 char file_path[PATH_MAX];
2241 unsigned int rnumber;
2244 if (last_non_stale_mirror(mirror_id, layout)) {
2247 "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2253 /* don't use volatile file for mirror destroy */
2257 * try the old way to delete mirror using
2262 rc = snprintf(file_path,
2264 "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2268 rc >= sizeof(file_path)) {
2269 fdv = -ENAMETOOLONG;
2273 open_flags = O_RDWR |
2274 (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2275 O_CREAT | O_EXCL | O_NOFOLLOW;
2276 fdv = open(file_path, open_flags,
2280 } while (fdv < 0 && rc == -EEXIST);
2286 "error %s: not permitted on encrypted file '%s': %d\n",
2287 progname, fname, rc);
2291 snprintf(victim, sizeof(victim), "%s.mirror~%u",
2293 fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2296 /* user specified victim file */
2300 "error %s: not permitted on encrypted file '%s': %d\n",
2301 progname, fname, rc);
2304 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2309 "error %s: create victim file failed: %s\n",
2310 progname, strerror(errno));
2314 /* get lease lock of fname */
2315 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2318 "error %s: cannot get lease of file '%s': %d\n",
2319 progname, fname, rc);
2323 /* Atomatically put lease, split layouts and close. */
2324 data = malloc(offsetof(typeof(*data), lil_ids[2]));
2330 data->lil_mode = LL_LEASE_UNLCK;
2331 data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2332 data->lil_count = 2;
2333 data->lil_ids[0] = fdv;
2334 data->lil_ids[1] = mirror_id;
2335 rc = llapi_lease_set(fd, data);
2337 if (rc == -EINVAL && purge) {
2338 /* could be old MDS which prohibit fd==fdv */
2343 if (rc == 0) /* lost lease lock */
2346 "error %s: cannot split '%s': %s\n",
2347 progname, fname, strerror(-rc));
2359 llapi_layout_free(layout);
2364 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2365 __u16 *mirror_ids, int ids_nr);
2367 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2368 __u64 migration_flags,
2369 struct llapi_stripe_param *param,
2370 struct llapi_layout *layout)
2372 struct ll_ioc_lease *data = NULL;
2375 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2377 error_loc = "cannot get lease";
2381 /* Atomically put lease, merge layouts, resync and close. */
2382 data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2384 error_loc = "memory allocation";
2387 data->lil_mode = LL_LEASE_UNLCK;
2388 data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2389 data->lil_count = 1;
2390 data->lil_ids[0] = fdv;
2391 rc = llapi_lease_set(fd, data);
2393 error_loc = "cannot merge layout";
2395 } else if (rc == 0) {
2397 error_loc = "lost lease lock";
2403 rc = lfs_mirror_resync_file(name, data, NULL, 0);
2405 error_loc = "cannot resync file";
2409 /* delete first mirror now */
2410 rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2412 error_loc = "cannot delete old layout";
2420 fprintf(stderr, "error: %s: %s: %s: %s\n",
2421 progname, name, error_loc, strerror(-rc));
2422 else if (migration_flags & MIGRATION_VERBOSE)
2423 printf("%s\n", name);
2430 * Parse a string containing an target index list into an array of integers.
2432 * The input string contains a comma delimited list of individual
2433 * indices and ranges, for example "1,2-4,7". Add the indices into the
2434 * \a tgts array and remove duplicates.
2436 * \param[out] tgts array to store indices in
2437 * \param[in] size size of \a tgts array
2438 * \param[in] offset starting index in \a tgts
2439 * \param[in] arg string containing OST index list
2440 * \param[in/out] overstriping index list may contain duplicates
2442 * \retval positive number of indices in \a tgts
2443 * \retval -EINVAL unable to parse \a arg
2445 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2446 unsigned long long *pattern)
2450 int slots = size - offset;
2452 bool overstriped = false;
2458 end_of_loop = false;
2459 while (!end_of_loop) {
2460 int start_index = 0;
2463 char *endptr = NULL;
2467 ptr = strchrnul(arg, ',');
2469 end_of_loop = *ptr == '\0';
2473 start_index = strtol(arg, &endptr, 0);
2474 if (endptr == arg) /* no data at all */
2476 if (errno != 0 || start_index < -1 ||
2477 (*endptr != '-' && *endptr != '\0'))
2480 end_index = start_index;
2481 if (*endptr == '-') {
2483 end_index = strtol(endptr + 1, &endptr, 0);
2484 if (errno != 0 || *endptr != '\0' || end_index < -1)
2486 if (end_index < start_index)
2490 for (i = start_index; i <= end_index && slots > 0; i++) {
2493 /* remove duplicate */
2494 for (j = 0; j < offset; j++) {
2495 if (tgts[j] == i && pattern &&
2496 *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2498 else if (tgts[j] == i)
2504 if (j == offset) { /* check complete */
2510 if (slots == 0 && i < end_index)
2518 if (!end_of_loop && ptr)
2521 if (!overstriped && pattern)
2522 *pattern = LLAPI_LAYOUT_DEFAULT;
2524 return rc < 0 ? rc : nr;
2527 struct lfs_setstripe_args {
2528 unsigned long long lsa_comp_end;
2529 unsigned long long lsa_stripe_size;
2530 unsigned long long lsa_extension_size;
2531 long long lsa_stripe_count;
2532 long long lsa_stripe_off;
2533 __u32 lsa_comp_flags;
2534 __u32 lsa_comp_neg_flags;
2535 unsigned long long lsa_pattern;
2536 unsigned int lsa_mirror_count;
2538 bool lsa_first_comp;
2539 bool lsa_extension_comp;
2541 char *lsa_pool_name;
2544 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2546 unsigned int mirror_count = lsa->lsa_mirror_count;
2547 bool first_comp = lsa->lsa_first_comp;
2549 memset(lsa, 0, sizeof(*lsa));
2551 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2552 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2553 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2554 lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2555 lsa->lsa_pool_name = NULL;
2557 lsa->lsa_mirror_count = mirror_count;
2558 lsa->lsa_first_comp = first_comp;
2562 * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2563 * @lsa: Stripe options to be initialized and inherited.
2565 * This function initializes stripe options in @lsa and inherit
2566 * stripe_size, stripe_count and OST pool_name options.
2570 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2572 unsigned long long stripe_size;
2573 long long stripe_count;
2574 char *pool_name = NULL;
2576 stripe_size = lsa->lsa_stripe_size;
2577 stripe_count = lsa->lsa_stripe_count;
2578 pool_name = lsa->lsa_pool_name;
2580 setstripe_args_init(lsa);
2582 lsa->lsa_stripe_size = stripe_size;
2583 lsa->lsa_stripe_count = stripe_count;
2584 lsa->lsa_pool_name = pool_name;
2587 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2589 return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2590 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2591 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2592 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2593 lsa->lsa_comp_end != 0);
2596 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2598 if (lsa->lsa_nr_tgts) {
2599 if (lsa->lsa_nr_tgts < 0 ||
2600 lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2601 fprintf(stderr, "Invalid nr_tgts(%d)\n",
2607 if (lsa->lsa_stripe_count > 0 &&
2608 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2609 lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2610 lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2611 fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2612 lsa->lsa_stripe_count,
2624 * comp_args_to_layout() - Create or extend a composite layout.
2625 * @composite: Pointer to the composite layout.
2626 * @lsa: Stripe options for the new component.
2628 * This function creates or extends a composite layout by adding a new
2629 * component with stripe options from @lsa.
2631 * When modified, adjust llapi_stripe_param_verify() if needed as well.
2633 * Return: 0 on success or an error code on failure.
2635 static int comp_args_to_layout(struct llapi_layout **composite,
2636 struct lfs_setstripe_args *lsa,
2639 struct llapi_layout *layout = *composite;
2640 uint64_t prev_end = 0;
2646 layout = llapi_layout_alloc();
2648 fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2653 *composite = layout;
2654 lsa->lsa_first_comp = true;
2659 * Get current component extent, current component
2660 * must be the tail component.
2662 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2664 fprintf(stderr, "Get comp extent failed. %s\n",
2669 if (lsa->lsa_first_comp) {
2671 rc = llapi_layout_add_first_comp(layout);
2673 rc = llapi_layout_comp_add(layout);
2676 fprintf(stderr, "Add component failed. %s\n",
2682 rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2684 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2685 lsa->lsa_comp_flags, strerror(errno));
2690 uint64_t comp_end = lsa->lsa_comp_end;
2693 * The extendable component is 0-length, so it can be removed
2694 * if there is insufficient space to extend it.
2696 if (lsa->lsa_extension_comp)
2697 comp_end = prev_end;
2699 rc = llapi_layout_comp_extent_set(layout, prev_end,
2702 fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2703 prev_end, comp_end, strerror(errno));
2707 /* reset lsa_first_comp */
2708 lsa->lsa_first_comp = false;
2710 /* Data-on-MDT component setting */
2711 if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2713 if (lsa->lsa_stripe_count == 0)
2714 lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2715 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2716 lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2717 if (lsa->lsa_stripe_off == -1 ||
2718 lsa->lsa_stripe_off == 0)
2719 lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2721 * In case of Data-on-MDT patterns the only extra option
2722 * applicable is stripe size option.
2724 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2726 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2727 lsa->lsa_stripe_count);
2731 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2732 lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2734 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2735 lsa->lsa_stripe_size);
2739 if (lsa->lsa_nr_tgts != 0) {
2741 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2746 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2748 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2749 lsa->lsa_stripe_off);
2753 if (lsa->lsa_pool_name != 0) {
2755 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2756 lsa->lsa_pool_name);
2761 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2763 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2768 /* Data-on-MDT component has always single stripe up to end */
2769 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2770 } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2771 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2773 fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2780 size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2781 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2783 if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2784 rc = llapi_layout_extension_size_set(layout, size);
2786 rc = llapi_layout_stripe_size_set(layout, size);
2789 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2790 size, strerror(errno));
2794 rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2796 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2797 lsa->lsa_stripe_count, strerror(errno));
2801 if (lsa->lsa_pool_name) {
2802 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2804 fprintf(stderr, "Set pool name: %s failed. %s\n",
2805 lsa->lsa_pool_name, strerror(errno));
2809 rc = llapi_layout_pool_name_set(layout, "");
2811 fprintf(stderr, "Clear pool name failed: %s\n",
2817 rc = lsa_args_stripe_count_check(lsa);
2821 if (lsa->lsa_nr_tgts > 0) {
2824 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2825 rc = llapi_layout_ost_index_set(layout, i,
2830 /* Make sure stripe offset is in OST list. */
2831 if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2835 fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2836 lsa->lsa_stripe_off);
2840 } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2841 lsa->lsa_stripe_off != -1) {
2842 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2845 fprintf(stderr, "Set ost index %d failed. %s\n",
2846 i, strerror(errno));
2850 /* Create the second, virtual component of extension space */
2851 if (lsa->lsa_extension_comp) {
2852 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2853 lsa->lsa_extension_comp = false;
2860 static int build_component(struct llapi_layout **layout,
2861 struct lfs_setstripe_args *lsa, bool set_extent)
2865 rc = comp_args_to_layout(layout, lsa, set_extent);
2869 if (lsa->lsa_mirror_count > 0) {
2870 rc = llapi_layout_mirror_count_set(*layout,
2871 lsa->lsa_mirror_count);
2875 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2878 lsa->lsa_mirror_count = 0;
2884 static int build_prev_component(struct llapi_layout **layout,
2885 struct lfs_setstripe_args *prev,
2886 struct lfs_setstripe_args *lsa,
2889 int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2892 if (prev->lsa_stripe_size) {
2894 prev->lsa_comp_end = lsa->lsa_comp_end;
2895 prev->lsa_extension_size = lsa->lsa_extension_size;
2896 prev->lsa_extension_comp = true;
2899 rc = build_component(layout, prev, true);
2905 * Copy lsa to previous lsa;
2906 * if this is an extension component, make the previous invalid;
2909 prev->lsa_stripe_size = 0;
2916 #ifndef LCME_TEMPLATE_FLAGS
2917 #define LCME_TEMPLATE_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
2921 static int build_layout_from_yaml_node(struct cYAML *node,
2922 struct llapi_layout **layout,
2923 struct lfs_setstripe_args *lsa,
2924 struct lfs_setstripe_args *prevp)
2926 struct lfs_setstripe_args prev = { 0 };
2927 __u32 *osts = lsa->lsa_tgts;
2935 string = node->cy_string;
2937 if (node->cy_type == CYAML_TYPE_OBJECT) {
2938 /* go deep to sub blocks */
2939 if (string && !strncmp(string, "component", 9) &&
2940 strncmp(string, "component0", 10) &&
2941 strncmp(string, "components", 10)) {
2942 rc = build_prev_component(layout, prevp, lsa,
2947 /* initialize lsa. */
2948 setstripe_args_init(lsa);
2949 lsa->lsa_first_comp = false;
2950 lsa->lsa_tgts = osts;
2953 rc = build_layout_from_yaml_node(node->cy_child, layout,
2958 if (!node->cy_string)
2961 /* skip leading lmm_ if present, to simplify parsing */
2962 if (strncmp(string, "lmm_", 4) == 0)
2965 if (node->cy_type == CYAML_TYPE_STRING) {
2966 if (!strcmp(string, "lcme_extent.e_end")) {
2967 if (!strcmp(node->cy_valuestring, "EOF") ||
2968 !strcmp(node->cy_valuestring, "eof"))
2969 lsa->lsa_comp_end = LUSTRE_EOF;
2970 } else if (!strcmp(string, "pool")) {
2971 lsa->lsa_pool_name = node->cy_valuestring;
2972 } else if (!strcmp(string, "pattern")) {
2973 if (!strcmp(node->cy_valuestring, "mdt"))
2974 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2975 if (!strcmp(node->cy_valuestring,
2976 "raid0,overstriped"))
2978 LLAPI_LAYOUT_OVERSTRIPING;
2979 } else if (!strcmp(string, "lcme_flags")) {
2980 rc = comp_str2flags(node->cy_valuestring,
2981 &lsa->lsa_comp_flags,
2982 &lsa->lsa_comp_neg_flags);
2986 * Only template flags have meaning in
2987 * the layout for a new file
2989 lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
2991 } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2992 if (!strcmp(string, "lcm_mirror_count")) {
2993 lsa->lsa_mirror_count = node->cy_valueint;
2994 } else if (!strcmp(string, "lcme_extent.e_start")) {
2995 if (node->cy_valueint == 0)
2996 lsa->lsa_first_comp = true;
2997 } else if (!strcmp(string, "lcme_extent.e_end")) {
2998 if (node->cy_valueint == -1)
2999 lsa->lsa_comp_end = LUSTRE_EOF;
3001 lsa->lsa_comp_end = node->cy_valueint;
3002 } else if (!strcmp(string, "stripe_count")) {
3003 lsa->lsa_stripe_count = node->cy_valueint;
3004 } else if (!strcmp(string, "stripe_size")) {
3005 lsa->lsa_stripe_size = node->cy_valueint;
3006 } else if (!strcmp(string, "extension_size")) {
3007 lsa->lsa_extension_size = node->cy_valueint;
3008 lsa->lsa_extension_comp = true;
3009 } else if (!strcmp(string, "stripe_offset")) {
3010 lsa->lsa_stripe_off = node->cy_valueint;
3011 } else if (!strcmp(string, "l_ost_idx")) {
3012 osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3017 node = node->cy_next;
3020 if (prevp == &prev) {
3021 rc = build_prev_component(layout, prevp, lsa, true);
3025 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3026 rc = build_component(layout, lsa, *layout != NULL);
3032 static int lfs_comp_create_from_yaml(char *template,
3033 struct llapi_layout **layout,
3034 struct lfs_setstripe_args *lsa,
3037 struct cYAML *tree = NULL, *err_rc = NULL;
3040 tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3042 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3043 progname, template);
3044 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3045 "can't parse", &err_rc);
3046 cYAML_print_tree2file(stderr, err_rc);
3047 cYAML_free_tree(err_rc);
3052 /* initialize lsa for plain file */
3053 setstripe_args_init(lsa);
3054 lsa->lsa_tgts = osts;
3056 rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3058 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3059 progname, template);
3062 /* clean clean lsa */
3063 setstripe_args_init(lsa);
3067 cYAML_free_tree(tree);
3072 * Get the extension size from the next (SEL) component and extend the
3073 * current component on it. The start of the next component is to be
3076 * \param[in] layout the current layout
3077 * \param[in] start the start of the current component
3078 * \param[in,out] end the end of the current component
3079 * \param[in] offset the offset to adjust the end position to instead of
3082 * \retval 0 - extended successfully
3083 * \retval < 0 - error
3085 static int layout_extend_comp(struct llapi_layout *layout,
3086 uint64_t start, uint64_t *end,
3089 uint64_t size, next_start, next_end;
3092 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3095 "%s setstripe: cannot move component cursor: %s\n",
3096 progname, strerror(errno));
3101 * Even if the @size will not be used below, this will fail if
3102 * this is not a SEL component - a good confirmation we are
3103 * working on right components.
3105 rc = llapi_layout_extension_size_get(layout, &size);
3108 "%s setstripe: cannot get component ext size: %s\n",
3109 progname, strerror(errno));
3113 rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3115 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3116 progname, strerror(errno));
3120 next_start += offset ?: size;
3121 rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3123 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3124 progname, strerror(errno));
3128 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3131 "%s setstripe: cannot move component cursor: %s\n",
3132 progname, strerror(errno));
3136 *end += offset ?: size;
3137 rc = llapi_layout_comp_extent_set(layout, start, *end);
3139 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3140 progname, strerror(errno));
3148 * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3149 * end of the last component in the existing file, and adjust the
3150 * first extent start of the components to be added accordingly.
3152 * In the create mode, we need to check if the first component is an extendable
3153 * SEL component and extend its length to the extension size (first component
3154 * of the PFL file is initialised at the create time, cannot be 0-lenght.
3156 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3159 struct llapi_layout *head;
3160 uint64_t start = 0, prev_end = 0;
3164 if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3169 head = llapi_layout_get_by_path(fname, 0);
3172 "%s setstripe: cannot read layout from '%s': %s\n",
3173 progname, fname, strerror(errno));
3175 } else if (errno == ENODATA) {
3177 * file without LOVEA, this component-add will be turned
3178 * into a component-create.
3180 llapi_layout_free(head);
3184 * the new layout will be added to an empty one, it
3185 * still needs to be adjusted below
3189 } else if (!llapi_layout_is_composite(head)) {
3191 "%s setstripe: '%s' not a composite file\n",
3193 llapi_layout_free(head);
3197 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3200 "%s setstripe: cannot get prev extent: %s\n",
3201 progname, strerror(errno));
3202 llapi_layout_free(head);
3206 llapi_layout_free(head);
3210 /* Make sure we use the first component of the layout to be added. */
3211 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3214 "%s setstripe: cannot move component cursor: %s\n",
3215 progname, strerror(errno));
3219 rc = llapi_layout_comp_extent_get(layout, &start, &end);
3221 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3222 progname, strerror(errno));
3226 if (start == 0 && end == 0) {
3227 rc = layout_extend_comp(layout, start, &end,
3228 comp_add ? prev_end : 0);
3233 if (start > prev_end || end < prev_end) {
3235 "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3236 progname, start, end, prev_end);
3240 rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3243 "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3244 progname, prev_end, end, strerror(errno));
3251 static int mirror_adjust_first_extents(struct mirror_args *list)
3259 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3262 list = list->m_next;
3268 static inline bool arg_is_eof(char *arg)
3270 return !strncmp(arg, "-1", strlen("-1")) ||
3271 !strncmp(arg, "EOF", strlen("EOF")) ||
3272 !strncmp(arg, "eof", strlen("eof"));
3276 * lfs_mirror_alloc() - Allocate a mirror argument structure.
3278 * Return: Valid mirror_args pointer on success and
3279 * NULL if memory allocation fails.
3281 static struct mirror_args *lfs_mirror_alloc(void)
3283 struct mirror_args *mirror = NULL;
3286 mirror = calloc(1, sizeof(*mirror));
3288 mirror->m_inherit = false;
3299 * lfs_mirror_free() - Free memory allocated for a mirror argument
3301 * @mirror: Previously allocated mirror argument structure by
3302 * lfs_mirror_alloc().
3304 * Free memory allocated for @mirror.
3308 static void lfs_mirror_free(struct mirror_args *mirror)
3310 if (mirror->m_layout)
3311 llapi_layout_free(mirror->m_layout);
3316 * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3317 * @mirror_list: Previously allocated mirror list.
3319 * Free memory allocated for @mirror_list.
3323 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3325 struct mirror_args *next_mirror = NULL;
3327 while (mirror_list) {
3328 next_mirror = mirror_list->m_next;
3329 lfs_mirror_free(mirror_list);
3330 mirror_list = next_mirror;
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,
3356 #ifndef LCME_USER_MIRROR_FLAGS
3357 /* The mirror flags can be set by users at creation time. */
3358 #define LCME_USER_MIRROR_FLAGS (LCME_FL_PREF_RW)
3362 static int lfs_setstripe_internal(int argc, char **argv,
3363 enum setstripe_origin opc)
3365 struct lfs_setstripe_args lsa = { 0 };
3366 struct llapi_stripe_param *param = NULL;
3367 struct find_param migrate_mdt_param = {
3377 unsigned long long size_units = 1;
3378 bool migrate_mode = false;
3379 bool migrate_mdt_mode = false;
3380 bool setstripe_mode = false;
3381 bool migration_block = false;
3382 __u64 migration_flags = 0;
3383 __u32 tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3384 int comp_del = 0, comp_set = 0;
3387 struct llapi_layout *layout = NULL;
3388 struct llapi_layout **lpp = &layout;
3389 bool mirror_mode = false;
3390 bool has_m_file = false;
3391 __u32 mirror_count = 0;
3392 enum mirror_flags mirror_flags = 0;
3393 struct mirror_args *mirror_list = NULL;
3394 struct mirror_args *new_mirror = NULL;
3395 struct mirror_args *last_mirror = NULL;
3396 __u16 mirror_id = 0;
3398 bool from_yaml = false;
3399 bool from_copy = false;
3400 char *template = NULL;
3401 bool foreign_mode = false;
3403 uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3404 char *mode_opt = NULL;
3405 mode_t previous_umask = 0;
3406 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3408 struct option long_opts[] = {
3409 /* find { .val = '0', .name = "null", .has_arg = no_argument }, */
3410 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
3411 /* --block is only valid in migrate mode */
3412 { .val = 'b', .name = "block", .has_arg = no_argument },
3413 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
3414 { .val = LFS_COMP_ADD_OPT,
3415 .name = "comp-add", .has_arg = no_argument },
3416 { .val = LFS_COMP_ADD_OPT,
3417 .name = "component-add", .has_arg = no_argument },
3418 { .val = LFS_COMP_DEL_OPT,
3419 .name = "comp-del", .has_arg = no_argument },
3420 { .val = LFS_COMP_DEL_OPT,
3421 .name = "component-del", .has_arg = no_argument },
3422 { .val = LFS_COMP_FLAGS_OPT,
3423 .name = "comp-flags", .has_arg = required_argument },
3424 { .val = LFS_COMP_FLAGS_OPT,
3425 .name = "component-flags",
3426 .has_arg = required_argument },
3427 { .val = LFS_COMP_SET_OPT,
3428 .name = "comp-set", .has_arg = no_argument },
3429 { .val = LFS_COMP_SET_OPT,
3430 .name = "component-set",
3431 .has_arg = no_argument},
3432 { .val = LFS_COMP_NO_VERIFY_OPT,
3433 .name = "no-verify", .has_arg = no_argument},
3434 { .val = LFS_LAYOUT_FLAGS_OPT,
3435 .name = "flags", .has_arg = required_argument},
3436 { .val = LFS_LAYOUT_FOREIGN_OPT,
3437 .name = "foreign", .has_arg = optional_argument},
3438 { .val = LFS_MIRROR_ID_OPT,
3439 .name = "mirror-id", .has_arg = required_argument},
3440 { .val = LFS_MODE_OPT,
3441 .name = "mode", .has_arg = required_argument},
3442 { .val = LFS_LAYOUT_COPY,
3443 .name = "copy", .has_arg = required_argument},
3444 { .val = 'c', .name = "stripe-count", .has_arg = required_argument},
3445 { .val = 'c', .name = "stripe_count", .has_arg = required_argument},
3446 { .val = 'c', .name = "mdt-count", .has_arg = required_argument},
3447 { .val = 'C', .name = "overstripe-count",
3448 .has_arg = required_argument},
3449 { .val = 'd', .name = "delete", .has_arg = no_argument},
3450 { .val = 'd', .name = "destroy", .has_arg = no_argument},
3451 /* --non-direct is only valid in migrate mode */
3452 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3453 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3454 { .val = 'E', .name = "component-end",
3455 .has_arg = required_argument},
3456 { .val = 'f', .name = "file", .has_arg = required_argument },
3457 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3458 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3459 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3460 { .val = 'h', .name = "help", .has_arg = no_argument },
3461 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3462 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3463 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3464 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3465 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3466 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3467 { .val = 'L', .name = "layout", .has_arg = required_argument },
3468 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3469 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3470 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3471 /* --non-block is only valid in migrate mode */
3472 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3473 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3474 { .val = 'o', .name = "ost", .has_arg = required_argument },
3475 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3476 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3477 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3479 { .val = 'p', .name = "pool", .has_arg = required_argument },
3480 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3481 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3482 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3483 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3484 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3485 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3486 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3487 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3488 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3489 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3490 /* --verbose is only valid in migrate mode */
3491 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3492 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3493 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3494 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3495 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3496 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3499 setstripe_args_init(&lsa);
3501 migrate_mode = (opc == SO_MIGRATE);
3502 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3503 setstripe_mode = (opc == SO_SETSTRIPE);
3504 if (opc == SO_MIRROR_DELETE) {
3506 mirror_flags = MF_DESTROY;
3509 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3511 while ((c = getopt_long(argc, argv,
3512 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:y:z:",
3513 long_opts, NULL)) >= 0) {
3519 case LFS_COMP_ADD_OPT:
3522 case LFS_COMP_DEL_OPT:
3525 case LFS_COMP_FLAGS_OPT:
3526 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3527 &lsa.lsa_comp_neg_flags);
3530 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3532 "%s: inverted flags are not supported\n",
3537 case LFS_COMP_SET_OPT:
3540 case LFS_COMP_NO_VERIFY_OPT:
3541 mirror_flags |= MF_NO_VERIFY;
3543 case LFS_MIRROR_ID_OPT: {
3544 unsigned long int id;
3547 id = strtoul(optarg, &end, 0);
3548 if (errno != 0 || *end != '\0' || id == 0 ||
3551 "%s %s: invalid mirror ID '%s'\n",
3552 progname, argv[0], optarg);
3556 mirror_id = (__u16)id;
3559 case LFS_LAYOUT_FLAGS_OPT: {
3562 /* check for numeric flags (foreign and mirror cases) */
3563 if (setstripe_mode && !mirror_mode && !last_mirror) {
3565 flags = strtoul(optarg, &end, 16);
3566 if (errno != 0 || *end != '\0' ||
3567 flags >= UINT32_MAX) {
3569 "%s %s: invalid hex flags '%s'\n",
3570 progname, argv[0], optarg);
3573 if (!foreign_mode) {
3575 "%s %s: hex flags must be specified with --foreign option\n",
3582 if (!mirror_mode || !last_mirror) {
3584 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3589 result = comp_str2flags(optarg, &last_mirror->m_flags,
3596 "%s: inverted flags are not supported\n",
3601 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3603 "%s: unsupported mirror flags: %s\n",
3610 case LFS_LAYOUT_FOREIGN_OPT:
3612 /* check pure numeric */
3613 type = strtoul(optarg, &end, 0);
3616 type = check_foreign_type_name(optarg);
3617 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3619 "%s %s: unrecognized foreign type '%s'\n",
3624 } else if (type >= UINT32_MAX) {
3626 "%s %s: invalid foreign type '%s'\n",
3627 progname, argv[0], optarg);
3631 foreign_mode = true;
3636 mode = strtoul(mode_opt, &end, 8);
3639 "%s %s: bad mode '%s'\n",
3640 progname, argv[0], mode_opt);
3643 previous_umask = umask(0);
3646 case LFS_LAYOUT_COPY:
3651 if (!migrate_mode) {
3653 "%s %s: -b|--block valid only for migrate command\n",
3657 migration_block = true;
3660 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3662 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3666 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3670 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3671 if (errno != 0 || *end != '\0'||
3672 lsa.lsa_stripe_count < -1 ||
3673 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3675 "%s %s: invalid stripe count '%s'\n",
3676 progname, argv[0], optarg);
3680 if (lsa.lsa_stripe_count == -1)
3681 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3684 /* delete the default striping pattern */
3686 if (opc == SO_MIRROR_SPLIT) {
3689 "%s %s: -d cannot used with -f\n",
3693 mirror_flags |= MF_DESTROY;
3697 if (!migrate_mode) {
3699 "%s %s: -D|--non-direct is valid only for migrate command\n",
3703 migration_flags |= MIGRATION_NONDIRECT;
3706 if (lsa.lsa_comp_end != 0) {
3707 result = comp_args_to_layout(lpp, &lsa, true);
3709 fprintf(stderr, "%s: invalid layout\n",
3714 setstripe_args_init_inherit(&lsa);
3717 if (arg_is_eof(optarg)) {
3718 lsa.lsa_comp_end = LUSTRE_EOF;
3720 result = llapi_parse_size(optarg,
3723 /* assume units of KB if too small */
3724 if (lsa.lsa_comp_end < 4096)
3725 lsa.lsa_comp_end *= 1024;
3727 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3729 "%s %s: invalid component end '%s'\n",
3730 progname, argv[0], optarg);
3736 if (!migrate_mode) {
3738 "--mdt-hash is valid only for migrate command\n");
3742 lsa.lsa_pattern = check_hashtype(optarg);
3743 if (lsa.lsa_pattern == 0) {
3745 "%s %s: bad stripe hash type '%s'\n",
3746 progname, argv[0], optarg);
3752 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3753 if (errno != 0 || *end != '\0' ||
3754 lsa.lsa_stripe_off < -1 ||
3755 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3757 "%s %s: invalid stripe offset '%s'\n",
3758 progname, argv[0], optarg);
3761 if (lsa.lsa_stripe_off == -1)
3762 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3765 comp_id = strtoul(optarg, &end, 0);
3766 if (*end != '\0' || comp_id == 0 ||
3767 comp_id > LCME_ID_MAX) {
3769 "%s %s: invalid component ID '%s'\n",
3770 progname, argv[0], optarg);
3775 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3777 "error: %s: invalid option: %s\n",
3778 progname, argv[optopt + 1]);
3781 if (opc == SO_MIRROR_EXTEND) {
3784 "error: %s: '-N' must exist in front of '%s'\n",
3785 progname, argv[optopt + 1]);
3788 last_mirror->m_file = optarg;
3789 last_mirror->m_count = 1;
3793 mirror_list = lfs_mirror_alloc();
3794 mirror_list->m_file = optarg;
3799 if (strcmp(argv[optind - 1], "mdt") == 0) {
3800 /* Can be only the first component */
3804 "error: 'mdt' layout can be only the first one\n");
3807 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3810 "error: 'mdt' layout size is too big\n");
3813 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3814 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
3815 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3818 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3824 if (!migrate_mode) {
3826 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3830 migrate_mdt_mode = true;
3831 lsa.lsa_nr_tgts = parse_targets(tgts,
3832 sizeof(tgts) / sizeof(__u32),
3833 lsa.lsa_nr_tgts, optarg, NULL);
3834 if (lsa.lsa_nr_tgts < 0) {
3836 "%s: invalid MDT target(s) '%s'\n",
3841 lsa.lsa_tgts = tgts;
3842 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3843 lsa.lsa_stripe_off = tgts[0];
3846 if (!migrate_mode) {
3848 "%s %s: -n|--non-block valid only for migrate command\n",
3852 migration_flags |= MIGRATION_NONBLOCK;
3855 if (opc == SO_SETSTRIPE) {
3856 opc = SO_MIRROR_CREATE;
3862 mirror_count = strtoul(optarg, &end, 0);
3863 if (errno != 0 || *end != '\0' ||
3864 mirror_count == 0 ||
3865 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3867 "error: %s: bad mirror count: %s\n",
3874 new_mirror = lfs_mirror_alloc();
3875 new_mirror->m_count = mirror_count;
3878 mirror_list = new_mirror;
3881 /* wrap up last mirror */
3882 if (!setstripe_args_specified(&lsa))
3883 last_mirror->m_inherit = true;
3884 if (lsa.lsa_comp_end == 0)
3885 lsa.lsa_comp_end = LUSTRE_EOF;
3887 result = comp_args_to_layout(lpp, &lsa, true);
3889 lfs_mirror_free(new_mirror);
3893 setstripe_args_init_inherit(&lsa);
3895 last_mirror->m_next = new_mirror;
3898 last_mirror = new_mirror;
3899 lpp = &last_mirror->m_layout;
3902 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3903 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3905 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3907 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3909 "%s %s: -o|--ost incompatible with DoM layout\n",
3914 * -o allows overstriping, and must note it because
3915 * parse_targets is shared with MDT striping, which
3916 * does not allow duplicates
3918 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3919 lsa.lsa_nr_tgts = parse_targets(tgts,
3920 sizeof(tgts) / sizeof(__u32),
3921 lsa.lsa_nr_tgts, optarg,
3923 if (lsa.lsa_nr_tgts < 0) {
3925 "%s %s: invalid OST target(s) '%s'\n",
3926 progname, argv[0], optarg);
3930 lsa.lsa_tgts = tgts;
3931 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3932 lsa.lsa_stripe_off = tgts[0];
3937 lsa.lsa_pool_name = optarg;
3939 if (strlen(lsa.lsa_pool_name) == 0 ||
3940 strncmp(lsa.lsa_pool_name, "none",
3941 LOV_MAXPOOLNAME) == 0)
3942 lsa.lsa_pool_name = NULL;
3945 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3947 /* assume units of KB if too small to be valid */
3948 if (lsa.lsa_stripe_size < 4096)
3949 lsa.lsa_stripe_size *= 1024;
3951 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3953 "%s %s: invalid stripe size '%s'\n",
3954 progname, argv[0], optarg);
3959 if (!migrate_mode) {
3961 "%s %s: -v|--verbose valid only for migrate command\n",
3965 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3966 migration_flags = MIGRATION_VERBOSE;
3976 result = llapi_parse_size(optarg,
3977 &lsa.lsa_extension_size,
3981 "%s %s: invalid extension size '%s'\n",
3982 progname, argv[0], optarg);
3986 lsa.lsa_extension_comp = true;
3989 fprintf(stderr, "%s: unrecognized option '%s'\n",
3990 progname, argv[optind - 1]);
3996 fname = argv[optind];
3998 if (optind == argc) {
3999 fprintf(stderr, "%s %s: FILE must be specified\n",
4004 /* lfs migrate $filename should keep the file's layout by default */
4005 if (migrate_mode && !layout && !from_yaml &&
4006 !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4009 if (xattr && !foreign_mode) {
4011 * only print a warning as this is harmless and will be ignored
4014 "%s %s: xattr has been specified for non-foreign layout\n",
4016 } else if (foreign_mode && !xattr) {
4018 "%s %s: xattr must be provided in foreign mode\n",
4023 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4024 comp_set || comp_id || delete || from_copy ||
4025 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4028 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4033 if (mirror_mode && mirror_count == 0) {
4035 "error: %s: --mirror-count|-N option is required\n",
4042 if (!setstripe_args_specified(&lsa))
4043 last_mirror->m_inherit = true;
4044 if (lsa.lsa_comp_end == 0)
4045 lsa.lsa_comp_end = LUSTRE_EOF;
4048 if (lsa.lsa_comp_end != 0) {
4049 result = comp_args_to_layout(lpp, &lsa, true);
4051 fprintf(stderr, "error: %s: invalid layout\n",
4058 if (mirror_flags & MF_NO_VERIFY) {
4059 if (opc != SO_MIRROR_EXTEND) {
4061 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4065 } else if (!has_m_file) {
4067 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4074 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4076 "%s %s: --component-set doesn't have component-id set\n",
4081 if ((delete + comp_set + comp_del + comp_add) > 1) {
4083 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4088 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4089 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4091 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4096 if ((comp_set || comp_del) &&
4097 (setstripe_args_specified(&lsa) || layout != NULL)) {
4099 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4104 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4106 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4111 if (comp_add || comp_del) {
4114 result = lstat(fname, &st);
4115 if (result == 0 && S_ISDIR(st.st_mode)) {
4117 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4124 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4133 "%s %s: option -E must be specified with --component-add\n",
4139 if (from_yaml && from_copy) {
4141 "%s: can't specify --yaml and --copy together\n",
4146 if ((from_yaml || from_copy) &&
4147 (setstripe_args_specified(&lsa) || layout != NULL)) {
4149 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4154 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4156 "%s %s: options --non-block and --block are mutually exclusive\n",
4161 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4162 opc != SO_MIRROR_DELETE && comp_id != 0) {
4164 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4169 if (migrate_mdt_mode) {
4170 struct lmv_user_md *lmu;
4172 /* initialize migrate mdt parameters */
4173 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4174 LMV_USER_MAGIC_SPECIFIC));
4177 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4178 progname, argv[0], strerror(ENOMEM));
4182 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4183 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4184 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4186 "%s %s: migrate should specify MDT index\n",
4191 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4192 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4193 lmu->lum_hash_type = lsa.lsa_pattern;
4195 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4196 if (lsa.lsa_pool_name) {
4197 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4198 sizeof(lmu->lum_pool_name) - 1);
4199 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4201 if (lsa.lsa_nr_tgts > 1) {
4204 if (lsa.lsa_stripe_count > 0 &&
4205 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4206 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4208 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4209 progname, lsa.lsa_stripe_count,
4215 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4216 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4217 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4218 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4220 lmu->lum_magic = LMV_USER_MAGIC;
4223 migrate_mdt_param.fp_lmv_md = lmu;
4224 migrate_mdt_param.fp_migrate = 1;
4225 } else if (!layout) {
4226 if (lsa_args_stripe_count_check(&lsa))
4229 /* initialize stripe parameters */
4230 param = calloc(1, offsetof(typeof(*param),
4231 lsp_osts[lsa.lsa_nr_tgts]));
4234 "%s %s: cannot allocate memory for parameters: %s\n",
4235 progname, argv[0], strerror(ENOMEM));
4240 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4241 param->lsp_stripe_size = lsa.lsa_stripe_size;
4242 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4243 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4244 param->lsp_stripe_count = -1;
4246 param->lsp_stripe_count = lsa.lsa_stripe_count;
4248 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4249 param->lsp_stripe_offset = -1;
4251 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4252 param->lsp_stripe_pattern =
4253 llapi_pattern_to_lov(lsa.lsa_pattern);
4254 if (param->lsp_stripe_pattern == EINVAL) {
4255 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4260 param->lsp_pool = lsa.lsa_pool_name;
4261 param->lsp_is_specific = false;
4263 if (lsa.lsa_nr_tgts > 0) {
4264 param->lsp_is_specific = true;
4265 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4266 memcpy(param->lsp_osts, tgts,
4267 sizeof(*tgts) * lsa.lsa_nr_tgts);
4272 /* generate a layout from a YAML template */
4273 result = lfs_comp_create_from_yaml(template, &layout,
4277 "error: %s: can't create composite layout from template file %s\n",
4283 if (layout != NULL || mirror_list != NULL) {
4285 result = mirror_adjust_first_extents(mirror_list);
4287 result = layout_adjust_first_extent(fname, layout,
4289 if (result == -ENODATA)
4291 else if (result != 0) {
4292 fprintf(stderr, "error: %s: invalid layout\n",
4298 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4300 layout = llapi_layout_get_by_path(template ?: fname, 0);
4303 "%s: can't create composite layout from file %s: %s\n",
4304 progname, template ?: fname,
4311 if (migrate_mdt_mode) {
4312 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4313 } else if (migrate_mode) {
4314 result = lfs_migrate(fname, migration_flags, param,
4316 } else if (comp_set != 0) {
4317 result = lfs_component_set(fname, comp_id,
4320 lsa.lsa_comp_neg_flags);
4321 } else if (comp_del != 0) {
4322 result = lfs_component_del(fname, comp_id,
4324 lsa.lsa_comp_neg_flags);
4325 } else if (comp_add != 0) {
4326 result = lfs_component_add(fname, layout);
4327 } else if (opc == SO_MIRROR_CREATE) {
4328 result = mirror_create(fname, mirror_list);
4329 } else if (opc == SO_MIRROR_EXTEND) {
4330 result = mirror_extend(fname, mirror_list,
4332 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4333 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4335 "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4339 if (lsa.lsa_pool_name)
4340 mirror_flags |= MF_COMP_POOL;
4341 else if (mirror_id != 0)
4342 comp_id = mirror_id;
4344 mirror_flags |= MF_COMP_ID;
4345 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4347 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4351 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4353 has_m_file ? mirror_list->m_file :
4355 } else if (layout) {
4356 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4362 } else if (foreign_mode) {
4363 result = llapi_file_create_foreign(fname, mode, type,
4370 result = llapi_file_open_param(fname,
4379 /* Save the first error encountered. */
4387 umask(previous_umask);
4390 free(migrate_mdt_param.fp_lmv_md);
4391 llapi_layout_free(layout);
4392 lfs_mirror_list_free(mirror_list);
4397 llapi_layout_free(layout);
4398 lfs_mirror_list_free(mirror_list);
4402 static int lfs_poollist(int argc, char **argv)
4407 return llapi_poollist(argv[1]);
4410 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4411 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4416 char *endptr = "AD";
4421 else if (str[0] == '-')
4427 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4428 long long val = strtoll(timebuf, &endptr, 0);
4433 unit *= 52; /* 52 weeks + 1 day below */
4434 case 'w': /* fallthrough */
4436 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4437 param->fp_time_margin *= (1 + unit / 52);
4438 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4439 case '\0': /* days are default unit if none used */
4440 case 'd': /* fallthrough */
4442 case 'h': /* fallthrough */
4444 case 'm': /* fallthrough */
4446 case 's': /* fallthrough */
4448 /* don't need to multiply by 1 for seconds */
4451 "%s find: bad time string '%s': %s\n",
4452 progname, timebuf, strerror(EINVAL));
4456 if (param->fp_time_margin == 0 ||
4457 (*endptr && unit < param->fp_time_margin))
4458 param->fp_time_margin = unit;
4465 fprintf(stderr, "%s find: bad time '%s': too large\n",
4475 static int str2quotaid(__u32 *id, const char *arg)
4477 unsigned long int projid_tmp = 0;
4478 char *endptr = NULL;
4480 projid_tmp = strtoul(arg, &endptr, 10);
4481 if (*endptr != '\0')
4483 /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4484 if (projid_tmp >= UINT32_MAX)
4491 static int name2uid(unsigned int *id, const char *name)
4493 struct passwd *passwd;
4495 passwd = getpwnam(name);
4498 *id = passwd->pw_uid;
4503 static int name2gid(unsigned int *id, const char *name)
4505 struct group *group;
4507 group = getgrnam(name);
4510 *id = group->gr_gid;
4515 static inline int name2projid(unsigned int *id, const char *name)
4520 static int uid2name(char **name, unsigned int id)
4522 struct passwd *passwd;
4524 passwd = getpwuid(id);
4527 *name = passwd->pw_name;
4532 static inline int gid2name(char **name, unsigned int id)
4534 struct group *group;
4536 group = getgrgid(id);
4539 *name = group->gr_name;
4544 static int name2layout(__u32 *layout, char *name)
4546 char *ptr, *layout_name;
4549 for (ptr = name; ; ptr = NULL) {
4550 layout_name = strtok(ptr, ",");
4553 if (strcmp(layout_name, "released") == 0)
4554 *layout |= LOV_PATTERN_F_RELEASED;
4555 else if (strcmp(layout_name, "raid0") == 0)
4556 *layout |= LOV_PATTERN_RAID0;
4557 else if (strcmp(layout_name, "mdt") == 0)
4558 *layout |= LOV_PATTERN_MDT;
4559 else if (strcmp(layout_name, "overstriping") == 0)
4560 *layout |= LOV_PATTERN_OVERSTRIPING;
4567 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4570 int user, group, other;
4575 mode_t previous_flags;
4577 user = group = other = 0;
4597 user = group = other = 1;
4608 who = user || group || other;
4611 usermask = umask(0022);
4616 if (*input == '-' || *input == '+' || *input == '=')
4619 /* operation is required */
4622 /* get the flags in *outmode */
4625 previous_flags = (*outmode & 0700);
4626 perm |= user ? previous_flags : 0;
4627 perm |= group ? (previous_flags >> 3) : 0;
4628 perm |= other ? (previous_flags >> 6) : 0;
4632 previous_flags = (*outmode & 0070);
4633 perm |= user ? (previous_flags << 3) : 0;
4634 perm |= group ? previous_flags : 0;
4635 perm |= other ? (previous_flags >> 3) : 0;
4639 previous_flags = (*outmode & 0007);
4640 perm |= user ? (previous_flags << 6) : 0;
4641 perm |= group ? (previous_flags << 3) : 0;
4642 perm |= other ? previous_flags : 0;
4649 /* this part is optional,
4650 * if empty perm = 0 and *outmode is not modified
4657 perm |= user ? 0400 : 0;
4658 perm |= group ? 0040 : 0;
4659 perm |= other ? 0004 : 0;
4660 /* set read permission for uog except for umask's
4663 perm |= who ? 0 : (0444 & ~usermask);
4666 perm |= user ? 0200 : 0;
4667 perm |= group ? 0020 : 0;
4668 perm |= other ? 0002 : 0;
4669 /* set write permission for uog except for umask'
4672 perm |= who ? 0 : (0222 & ~usermask);
4675 perm |= user ? 0100 : 0;
4676 perm |= group ? 0010 : 0;
4677 perm |= other ? 0001 : 0;
4678 /* set execute permission for uog except for umask'
4681 perm |= who ? 0 : (0111 & ~usermask);
4685 * Adds execute permission to 'u', 'g' and/or 'g' if
4686 * specified and either 'u', 'g' or 'o' already has
4687 * execute permissions.
4689 if ((*outmode & 0111) != 0) {
4690 perm |= user ? 0100 : 0;
4691 perm |= group ? 0010 : 0;
4692 perm |= other ? 0001 : 0;
4693 perm |= !who ? 0111 : 0;
4697 /* s is ignored if o is given, but it's not an error */
4698 if (other && !group && !user)
4700 perm |= user ? S_ISUID : 0;
4701 perm |= group ? S_ISGID : 0;
4704 /* 't' should be used when 'a' is given
4707 perm |= (!who || all) ? S_ISVTX : 0;
4708 /* using ugo with t is not an error */
4719 /* uog flags should be only one character long */
4720 if (previous_flags && (*input != '\0' && *input != ','))
4725 /* remove the flags from outmode */
4729 /* add the flags to outmode */
4733 /* set the flags of outmode to perm */
4743 static int str2mode_t(const char *input, mode_t *outmode)
4750 if (*input >= '0' && *input <= '7') {
4751 /* parse octal representation */
4756 /* look for invalid digits in octal representation */
4757 while (isdigit(*iter))
4762 *outmode = strtoul(input, &end, 8);
4764 if (errno != 0 || *outmode > 07777) {
4769 } else if (*input == '8' || *input == '9') {
4770 /* error: invalid octal number */
4773 /* parse coma seperated list of symbolic representation */
4782 rc = parse_symbolic(input, outmode, &end);
4787 } while (*end == ',');
4795 static int lfs_find(int argc, char **argv)
4800 struct find_param param = {
4803 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4805 struct option long_opts[] = {
4806 { .val = 'A', .name = "atime", .has_arg = required_argument },
4807 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4808 { .val = 'B', .name = "btime", .has_arg = required_argument },
4809 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4810 { .val = LFS_COMP_COUNT_OPT,
4811 .name = "comp-count", .has_arg = required_argument },
4812 { .val = LFS_COMP_COUNT_OPT,
4813 .name = "component-count",
4814 .has_arg = required_argument },
4815 { .val = LFS_COMP_FLAGS_OPT,
4816 .name = "comp-flags", .has_arg = required_argument },
4817 { .val = LFS_COMP_FLAGS_OPT,
4818 .name = "component-flags",
4819 .has_arg = required_argument },
4820 { .val = LFS_COMP_START_OPT,
4821 .name = "comp-start", .has_arg = required_argument },
4822 { .val = LFS_COMP_START_OPT,
4823 .name = "component-start",
4824 .has_arg = required_argument },
4825 { .val = LFS_MIRROR_STATE_OPT,
4826 .name = "mirror-state", .has_arg = required_argument },
4827 { .val = LFS_NEWERXY_OPT,
4828 .name = "newer", .has_arg = required_argument},
4829 { .val = LFS_NEWERXY_OPT,
4830 .name = "neweraa", .has_arg = required_argument},
4831 { .val = LFS_NEWERXY_OPT,
4832 .name = "neweram", .has_arg = required_argument},
4833 { .val = LFS_NEWERXY_OPT,
4834 .name = "newerac", .has_arg = required_argument},
4835 { .val = LFS_NEWERXY_OPT,
4836 .name = "newerab", .has_arg = required_argument},
4837 { .val = LFS_NEWERXY_OPT,
4838 .name = "newerma", .has_arg = required_argument},
4839 { .val = LFS_NEWERXY_OPT,
4840 .name = "newermm", .has_arg = required_argument},
4841 { .val = LFS_NEWERXY_OPT,
4842 .name = "newermc", .has_arg = required_argument},
4843 { .val = LFS_NEWERXY_OPT,
4844 .name = "newermb", .has_arg = required_argument},
4845 { .val = LFS_NEWERXY_OPT,
4846 .name = "newerca", .has_arg = required_argument},
4847 { .val = LFS_NEWERXY_OPT,
4848 .name = "newercm", .has_arg = required_argument},
4849 { .val = LFS_NEWERXY_OPT,
4850 .name = "newercc", .has_arg = required_argument},
4851 { .val = LFS_NEWERXY_OPT,
4852 .name = "newercb", .has_arg = required_argument},
4853 { .val = LFS_NEWERXY_OPT,
4854 .name = "newerba", .has_arg = required_argument},
4855 { .val = LFS_NEWERXY_OPT,
4856 .name = "newerbm", .has_arg = required_argument},
4857 { .val = LFS_NEWERXY_OPT,
4858 .name = "newerbc", .has_arg = required_argument},
4859 { .val = LFS_NEWERXY_OPT,
4860 .name = "newerbb", .has_arg = required_argument},
4861 { .val = LFS_NEWERXY_OPT,
4862 .name = "newerBa", .has_arg = required_argument},
4863 { .val = LFS_NEWERXY_OPT,
4864 .name = "newerBm", .has_arg = required_argument},
4865 { .val = LFS_NEWERXY_OPT,
4866 .name = "newerBc", .has_arg = required_argument},
4867 { .val = LFS_NEWERXY_OPT,
4868 .name = "newerBB", .has_arg = required_argument},
4869 { .val = LFS_NEWERXY_OPT,
4870 .name = "newerat", .has_arg = required_argument},
4871 { .val = LFS_NEWERXY_OPT,
4872 .name = "newermt", .has_arg = required_argument},
4873 { .val = LFS_NEWERXY_OPT,
4874 .name = "newerct", .has_arg = required_argument},
4875 { .val = LFS_NEWERXY_OPT,
4876 .name = "newerbt", .has_arg = required_argument},
4877 { .val = LFS_NEWERXY_OPT,
4878 .name = "newerBt", .has_arg = required_argument},
4879 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4880 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4881 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4882 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4883 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4884 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4885 { .val = 'E', .name = "component-end",
4886 .has_arg = required_argument },
4887 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4888 { .val = LFS_LAYOUT_FOREIGN_OPT,
4889 .name = "foreign", .has_arg = optional_argument},
4890 { .val = 'g', .name = "gid", .has_arg = required_argument },
4891 { .val = 'G', .name = "group", .has_arg = required_argument },
4892 { .val = 'h', .name = "help", .has_arg = no_argument },
4893 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4894 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4895 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4896 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4897 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4898 { .val = 'L', .name = "layout", .has_arg = required_argument },
4899 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4900 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4901 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4902 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4903 { .val = 'n', .name = "name", .has_arg = required_argument },
4904 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4905 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4906 { .val = 'O', .name = "obd", .has_arg = required_argument },
4907 { .val = 'O', .name = "ost", .has_arg = required_argument },
4908 /* no short option for pool yet, can be 'p' after 2.18 */
4909 { .val = LFS_POOL_OPT,
4910 .name = "pool", .has_arg = required_argument },
4911 { .val = '0', .name = "print0", .has_arg = no_argument },
4912 { .val = 'P', .name = "print", .has_arg = no_argument },
4913 { .val = LFS_PROJID_OPT,
4914 .name = "projid", .has_arg = required_argument },
4915 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4916 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4917 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4918 { .val = 's', .name = "size", .has_arg = required_argument },
4919 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4920 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4921 { .val = 't', .name = "type", .has_arg = required_argument },
4922 { .val = LFS_FIND_PERM,
4923 .name = "perm", .has_arg = required_argument },
4924 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4925 { .val = 'u', .name = "uid", .has_arg = required_argument },
4926 { .val = 'U', .name = "user", .has_arg = required_argument },
4927 { .val = 'z', .name = "extension-size",
4928 .has_arg = required_argument },
4929 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4930 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4931 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4945 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4946 while ((c = getopt_long_only(argc, argv,
4947 "-0A:b:B:c:C:D:E:g:G:hH:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4948 long_opts, &optidx)) >= 0) {
4953 /* '!' is part of option */
4955 * when getopt_long_only() finds a string which is not
4956 * an option nor a known option argument it returns 1
4957 * in that case if we already have found pathstart and pathend
4958 * (i.e. we have the list of pathnames),
4959 * the only supported value is "!"
4961 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4962 if (!isoption && pathend != -1) {
4964 "err: %s: filename|dirname must either precede options or follow options\n",
4969 if (!isoption && pathstart == -1)
4970 pathstart = optind - 1;
4971 if (isoption && pathstart != -1 && pathend == -1)
4972 pathend = optind - 2;
4979 * unknown; opt is "!" or path component,
4980 * checking done above.
4982 if (strcmp(optarg, "!") == 0)
4986 xtime = ¶m.fp_atime;
4987 xsign = ¶m.fp_asign;
4988 param.fp_exclude_atime = !!neg_opt;
4989 /* no break, this falls through to 'B' for btime */
4992 xtime = ¶m.fp_btime;
4993 xsign = ¶m.fp_bsign;
4994 param.fp_exclude_btime = !!neg_opt;
4996 /* no break, this falls through to 'C' for ctime */
4999 xtime = ¶m.fp_ctime;
5000 xsign = ¶m.fp_csign;
5001 param.fp_exclude_ctime = !!neg_opt;
5003 /* no break, this falls through to 'M' for mtime */
5006 xtime = ¶m.fp_mtime;
5007 xsign = ¶m.fp_msign;
5008 param.fp_exclude_mtime = !!neg_opt;
5010 rc = set_time(¶m, &t, xtime, optarg);
5011 if (rc == LONG_MAX) {
5019 if (optarg[0] == '+') {
5020 param.fp_blocks_sign = -1;
5022 } else if (optarg[0] == '-') {
5023 param.fp_blocks_sign = 1;
5027 param.fp_blocks_units = 1024;
5028 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5029 ¶m.fp_blocks_units, 0);
5031 fprintf(stderr, "error: bad blocks '%s'\n",
5035 param.fp_check_blocks = 1;
5036 param.fp_exclude_blocks = !!neg_opt;
5038 case LFS_COMP_COUNT_OPT:
5039 if (optarg[0] == '+') {
5040 param.fp_comp_count_sign = -1;
5042 } else if (optarg[0] == '-') {
5043 param.fp_comp_count_sign = 1;
5048 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5049 if (errno != 0 || *endptr != '\0' ||
5050 param.fp_comp_count > UINT32_MAX) {
5052 "error: bad component count '%s'\n",
5056 param.fp_check_comp_count = 1;
5057 param.fp_exclude_comp_count = !!neg_opt;
5059 case LFS_COMP_FLAGS_OPT:
5060 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5061 ¶m.fp_comp_neg_flags);
5064 "error: bad component flags '%s'\n",
5068 param.fp_check_comp_flags = 1;
5070 __u32 flags = param.fp_comp_neg_flags;
5072 param.fp_comp_neg_flags = param.fp_comp_flags;
5073 param.fp_comp_flags = flags;
5076 case LFS_COMP_START_OPT:
5077 if (optarg[0] == '+') {
5078 param.fp_comp_start_sign = -1;
5080 } else if (optarg[0] == '-') {
5081 param.fp_comp_start_sign = 1;
5085 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5086 ¶m.fp_comp_start_units, 0);
5089 "error: bad component start '%s'\n",
5093 param.fp_check_comp_start = 1;
5094 param.fp_exclude_comp_start = !!neg_opt;
5096 case LFS_MIRROR_STATE_OPT:
5097 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5098 ¶m.fp_mirror_neg_state);
5101 "error: bad mirrored file state '%s'\n",
5105 param.fp_check_mirror_state = 1;
5107 __u16 state = param.fp_mirror_neg_state;
5109 param.fp_mirror_neg_state =
5110 param.fp_mirror_state;
5111 param.fp_mirror_state = state;
5115 if (optarg[0] == '+') {
5116 param.fp_stripe_count_sign = -1;
5118 } else if (optarg[0] == '-') {
5119 param.fp_stripe_count_sign = 1;
5124 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5125 if (errno != 0 || *endptr != '\0' ||
5126 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5128 "error: bad stripe_count '%s'\n",
5133 param.fp_check_stripe_count = 1;
5134 param.fp_exclude_stripe_count = !!neg_opt;
5138 param.fp_max_depth = strtol(optarg, 0, 0);
5139 if (errno != 0 || param.fp_max_depth < 0) {
5141 "error: bad maxdepth '%s'\n",
5148 if (optarg[0] == '+') {
5149 param.fp_comp_end_sign = -1;
5151 } else if (optarg[0] == '-') {
5152 param.fp_comp_end_sign = 1;
5156 if (arg_is_eof(optarg)) {
5157 param.fp_comp_end = LUSTRE_EOF;
5158 param.fp_comp_end_units = 1;
5161 rc = llapi_parse_size(optarg,
5163 ¶m.fp_comp_end_units, 0);
5164 /* assume units of KB if too small */
5165 if (param.fp_comp_end < 4096)
5166 param.fp_comp_end *= 1024;
5170 "error: bad component end '%s'\n",
5174 param.fp_check_comp_end = 1;
5175 param.fp_exclude_comp_end = !!neg_opt;
5177 case LFS_LAYOUT_FOREIGN_OPT: {
5178 /* all types by default */
5179 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5182 /* check pure numeric */
5183 type = strtoul(optarg, &endptr, 0);
5186 type = check_foreign_type_name(optarg);
5187 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5189 "%s %s: unknown foreign type '%s'\n",
5194 } else if (type >= UINT32_MAX) {
5196 "%s %s: invalid foreign type '%s'\n",
5197 progname, argv[0], optarg);
5201 param.fp_foreign_type = type;
5202 param.fp_check_foreign = 1;
5203 param.fp_exclude_foreign = !!neg_opt;
5206 case LFS_NEWERXY_OPT: {
5212 time_t ref = time(NULL);
5214 /* no need to check bad options, they won't get here */
5215 if (strlen(long_opts[optidx].name) == 7) {
5216 x = long_opts[optidx].name[5];
5217 y = long_opts[optidx].name[6];
5221 static const char *const fmts[] = {
5222 "%Y-%m-%d %H:%M:%S",
5225 "%H:%M:%S", /* sometime today */
5234 for (i = 0; fmts[i] != NULL; i++) {
5237 /* Init for times relative to today */
5238 if (strncmp(fmts[i], "%H", 2) == 0)
5239 localtime_r(&ref, &tm);
5241 memset(&tm, 0, sizeof(tm));
5242 ptr = strptime(optarg, fmts[i], &tm);
5244 while (ptr && isspace(*ptr))
5246 if (ptr == optarg + strlen(optarg)) {
5254 "%s: invalid time '%s'\n",
5257 "supported formats are:\n ");
5258 for (i = 0; fmts[i] != NULL; i++)
5259 fprintf(stderr, "'%s', ",
5261 fprintf(stderr, "\n");
5267 } else if (y == 'b' || y == 'B') {
5270 rc = llapi_get_lum_file(optarg, NULL, &stx,
5272 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5273 if (!(stx.stx_mask & STATX_BTIME))
5278 "%s: get btime failed '%s': %s\n",
5284 ref = stx.stx_btime.tv_sec;
5286 struct stat statbuf;
5288 if (stat(optarg, &statbuf) < 0) {
5290 "%s: cannot stat file '%s': %s\n",
5299 ref = statbuf.st_atime;
5302 ref = statbuf.st_mtime;
5305 ref = statbuf.st_ctime;
5309 "%s: invalid Y argument: '%c'\n",
5318 xidx = NEWERXY_ATIME;
5321 xidx = NEWERXY_MTIME;
5324 xidx = NEWERXY_CTIME;
5328 xidx = NEWERXY_BTIME;
5332 "%s: invalid X argument: '%c'\n",
5339 newery = ¶m.fp_newery[xidx][negidx];
5345 *newery = *newery > ref ? ref : *newery;
5347 *newery = *newery > ref ? *newery : ref;
5349 param.fp_newerxy = 1;
5354 rc = name2gid(¶m.fp_gid, optarg);
5356 if (str2quotaid(¶m.fp_gid, optarg)) {
5358 "Group/GID: %s cannot be found.\n",
5364 param.fp_exclude_gid = !!neg_opt;
5365 param.fp_check_gid = 1;
5368 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5369 ¶m.fp_hash_exflags,
5370 ¶m.fp_hash_type);
5375 if (param.fp_hash_inflags || param.fp_hash_exflags)
5376 param.fp_check_hash_flag = 1;
5377 param.fp_exclude_hash_type = !!neg_opt;
5383 ret = name2layout(¶m.fp_layout, optarg);
5386 param.fp_exclude_layout = !!neg_opt;
5387 param.fp_check_layout = 1;
5391 rc = name2uid(¶m.fp_uid, optarg);
5393 if (str2quotaid(¶m.fp_uid, optarg)) {
5395 "User/UID: %s cannot be found.\n",
5401 param.fp_exclude_uid = !!neg_opt;
5402 param.fp_check_uid = 1;
5405 param.fp_pattern = (char *)optarg;
5406 param.fp_exclude_pattern = !!neg_opt;
5409 if (optarg[0] == '+') {
5410 param.fp_mirror_count_sign = -1;
5412 } else if (optarg[0] == '-') {
5413 param.fp_mirror_count_sign = 1;
5418 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5419 if (errno != 0 || *endptr != '\0' ||
5420 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5422 "error: bad mirror count '%s'\n",
5426 param.fp_check_mirror_count = 1;
5427 param.fp_exclude_mirror_count = !!neg_opt;
5432 char *buf, *token, *next, *p;
5436 buf = strdup(optarg);
5442 param.fp_exclude_obd = !!neg_opt;
5445 while (token && *token) {
5446 token = strchr(token, ',');
5453 param.fp_exclude_mdt = !!neg_opt;
5454 param.fp_num_alloc_mdts += len;
5455 tmp = realloc(param.fp_mdt_uuid,
5456 param.fp_num_alloc_mdts *
5457 sizeof(*param.fp_mdt_uuid));
5463 param.fp_mdt_uuid = tmp;
5465 param.fp_exclude_obd = !!neg_opt;
5466 param.fp_num_alloc_obds += len;
5467 tmp = realloc(param.fp_obd_uuid,
5468 param.fp_num_alloc_obds *
5469 sizeof(*param.fp_obd_uuid));
5475 param.fp_obd_uuid = tmp;
5477 for (token = buf; token && *token; token = next) {
5478 struct obd_uuid *puuid;
5482 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5485 ¶m.fp_obd_uuid[param.fp_num_obds++];
5487 p = strchr(token, ',');
5494 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5499 strncpy(puuid->uuid, token,
5500 sizeof(puuid->uuid));
5507 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5511 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5513 "Pool name %s is too long (max %d)\n",
5514 optarg, LOV_MAXPOOLNAME);
5519 * We do check for empty pool because empty pool
5520 * is used to find V1 LOV attributes
5522 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5523 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5524 param.fp_exclude_pool = !!neg_opt;
5525 param.fp_check_pool = 1;
5527 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5528 case 'p': /* want this for --pool, to match getstripe/find */
5530 "warning: -p deprecated, use --print0 or -0\n");
5533 param.fp_zero_end = 1;
5535 case 'P': /* we always print, this option is a no-op */
5537 case LFS_PROJID_OPT:
5538 rc = name2projid(¶m.fp_projid, optarg);
5540 if (str2quotaid(¶m.fp_projid, optarg)) {
5542 "Invalid project ID: %s\n",
5548 param.fp_exclude_projid = !!neg_opt;
5549 param.fp_check_projid = 1;
5552 if (optarg[0] == '+') {
5553 param.fp_size_sign = -1;
5555 } else if (optarg[0] == '-') {
5556 param.fp_size_sign = 1;
5560 ret = llapi_parse_size(optarg, ¶m.fp_size,
5561 ¶m.fp_size_units, 0);
5563 fprintf(stderr, "error: bad file size '%s'\n",
5567 param.fp_check_size = 1;
5568 param.fp_exclude_size = !!neg_opt;
5571 if (optarg[0] == '+') {
5572 param.fp_stripe_size_sign = -1;
5574 } else if (optarg[0] == '-') {
5575 param.fp_stripe_size_sign = 1;
5579 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5580 ¶m.fp_stripe_size_units, 0);
5581 /* assume units of KB if too small to be valid */
5582 if (param.fp_stripe_size < 4096)
5583 param.fp_stripe_size *= 1024;
5585 fprintf(stderr, "error: bad stripe_size '%s'\n",
5589 param.fp_check_stripe_size = 1;
5590 param.fp_exclude_stripe_size = !!neg_opt;
5593 param.fp_exclude_type = !!neg_opt;
5594 switch (optarg[0]) {
5596 param.fp_type = S_IFBLK;
5599 param.fp_type = S_IFCHR;
5602 param.fp_type = S_IFDIR;
5605 param.fp_type = S_IFREG;
5608 param.fp_type = S_IFLNK;
5611 param.fp_type = S_IFIFO;
5614 param.fp_type = S_IFSOCK;
5617 fprintf(stderr, "%s: bad type '%s'\n",
5624 param.fp_exclude_perm = !!neg_opt;
5625 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5626 if (*optarg == '/') {
5627 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5629 } else if (*optarg == '-') {
5630 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5634 if (str2mode_t(optarg, ¶m.fp_perm)) {
5635 fprintf(stderr, "error: invalid mode '%s'\n",
5642 if (optarg[0] == '+') {
5643 param.fp_mdt_count_sign = -1;
5645 } else if (optarg[0] == '-') {
5646 param.fp_mdt_count_sign = 1;
5651 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5652 if (errno != 0 || *endptr != '\0' ||
5653 param.fp_mdt_count >= UINT32_MAX) {
5654 fprintf(stderr, "error: bad mdt_count '%s'\n",
5659 param.fp_check_mdt_count = 1;
5660 param.fp_exclude_mdt_count = !!neg_opt;
5663 if (optarg[0] == '+') {
5664 param.fp_ext_size_sign = -1;
5666 } else if (optarg[0] == '-') {
5667 param.fp_ext_size_sign = 1;
5671 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5672 ¶m.fp_ext_size_units, 0);
5674 fprintf(stderr, "error: bad ext-size '%s'\n",
5678 param.fp_ext_size /= SEL_UNIT_SIZE;
5679 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5680 param.fp_check_ext_size = 1;
5681 param.fp_exclude_ext_size = !!neg_opt;
5684 fprintf(stderr, "%s: unrecognized option '%s'\n",
5685 progname, argv[optind - 1]);
5692 if (pathstart == -1) {
5693 fprintf(stderr, "error: %s: no filename|pathname\n",
5697 } else if (pathend == -1) {
5703 rc = llapi_find(argv[pathstart], ¶m);
5706 pathbad = pathstart;
5708 } while (++pathstart < pathend);
5711 fprintf(stderr, "%s: failed for '%s': %s\n",
5712 progname, argv[pathbad], strerror(-rc));
5715 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5716 free(param.fp_obd_uuid);
5718 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5719 free(param.fp_mdt_uuid);
5724 static int lfs_getstripe_internal(int argc, char **argv,
5725 struct find_param *param)
5727 struct option long_opts[] = {
5728 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5729 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5730 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5731 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5732 { .val = LFS_COMP_COUNT_OPT,
5733 .name = "comp-count", .has_arg = no_argument },
5734 { .val = LFS_COMP_COUNT_OPT,
5735 .name = "component-count", .has_arg = no_argument },
5736 { .val = LFS_COMP_FLAGS_OPT,
5737 .name = "comp-flags", .has_arg = optional_argument },
5738 { .val = LFS_COMP_FLAGS_OPT,
5739 .name = "component-flags", .has_arg = optional_argument },
5740 { .val = LFS_COMP_START_OPT,
5741 .name = "comp-start", .has_arg = optional_argument },
5742 { .val = LFS_COMP_START_OPT,
5743 .name = "component-start", .has_arg = optional_argument },
5744 { .val = LFS_MIRROR_INDEX_OPT,
5745 .name = "mirror-index", .has_arg = required_argument },
5746 { .val = LFS_MIRROR_ID_OPT,
5747 .name = "mirror-id", .has_arg = required_argument },
5748 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5749 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5750 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5751 { .val = 'd', .name = "directory", .has_arg = no_argument },
5752 { .val = 'D', .name = "default", .has_arg = no_argument },
5753 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5754 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5755 { .val = 'F', .name = "fid", .has_arg = no_argument },
5756 { .val = 'g', .name = "generation", .has_arg = no_argument },
5757 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5758 { .val = 'h', .name = "help", .has_arg = no_argument },
5759 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5760 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5761 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5762 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5763 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5764 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5765 { .val = 'L', .name = "layout", .has_arg = no_argument },
5766 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5767 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5768 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5769 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5770 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5771 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5772 { .val = 'O', .name = "obd", .has_arg = required_argument },
5773 { .val = 'O', .name = "ost", .has_arg = required_argument },
5774 { .val = 'p', .name = "pool", .has_arg = no_argument },
5775 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5776 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5777 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5778 { .val = 'R', .name = "raw", .has_arg = no_argument },
5779 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5780 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5781 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5782 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5783 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5784 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5785 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5786 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5787 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5788 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5789 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5793 int pathstart = -1, pathend = -1;
5797 while ((c = getopt_long(argc, argv,
5798 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5799 long_opts, NULL)) != -1) {
5803 /* '!' is part of option */
5804 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5805 if (!isoption && pathend != -1) {
5807 "error: %s: filename|dirname must either precede options or follow options\n",
5811 if (!isoption && pathstart == -1)
5812 pathstart = optind - 1;
5813 if (isoption && pathstart != -1 && pathend == -1)
5814 pathend = optind - 2;
5818 /* unknown: opt is "!" */
5819 if (strcmp(optarg, "!") == 0)
5823 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5824 param->fp_verbose |= VERBOSE_COUNT;
5825 param->fp_max_depth = 0;
5828 case LFS_COMP_COUNT_OPT:
5829 param->fp_verbose |= VERBOSE_COMP_COUNT;
5830 param->fp_max_depth = 0;
5832 case LFS_COMP_FLAGS_OPT:
5834 rc = comp_str2flags(optarg,
5835 ¶m->fp_comp_flags,
5836 ¶m->fp_comp_neg_flags);
5839 "error: %s bad component flags '%s'.\n",
5843 param->fp_check_comp_flags = 1;
5845 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5846 param->fp_max_depth = 0;
5849 case LFS_COMP_START_OPT:
5852 if (tmp[0] == '+') {
5853 param->fp_comp_start_sign = -1;
5855 } else if (tmp[0] == '-') {
5856 param->fp_comp_start_sign = 1;
5859 rc = llapi_parse_size(tmp,
5860 ¶m->fp_comp_start,
5861 ¶m->fp_comp_start_units, 0);
5864 "error: %s bad component start '%s'.\n",
5868 param->fp_check_comp_start = 1;
5870 param->fp_verbose |= VERBOSE_COMP_START;
5871 param->fp_max_depth = 0;
5874 case LFS_MIRROR_INDEX_OPT: {
5875 unsigned long int mirror_index;
5877 if (optarg[0] == '+') {
5878 param->fp_mirror_index_sign = -1;
5880 } else if (optarg[0] == '-') {
5881 param->fp_mirror_index_sign = 1;
5886 mirror_index = strtoul(optarg, &end, 0);
5887 if (errno != 0 || *end != '\0' ||
5888 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5889 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5891 "%s %s: invalid mirror index '%s'\n",
5892 progname, argv[0], optarg);
5896 param->fp_mirror_index = (__u16)mirror_index;
5898 if (param->fp_mirror_id != 0) {
5900 "%s %s: can't specify both mirror index and mirror ID\n",
5904 param->fp_check_mirror_index = 1;
5905 param->fp_exclude_mirror_index = !!neg_opt;
5908 case LFS_MIRROR_ID_OPT: {
5909 unsigned long int mirror_id;
5911 if (optarg[0] == '+') {
5912 param->fp_mirror_id_sign = -1;
5914 } else if (optarg[0] == '-') {
5915 param->fp_mirror_id_sign = 1;
5920 mirror_id = strtoul(optarg, &end, 0);
5921 if (errno != 0 || *end != '\0' ||
5922 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5923 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5925 "%s %s: invalid mirror ID '%s'\n",
5926 progname, argv[0], optarg);
5930 param->fp_mirror_id = (__u16)mirror_id;
5932 if (param->fp_mirror_index != 0) {
5934 "%s %s: can't specify both mirror index and mirror ID\n",
5938 param->fp_check_mirror_id = 1;
5939 param->fp_exclude_mirror_id = !!neg_opt;
5943 param->fp_max_depth = 0;
5946 param->fp_get_default_lmv = 1;
5951 if (tmp[0] == '+') {
5952 param->fp_comp_end_sign = -1;
5954 } else if (tmp[0] == '-') {
5955 param->fp_comp_end_sign = 1;
5959 if (arg_is_eof(tmp)) {
5960 param->fp_comp_end = LUSTRE_EOF;
5961 param->fp_comp_end_units = 1;
5964 rc = llapi_parse_size(tmp,
5965 ¶m->fp_comp_end,
5966 ¶m->fp_comp_end_units, 0);
5967 /* assume units of KB if too small */
5968 if (param->fp_comp_end < 4096)
5969 param->fp_comp_end *= 1024;
5973 "error: %s bad component end '%s'.\n",
5977 param->fp_check_comp_end = 1;
5979 param->fp_verbose |= VERBOSE_COMP_END;
5980 param->fp_max_depth = 0;
5984 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5985 param->fp_verbose |= VERBOSE_DFID;
5986 param->fp_max_depth = 0;
5990 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5991 param->fp_verbose |= VERBOSE_GENERATION;
5992 param->fp_max_depth = 0;
5996 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5997 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
5998 param->fp_max_depth = 0;
6003 param->fp_comp_id = strtoul(optarg, &end, 0);
6004 if (*end != '\0' || param->fp_comp_id == 0 ||
6005 param->fp_comp_id > LCME_ID_MAX) {
6007 "error: %s bad component id '%s'\n",
6011 param->fp_check_comp_id = 1;
6013 param->fp_max_depth = 0;
6014 param->fp_verbose |= VERBOSE_COMP_ID;
6018 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6019 param->fp_verbose |= VERBOSE_PATTERN;
6020 param->fp_max_depth = 0;
6023 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6026 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6029 if (!(param->fp_verbose & VERBOSE_DETAIL))
6030 param->fp_max_depth = 0;
6031 param->fp_verbose |= VERBOSE_MDTINDEX;
6034 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6035 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6036 param->fp_max_depth = 0;
6040 if (param->fp_obd_uuid) {
6042 "error: %s: only one obduuid allowed",
6046 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6049 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6050 param->fp_verbose |= VERBOSE_POOL;
6051 param->fp_max_depth = 0;
6058 param->fp_recursive = 1;
6064 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6065 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6066 param->fp_max_depth = 0;
6070 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6076 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6077 param->fp_verbose |= VERBOSE_EXT_SIZE;
6078 param->fp_max_depth = 0;
6082 fprintf(stderr, "%s: unrecognized option '%s'\n",
6083 progname, argv[optind - 1]);
6089 if (pathstart == -1) {
6090 fprintf(stderr, "error: %s: no filename|pathname\n",
6093 } else if (pathend == -1) {
6101 if (param->fp_recursive)
6102 param->fp_max_depth = -1;
6103 else if (param->fp_verbose & VERBOSE_DETAIL)
6104 param->fp_max_depth = 1;
6106 if (!param->fp_verbose)
6107 param->fp_verbose = VERBOSE_DEFAULT;
6108 if (param->fp_quiet)
6109 param->fp_verbose = VERBOSE_OBJID;
6112 rc = llapi_getstripe(argv[pathstart], param);
6113 } while (++pathstart < pathend && !rc);
6116 fprintf(stderr, "error: %s failed for %s.\n",
6117 argv[0], argv[optind - 1]);
6121 static int lfs_tgts(int argc, char **argv)
6123 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6124 struct find_param param;
6125 int index = 0, rc = 0;
6130 if (argc == 2 && !realpath(argv[1], path)) {
6132 fprintf(stderr, "error: invalid path '%s': %s\n",
6133 argv[1], strerror(-rc));
6137 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6138 /* Check if we have a mount point */
6139 if (mntdir[0] == '\0')
6142 memset(¶m, 0, sizeof(param));
6143 if (!strcmp(argv[0], "mdts"))
6144 param.fp_get_lmv = 1;
6146 rc = llapi_ostlist(mntdir, ¶m);
6148 fprintf(stderr, "error: %s: failed on %s\n",
6151 if (path[0] != '\0')
6153 memset(mntdir, 0, PATH_MAX);
6159 static int lfs_getstripe(int argc, char **argv)
6161 struct find_param param = { 0 };
6163 param.fp_max_depth = 1;
6164 return lfs_getstripe_internal(argc, argv, ¶m);
6168 static int lfs_getdirstripe(int argc, char **argv)
6170 struct find_param param = { 0 };
6171 struct option long_opts[] = {
6172 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6173 { .val = 'D', .name = "default", .has_arg = no_argument },
6174 { .val = 'h', .name = "help", .has_arg = no_argument },
6175 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6176 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6177 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6178 { .val = 'O', .name = "obd", .has_arg = required_argument },
6179 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6180 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6181 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6182 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6183 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6184 { .val = LFS_INHERIT_RR_OPT,
6185 .name = "max-inherit-rr", .has_arg = no_argument },
6189 param.fp_get_lmv = 1;
6191 while ((c = getopt_long(argc, argv,
6192 "cDhHimO:rtTvXy", long_opts, NULL)) != -1) {
6196 param.fp_verbose |= VERBOSE_COUNT;
6199 param.fp_get_default_lmv = 1;
6201 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6204 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6208 param.fp_verbose |= VERBOSE_HASH_TYPE;
6210 case 'i': /* fallthrough */
6212 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6215 if (param.fp_obd_uuid) {
6217 "%s: only one obduuid allowed",
6221 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6224 param.fp_recursive = 1;
6227 param.fp_verbose |= VERBOSE_DETAIL;
6230 param.fp_verbose |= VERBOSE_INHERIT;
6232 case LFS_INHERIT_RR_OPT:
6233 param.fp_verbose |= VERBOSE_INHERIT_RR;
6239 fprintf(stderr, "%s: unrecognized option '%s'\n",
6240 progname, argv[optind - 1]);
6250 if (param.fp_recursive)
6251 param.fp_max_depth = -1;
6253 if (!param.fp_verbose)
6254 param.fp_verbose = VERBOSE_DEFAULT;
6257 rc = llapi_getstripe(argv[optind], ¶m);
6258 } while (++optind < argc && !rc);
6261 fprintf(stderr, "error: %s failed for %s.\n",
6262 argv[0], argv[optind - 1]);
6267 MNTDF_INODES = 0x0001,
6268 MNTDF_COOKED = 0x0002,
6269 MNTDF_LAZY = 0x0004,
6270 MNTDF_VERBOSE = 0x0008,
6271 MNTDF_SHOW = 0x0010,
6272 MNTDF_DECIMAL = 0x0020,
6275 #define COOK(value, base) \
6278 while (value > base) { \
6286 #define CDF "%11llu"
6287 #define HDF "%8.1f%c"
6291 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6293 double avail, used, ratio = 0;
6296 avail = st->os_ffree;
6297 used = st->os_files - st->os_ffree;
6299 avail = st->os_bavail;
6300 used = st->os_blocks - st->os_bfree;
6302 if (avail + used > 0)
6303 ratio = used / (used + avail) * 100;
6305 /* Round up to match df(1) usage percentage */
6306 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6310 * This is to identify various problem states for "lfs df" if .osn_err = true,
6311 * so only show flags reflecting those states by default. Informational states
6312 * are only shown with "-v" and use lower-case names to distinguish them.
6313 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6315 static struct obd_statfs_state_names {
6316 enum obd_statfs_state osn_state;
6317 const char osn_name;
6320 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6321 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6322 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6323 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6324 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6325 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6326 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6327 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6328 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6331 static int showdf(char *mntdir, struct obd_statfs *stat,
6332 char *uuid, enum mntdf_flags flags,
6333 char *type, int index, int rc)
6335 long long avail, used, total;
6337 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6338 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6339 char tbuf[3 * sizeof(__u64)];
6340 char ubuf[3 * sizeof(__u64)];
6341 char abuf[3 * sizeof(__u64)];
6342 char rbuf[3 * sizeof(__u64)];
6349 if (flags & MNTDF_INODES) {
6350 avail = stat->os_ffree;
6351 used = stat->os_files - stat->os_ffree;
6352 total = stat->os_files;
6354 int shift = flags & MNTDF_COOKED ? 0 : 10;
6356 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6357 used = ((stat->os_blocks - stat->os_bfree) *
6358 stat->os_bsize) >> shift;
6359 total = (stat->os_blocks * stat->os_bsize) >> shift;
6362 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6364 if (flags & MNTDF_COOKED) {
6365 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6369 cook_val = (double)total;
6370 i = COOK(cook_val, base);
6372 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6375 snprintf(tbuf, sizeof(tbuf), CDF, total);
6377 cook_val = (double)used;
6378 i = COOK(cook_val, base);
6380 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6383 snprintf(ubuf, sizeof(ubuf), CDF, used);
6385 cook_val = (double)avail;
6386 i = COOK(cook_val, base);
6388 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6391 snprintf(abuf, sizeof(abuf), CDF, avail);
6393 snprintf(tbuf, sizeof(tbuf), CDF, total);
6394 snprintf(ubuf, sizeof(tbuf), CDF, used);
6395 snprintf(abuf, sizeof(tbuf), CDF, avail);
6398 sprintf(rbuf, RDF, ratio);
6399 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6400 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6402 printf("[%s:%d]", type, index);
6404 if (stat->os_state) {
6408 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6409 if (oss_names[i].osn_state & stat->os_state &&
6410 (oss_names[i].osn_err ||
6411 flags & MNTDF_VERBOSE))
6412 printf("%c", oss_names[i].osn_name);
6419 printf(UUF": inactive device\n", uuid);
6422 printf(UUF": %s\n", uuid, strerror(-rc));
6429 struct ll_stat_type {
6434 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6436 struct ll_statfs_data {
6438 struct obd_statfs sd_st;
6441 struct ll_statfs_buf {
6443 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6446 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6447 int ops, struct ll_statfs_buf *lsb)
6449 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6450 struct obd_uuid uuid_buf;
6451 char *poolname = NULL;
6452 struct ll_stat_type types[] = {
6453 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6454 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6455 { .st_name = NULL } };
6456 struct ll_stat_type *tp;
6457 __u64 ost_files = 0;
6458 __u64 ost_ffree = 0;
6466 poolname = strchr(pool, '.');
6468 if (strncmp(fsname, pool, strlen(fsname))) {
6469 fprintf(stderr, "filesystem name incorrect\n");
6477 fd = open(mntdir, O_RDONLY);
6480 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6485 if (flags & MNTDF_SHOW) {
6486 if (flags & MNTDF_INODES)
6487 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6488 "UUID", "Inodes", "IUsed", "IFree",
6489 "IUse%", "Mounted on");
6491 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6493 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6494 "Used", "Available", "Use%", "Mounted on");
6497 for (tp = types; tp->st_name != NULL; tp++) {
6498 bool have_ost = false;
6500 if (!(tp->st_op & ops))
6503 for (index = 0; ; index++) {
6504 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6505 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6506 type = flags & MNTDF_LAZY ?
6507 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6508 rc2 = llapi_obd_fstatfs(fd, type, index,
6509 &stat_buf, &uuid_buf);
6514 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6515 if (!(flags & MNTDF_VERBOSE))
6517 } else if (rc2 < 0 && rc == 0) {
6522 * If we have OSTs then don't report MDT block counts.
6523 * For MDT-only filesystems the expectation is that all
6524 * layouts have a DoM component. For filesystems with
6525 * OSTs, files are not necessarily going to store data
6526 * on MDTs, and MDT space is limited to a fraction of
6527 * OST space, so don't include it in the summary.
6529 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6536 if (poolname && tp->st_op == LL_STATFS_LOV &&
6537 llapi_search_ost(fsname, poolname,
6538 obd_uuid2str(&uuid_buf)) != 1)
6542 * the llapi_obd_fstatfs() call may have returned with
6543 * an error, but if it filled in uuid_buf we will at
6544 * lease use that to print out a message for that OBD.
6545 * If we didn't get anything in the uuid_buf, then fill
6546 * it in so that we can print an error message.
6548 if (uuid_buf.uuid[0] == '\0')
6549 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6550 "%s%04x", tp->st_name, index);
6552 lsb->sb_buf[lsb->sb_count].sd_index = index;
6553 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6556 if (flags & MNTDF_SHOW)
6557 showdf(mntdir, &stat_buf,
6558 obd_uuid2str(&uuid_buf), flags,
6559 tp->st_name, index, rc2);
6564 if (tp->st_op == LL_STATFS_LMV) {
6565 sum.os_ffree += stat_buf.os_ffree;
6566 sum.os_files += stat_buf.os_files;
6567 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6568 ost_files += stat_buf.os_files;
6569 ost_ffree += stat_buf.os_ffree;
6571 sum.os_blocks += stat_buf.os_blocks *
6573 sum.os_bfree += stat_buf.os_bfree *
6575 sum.os_bavail += stat_buf.os_bavail *
6583 * If we have _some_ OSTs, but don't have as many free objects on the
6584 * OST as inodes on the MDTs, reduce the reported number of inodes
6585 * to compensate, so that the "inodes in use" number is correct.
6586 * This should be kept in sync with ll_statfs_internal().
6588 if (ost_files && ost_ffree < sum.os_ffree) {
6589 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6590 sum.os_ffree = ost_ffree;
6592 if (flags & MNTDF_SHOW) {
6594 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6602 LAYOUT_INHERIT_UNSET = -2,
6606 static int lfs_setdirstripe(int argc, char **argv)
6609 struct lfs_setstripe_args lsa = { 0 };
6610 struct llapi_stripe_param *param = NULL;
6611 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6614 char *mode_opt = NULL;
6615 bool default_stripe = false;
6616 bool delete = false;
6617 bool foreign_mode = false;
6618 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6619 mode_t previous_mode = 0;
6621 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6622 int max_inherit = LAYOUT_INHERIT_UNSET;
6623 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6624 struct option long_opts[] = {
6625 { .val = 'c', .name = "count", .has_arg = required_argument },
6626 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6627 { .val = 'd', .name = "delete", .has_arg = no_argument },
6628 { .val = 'D', .name = "default", .has_arg = no_argument },
6629 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6630 { .val = LFS_LAYOUT_FLAGS_OPT,
6631 .name = "flags", .has_arg = required_argument },
6632 { .val = LFS_LAYOUT_FOREIGN_OPT,
6633 .name = "foreign", .has_arg = optional_argument},
6634 { .val = 'h', .name = "help", .has_arg = no_argument },
6635 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6636 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6637 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6638 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6640 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6641 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6642 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6644 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6645 { .val = 'i', .name = "index", .has_arg = required_argument },
6647 { .val = 'o', .name = "mode", .has_arg = required_argument },
6648 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6649 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6651 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6652 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6653 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
6654 { .val = LFS_INHERIT_RR_OPT,
6655 .name = "max-inherit-rr", .has_arg = required_argument},
6656 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6660 setstripe_args_init(&lsa);
6662 while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
6663 long_opts, NULL)) >= 0) {
6671 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6672 if (errno != 0 || *end != '\0' ||
6673 lsa.lsa_stripe_count < -1 ||
6674 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6676 "%s: invalid stripe count '%s'\n",
6683 default_stripe = true;
6686 default_stripe = true;
6688 case LFS_LAYOUT_FOREIGN_OPT:
6690 /* check pure numeric */
6691 type = strtoul(optarg, &end, 0);
6694 type = check_foreign_type_name(optarg);
6695 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6697 "%s %s: unknown foreign type '%s'\n",
6702 } else if (type >= UINT32_MAX) {
6704 "%s %s: invalid foreign type '%s'\n",
6705 progname, argv[0], optarg);
6709 foreign_mode = true;
6711 case LFS_LAYOUT_FLAGS_OPT:
6713 flags = strtoul(optarg, &end, 16);
6714 if (errno != 0 || *end != '\0' ||
6715 flags >= UINT32_MAX) {
6717 "%s %s: invalid hex flags '%s'\n",
6718 progname, argv[0], optarg);
6721 if (!foreign_mode) {
6723 "%s %s: hex flags must be specified with --foreign option\n",
6728 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6731 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6735 lsa.lsa_pattern = check_hashtype(optarg);
6736 if (lsa.lsa_pattern == 0) {
6738 "%s %s: bad stripe hash type '%s'\n",
6739 progname, argv[0], optarg);
6744 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6747 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6748 if (strcmp(argv[optind - 1], "--index") == 0)
6750 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6753 lsa.lsa_nr_tgts = parse_targets(mdts,
6754 sizeof(mdts) / sizeof(__u32),
6755 lsa.lsa_nr_tgts, optarg, NULL);
6756 if (lsa.lsa_nr_tgts < 0) {
6758 "%s %s: invalid MDT target(s) '%s'\n",
6759 progname, argv[0], optarg);
6763 lsa.lsa_tgts = mdts;
6764 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6765 lsa.lsa_stripe_off = mdts[0];
6767 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6770 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6779 if (!default_stripe) {
6781 "%s %s: '--max-inherit' must be specified with '-D'\n",
6786 max_inherit = strtol(optarg, &end, 10);
6787 if (errno != 0 || *end != '\0' || max_inherit < -2) {
6789 "%s %s: invalid max-inherit '%s'\n",
6790 progname, argv[0], optarg);
6793 if (max_inherit == 0) {
6794 max_inherit = LMV_INHERIT_NONE;
6795 } else if (max_inherit == -1) {
6796 max_inherit = LMV_INHERIT_UNLIMITED;
6797 } else if (max_inherit > LMV_INHERIT_MAX) {
6799 "%s %s: max-inherit %d exceeds maximum %u\n",
6800 progname, argv[0], max_inherit,
6805 case LFS_INHERIT_RR_OPT:
6806 if (!default_stripe) {
6808 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6813 max_inherit_rr = strtol(optarg, &end, 10);
6814 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6816 "%s %s: invalid max-inherit-rr '%s'\n",
6817 progname, argv[0], optarg);
6820 if (max_inherit_rr == 0) {
6821 max_inherit_rr = LMV_INHERIT_RR_NONE;
6822 } else if (max_inherit_rr == -1) {
6823 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6824 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6826 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6827 progname, argv[0], max_inherit_rr,
6828 LMV_INHERIT_RR_MAX);
6833 fprintf(stderr, "%s: unrecognized option '%s'\n",
6834 progname, argv[optind - 1]);
6841 if (optind == argc) {
6842 fprintf(stderr, "%s %s: DIR must be specified\n",
6847 if (xattr && !foreign_mode) {
6849 * only print a warning as this is armless and will be
6853 "%s %s: xattr has been specified for non-foreign layout\n",
6855 } else if (foreign_mode && !xattr) {
6857 "%s %s: xattr must be provided in foreign mode\n",
6862 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6863 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6865 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6870 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6871 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6872 /* if no parameters set, create directory on least-used MDTs */
6873 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
6874 lsa.lsa_stripe_count = 1;
6878 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6879 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6881 "%s %s: cannot specify -d with -c or -i options\n",
6887 mode = strtoul(mode_opt, &end, 8);
6890 "%s %s: bad MODE '%s'\n",
6891 progname, argv[0], mode_opt);
6894 previous_mode = umask(0);
6897 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6898 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6899 lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
6901 "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
6902 progname, argv[0], lsa.lsa_stripe_off);
6906 /* foreign LMV/dir case */
6908 if (argc > optind + 1) {
6910 "%s %s: cannot specify multiple foreign dirs\n",
6915 dname = argv[optind];
6916 result = llapi_dir_create_foreign(dname, mode, type, flags,
6920 "%s mkdir: can't create foreign dir '%s': %s\n",
6921 progname, dname, strerror(-result));
6926 * initialize stripe parameters, in case param is converted to specific,
6927 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6929 param = calloc(1, offsetof(typeof(*param),
6930 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6931 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6934 "%s %s: cannot allocate memory for parameters: %s\n",
6935 progname, argv[0], strerror(ENOMEM));
6939 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6940 if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
6941 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6942 lsa.lsa_stripe_count == 0))
6943 lsa.lsa_stripe_count = 1;
6944 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6945 param->lsp_stripe_count = lsa.lsa_stripe_count;
6946 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6947 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
6949 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6950 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6951 param->lsp_stripe_pattern = lsa.lsa_pattern;
6953 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6954 param->lsp_pool = lsa.lsa_pool_name;
6955 param->lsp_is_specific = false;
6956 if (default_stripe) {
6957 if (max_inherit == LAYOUT_INHERIT_UNSET)
6958 max_inherit = LMV_INHERIT_DEFAULT;
6959 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
6960 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
6961 param->lsp_max_inherit = max_inherit;
6962 param->lsp_max_inherit_rr = max_inherit_rr;
6964 if (strcmp(argv[0], "mkdir") == 0)
6965 param->lsp_is_create = true;
6966 if (lsa.lsa_nr_tgts > 1) {
6967 if (lsa.lsa_stripe_count > 0 &&
6968 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6969 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6971 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6972 argv[0], lsa.lsa_stripe_count,
6978 param->lsp_is_specific = true;
6979 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6980 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6983 dname = argv[optind];
6985 if (default_stripe) {
6986 result = llapi_dir_set_default_lmv(dname, param);
6989 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6990 progname, dname, strerror(-result));
6994 result = llapi_dir_create(dname, mode, param);
6997 "%s setdirstripe: cannot create dir '%s': %s\n",
6998 progname, dname, strerror(-result));
6999 } while (!result && (dname = argv[++optind]));
7002 umask(previous_mode);
7009 static int lfs_rmentry(int argc, char **argv)
7016 fprintf(stderr, "error: %s: missing dirname\n",
7022 dname = argv[index];
7024 result = llapi_direntry_remove(dname);
7027 "error: %s: remove dir entry '%s' failed\n",
7031 dname = argv[++index];
7036 static int lfs_unlink_foreign(int argc, char **argv)
7043 fprintf(stderr, "error: %s: missing pathname\n",
7050 while (name != NULL) {
7051 result = llapi_unlink_foreign(name);
7054 "error: %s: unlink foreign entry '%s' failed\n",
7058 name = argv[++index];
7063 static int lfs_mv(int argc, char **argv)
7065 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7066 struct find_param param = {
7073 struct option long_opts[] = {
7074 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7075 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7076 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7079 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7081 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7084 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7088 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7089 if (errno != 0 || *end != '\0' ||
7090 lmu.lum_stripe_offset >= UINT32_MAX) {
7091 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7097 param.fp_verbose = VERBOSE_DETAIL;
7100 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7101 progname, argv[optind - 1]);
7106 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7107 fprintf(stderr, "%s mv: MDT index must be specified\n",
7112 if (optind >= argc) {
7113 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7117 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7119 /* initialize migrate mdt parameters */
7120 param.fp_lmv_md = &lmu;
7121 param.fp_migrate = 1;
7122 rc = llapi_migrate_mdt(argv[optind], ¶m);
7124 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7125 progname, argv[optind], lmu.lum_stripe_offset,
7130 static int lfs_osts(int argc, char **argv)
7132 return lfs_tgts(argc, argv);
7135 static int lfs_mdts(int argc, char **argv)
7137 return lfs_tgts(argc, argv);
7140 static int lfs_df(int argc, char **argv)
7142 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7143 enum mntdf_flags flags = MNTDF_SHOW;
7144 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7145 int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7146 char fsname[PATH_MAX] = "", *pool_name = NULL;
7147 struct option long_opts[] = {
7148 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7149 { .val = 'H', .name = "si", .has_arg = no_argument },
7150 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7151 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7152 { .val = 'p', .name = "pool", .has_arg = required_argument },
7153 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7156 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7159 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7162 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7165 flags |= MNTDF_INODES;
7168 flags |= MNTDF_LAZY;
7174 flags |= MNTDF_VERBOSE;
7177 fprintf(stderr, "%s: unrecognized option '%s'\n",
7178 progname, argv[optind - 1]);
7183 /* Handle case where path is not specified */
7184 if (optind == argc) {
7185 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7186 /* Check if we have a mount point */
7187 if (mntdir[0] == '\0')
7190 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7191 if (rc || path[0] != '\0')
7194 fsname[0] = '\0'; /* avoid matching in next loop */
7195 mntdir[0] = '\0'; /* avoid matching in next loop */
7196 path[0] = '\0'; /* clean for next loop */
7201 /* Loop through all the remaining arguments. These are Lustre FS
7204 for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7207 fsname[0] = '\0'; /* start clean */
7208 mntdir[0] = '\0'; /* start clean */
7209 path[0] = '\0'; /* start clean */
7211 /* path does not exists at all */
7212 if (!realpath(argv[arg_idx], path)) {
7214 fprintf(stderr, "error: invalid path '%s': %s\n",
7215 argv[arg_idx], strerror(-rc));
7216 /* save first seen error */
7223 /* path exists but may not be a Lustre filesystem */
7224 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7225 /* Check if we have a mount point */
7226 if (mntdir[0] == '\0')
7229 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7230 if (rc || path[0] != '\0') {
7233 /* save first seen error */
7241 llapi_printf(LLAPI_MSG_ERROR,
7242 "%s:%s Not a Lustre filesystem\n",
7243 argv[0], argv[arg_idx]);
7244 /* save first seen error */
7253 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7254 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7258 if (opt_fsname == opt_instance) { /* both true or both false */
7259 rc = llapi_getname(mntdir, buf, buflen);
7260 } else if (opt_fsname) {
7262 * llapi_search_mounts() fills @buf with fsname, but that is not
7263 * called if explicit paths are specified on the command-line
7266 rc = llapi_get_fsname(mntdir, buf, buflen);
7267 } else /* if (opt_instance) */ {
7268 rc = llapi_get_instance(mntdir, buf, buflen);
7272 fprintf(stderr, "cannot get instance for '%s': %s\n",
7273 mntdir, strerror(-rc));
7278 printf("%s %s\n", buf, mntdir);
7280 printf("%s\n", buf);
7285 static int lfs_getname(int argc, char **argv)
7287 struct option long_opts[] = {
7288 { .val = 'h', .name = "help", .has_arg = no_argument },
7289 { .val = 'i', .name = "instance", .has_arg = no_argument },
7290 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7292 bool opt_instance = false, opt_fsname = false;
7293 char fsname[PATH_MAX] = "";
7296 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7299 opt_instance = true;
7305 fprintf(stderr, "%s: unrecognized option '%s'\n",
7306 progname, argv[optind - 1]);
7313 if (optind == argc) { /* no paths specified, get all paths. */
7314 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7317 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7318 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7319 opt_instance, opt_fsname, true);
7322 path[0] = fsname[0] = mntdir[0] = '\0';
7324 } else { /* paths specified, only attempt to search these. */
7327 /* if only one path is given, print only requested info */
7328 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7330 for (; optind < argc; optind++) {
7331 rc2 = print_instance(argv[optind], fsname,
7332 sizeof(fsname), opt_instance,
7333 opt_fsname, opt_mntdir);
7343 static int lfs_check(int argc, char **argv)
7345 char mntdir[PATH_MAX] = {'\0'};
7354 fprintf(stderr, "%s check: server type must be specified\n",
7359 obd_types[0] = obd_type1;
7360 obd_types[1] = obd_type2;
7361 obd_types[2] = obd_type3;
7363 if (strcmp(argv[1], "osts") == 0) {
7364 strcpy(obd_types[0], "osc");
7365 } else if (strcmp(argv[1], "mdts") == 0 ||
7366 strcmp(argv[1], "mds") == 0) {
7367 strcpy(obd_types[0], "mdc");
7368 } else if (strcmp(argv[1], "mgts") == 0) {
7369 strcpy(obd_types[0], "mgc");
7370 } else if (strcmp(argv[1], "all") == 0 ||
7371 strcmp(argv[1], "servers") == 0) {
7373 strcpy(obd_types[0], "osc");
7374 strcpy(obd_types[1], "mdc");
7375 strcpy(obd_types[2], "mgc");
7377 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7382 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
7383 if (rc < 0 || mntdir[0] == '\0') {
7385 "%s check: cannot find mounted Lustre filesystem: %s\n",
7386 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7390 rc = llapi_target_check(num_types, obd_types, mntdir);
7392 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7393 progname, argv[1], strerror(-rc));
7398 #ifdef HAVE_SYS_QUOTA_H
7399 #define ADD_OVERFLOW(a, b) \
7400 ((((a) + (b)) < (a)) ? \
7401 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7403 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7404 * returns the value or ULONG_MAX on integer overflow or incorrect format
7406 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7407 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7408 * 3. empty integer value is interpreted as 0
7410 static unsigned long str2sec(const char *timestr)
7412 const char spec[] = "smhdw";
7413 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7414 unsigned long val = 0;
7417 if (strpbrk(timestr, spec) == NULL) {
7419 * no specifiers inside the time string,
7420 * should treat it as an integer value
7422 val = strtoul(timestr, &tail, 10);
7423 return *tail ? ULONG_MAX : val;
7426 /* format string is XXwXXdXXhXXmXXs */
7432 v = strtoul(timestr, &tail, 10);
7433 if (v == ULONG_MAX || *tail == '\0')
7435 * value too large (ULONG_MAX or more)
7436 * or missing specifier
7440 ptr = strchr(spec, *tail);
7442 /* unknown specifier */
7447 /* check if product will overflow the type */
7448 if (!(v < ULONG_MAX / mult[ind]))
7451 ADD_OVERFLOW(val, mult[ind] * v);
7452 if (val == ULONG_MAX)
7464 #define ARG2ULL(nr, str, def_units) \
7466 unsigned long long limit, units = def_units; \
7469 rc = llapi_parse_size(str, &limit, &units, 1); \
7471 fprintf(stderr, "%s: invalid limit '%s'\n", \
7478 static inline int has_times_option(int argc, char **argv)
7482 for (i = 1; i < argc; i++)
7483 if (!strcmp(argv[i], "-t"))
7489 static inline int lfs_verify_poolarg(char *pool)
7491 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7493 "Pool name '%.*s' is longer than %d\n",
7494 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7500 /* special grace time, only notify the user when its quota is over soft limit
7501 * but doesn't block new writes until the hard limit is reached.
7503 #define NOTIFY_GRACE "notify"
7504 #define NOTIFY_GRACE_TIME LQUOTA_GRACE_MASK
7507 static inline __u64 lustre_stoqb(size_t space)
7509 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7512 #define lustre_stoqb toqb
7515 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7518 char *mnt, *obd_type = (char *)qctl->obd_type;
7519 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7520 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7521 struct option long_opts[] = {
7522 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7523 { .val = 'g', .name = "group", .has_arg = no_argument },
7524 { .val = 'h', .name = "help", .has_arg = no_argument },
7525 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7526 { .val = 'p', .name = "projid", .has_arg = no_argument },
7527 { .val = 't', .name = "times", .has_arg = no_argument },
7528 { .val = 'u', .name = "user", .has_arg = no_argument },
7529 { .val = LFS_POOL_OPT,
7530 .name = "pool", .has_arg = required_argument },
7534 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7535 qctl->qc_type = ALLQUOTA;
7537 while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7538 long_opts, NULL)) != -1) {
7549 if (qctl->qc_type != ALLQUOTA) {
7551 "%s: -u/g/p cannot be used more than once\n",
7555 qctl->qc_type = qtype;
7558 if (strncmp(optarg, NOTIFY_GRACE,
7559 strlen(NOTIFY_GRACE)) == 0) {
7560 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7562 dqi->dqi_bgrace = str2sec(optarg);
7563 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7565 "%s: bad block-grace: %s\n",
7570 dqb->dqb_valid |= QIF_BTIME;
7573 if (strncmp(optarg, NOTIFY_GRACE,
7574 strlen(NOTIFY_GRACE)) == 0) {
7575 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7577 dqi->dqi_igrace = str2sec(optarg);
7578 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7580 "%s: bad inode-grace: %s\n",
7585 dqb->dqb_valid |= QIF_ITIME;
7587 case 't': /* Yes, of course! */
7590 if (lfs_verify_poolarg(optarg))
7592 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7593 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7595 /* getopt prints error message for us when opterr != 0 */
7597 fprintf(stderr, "%s: unrecognized option '%s'\n",
7598 progname, argv[optind - 1]);
7605 if (qctl->qc_type == ALLQUOTA) {
7606 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7611 if (optind != argc - 1) {
7612 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7613 progname, argv[optind + 1]);
7618 rc = llapi_quotactl(mnt, qctl);
7621 fprintf(stderr, "%s %s ", obd_type,
7622 obd_uuid2str(&qctl->obd_uuid));
7623 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7630 #define BSLIMIT (1 << 0)
7631 #define BHLIMIT (1 << 1)
7632 #define ISLIMIT (1 << 2)
7633 #define IHLIMIT (1 << 3)
7635 int lfs_setquota(int argc, char **argv)
7638 struct if_quotactl *qctl;
7639 char *mnt, *obd_type;
7640 struct obd_dqblk *dqb;
7641 struct option long_opts[] = {
7642 { .val = 'b', .name = "block-softlimit",
7643 .has_arg = required_argument },
7644 { .val = 'B', .name = "block-hardlimit",
7645 .has_arg = required_argument },
7646 { .val = 'd', .name = "default", .has_arg = no_argument },
7647 { .val = 'g', .name = "group", .has_arg = required_argument },
7648 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7649 { .val = 'h', .name = "help", .has_arg = no_argument },
7650 { .val = 'i', .name = "inode-softlimit",
7651 .has_arg = required_argument },
7652 { .val = 'I', .name = "inode-hardlimit",
7653 .has_arg = required_argument },
7654 { .val = 'p', .name = "projid", .has_arg = required_argument },
7655 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7656 { .val = 'u', .name = "user", .has_arg = required_argument },
7657 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7658 { .val = LFS_POOL_OPT,
7659 .name = "pool", .has_arg = required_argument },
7661 unsigned int limit_mask = 0;
7662 bool use_default = false;
7663 int qtype, qctl_len;
7665 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7666 qctl = malloc(qctl_len);
7670 memset(qctl, 0, qctl_len);
7671 obd_type = (char *)qctl->obd_type;
7672 dqb = &qctl->qc_dqblk;
7674 if (has_times_option(argc, argv)) {
7675 rc = lfs_setquota_times(argc, argv, qctl);
7679 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7680 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7681 * so it can be used as a marker that qc_type
7682 * isn't reinitialized from command line
7685 while ((c = getopt_long(argc, argv, "b:B:dg:Ghi:I:p:Pu:U",
7686 long_opts, NULL)) != -1) {
7689 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7692 goto quota_type_def;
7695 rc = name2uid(&qctl->qc_id, optarg);
7698 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7701 goto quota_type_def;
7704 rc = name2gid(&qctl->qc_id, optarg);
7707 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7710 goto quota_type_def;
7713 rc = name2projid(&qctl->qc_id, optarg);
7716 if (str2quotaid(&qctl->qc_id, optarg)) {
7718 "%s setquota: invalid id '%s'\n",
7725 if (qctl->qc_id == 0) {
7727 "%s setquota: can't set quota for root usr/group/project.\n",
7734 if (qctl->qc_type != ALLQUOTA) {
7736 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7741 qctl->qc_type = qtype;
7744 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7748 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7749 dqb->dqb_bsoftlimit >>= 10;
7750 limit_mask |= BSLIMIT;
7751 if (dqb->dqb_bsoftlimit &&
7752 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7754 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7756 (unsigned long long)dqb->dqb_bsoftlimit,
7760 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7761 dqb->dqb_bhardlimit >>= 10;
7762 limit_mask |= BHLIMIT;
7763 if (dqb->dqb_bhardlimit &&
7764 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7766 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7767 "See '%s help setquota' or Lustre manual for details\n",
7769 (unsigned long long)dqb->dqb_bhardlimit,
7773 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7774 limit_mask |= ISLIMIT;
7775 if (dqb->dqb_isoftlimit &&
7776 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7778 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7780 (unsigned long long)dqb->dqb_isoftlimit,
7784 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7785 limit_mask |= IHLIMIT;
7786 if (dqb->dqb_ihardlimit &&
7787 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7789 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7791 (unsigned long long)dqb->dqb_ihardlimit,
7795 if (lfs_verify_poolarg(optarg)) {
7799 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7800 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
7801 LUSTRE_Q_SETDEFAULT_POOL :
7802 LUSTRE_Q_SETQUOTAPOOL;
7806 "%s setquota: unrecognized option '%s'\n",
7807 progname, argv[optind - 1]);
7815 if (qctl->qc_type == ALLQUOTA) {
7817 "%s setquota: either -u or -g must be specified\n",
7823 if (!use_default && limit_mask == 0) {
7825 "%s setquota: at least one limit must be specified\n",
7831 if (use_default && limit_mask != 0) {
7833 "%s setquota: limits should not be specified when using default quota\n",
7839 if (use_default && qctl->qc_id == 0) {
7841 "%s setquota: can not set default quota for root user/group/project\n",
7847 if (optind != argc - 1) {
7849 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7850 progname, argv[optind]);
7858 dqb->dqb_bhardlimit = 0;
7859 dqb->dqb_bsoftlimit = 0;
7860 dqb->dqb_ihardlimit = 0;
7861 dqb->dqb_isoftlimit = 0;
7864 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7865 /* do not set inode limits for Pool Quotas */
7866 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
7867 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
7868 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7869 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7870 /* sigh, we can't just set blimits/ilimits */
7871 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7872 .qc_type = qctl->qc_type,
7873 .qc_id = qctl->qc_id};
7875 rc = llapi_quotactl(mnt, &tmp_qctl);
7879 if (!(limit_mask & BHLIMIT))
7880 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7881 if (!(limit_mask & BSLIMIT))
7882 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7883 if (!(limit_mask & IHLIMIT))
7884 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7885 if (!(limit_mask & ISLIMIT))
7886 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7888 /* Keep grace times if we have got no softlimit arguments */
7889 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7890 dqb->dqb_valid |= QIF_BTIME;
7891 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7894 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7895 dqb->dqb_valid |= QIF_ITIME;
7896 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7900 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7901 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7903 rc = llapi_quotactl(mnt, qctl);
7907 "%s setquota: cannot quotactl '%s' '%s': %s\n",
7909 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7912 "%s setquota: quotactl failed: %s\n",
7913 progname, strerror(-rc));
7917 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7923 /* Converts seconds value into format string
7924 * result is returned in buf
7926 * 1. result is in descenting order: 1w2d3h4m5s
7927 * 2. zero fields are not filled (except for p. 3): 5d1s
7928 * 3. zero seconds value is presented as "0s"
7930 static char *__sec2str(time_t seconds, char *buf)
7932 const char spec[] = "smhdw";
7933 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7938 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7939 c = seconds / mult[i];
7941 if (c > 0 || (i == 0 && buf == tail))
7942 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7951 static void sec2str(time_t seconds, char *buf, int rc)
7958 tail = __sec2str(seconds, tail);
7960 if (rc && tail - buf < 39) {
7966 static void diff2str(time_t seconds, char *buf, time_t now)
7971 if (seconds <= now) {
7972 strcpy(buf, "none");
7975 __sec2str(seconds - now, buf);
7978 static void print_quota_title(char *name, struct if_quotactl *qctl,
7979 bool human_readable, bool show_default)
7982 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7983 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7984 "Filesystem", "bquota", "blimit", "bgrace",
7985 "iquota", "ilimit", "igrace");
7987 printf("Disk quotas for %s %s (%cid %u):\n",
7988 qtype_name(qctl->qc_type), name,
7989 *qtype_name(qctl->qc_type), qctl->qc_id);
7990 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7991 "Filesystem", human_readable ? "used" : "kbytes",
7992 "quota", "limit", "grace",
7993 "files", "quota", "limit", "grace");
7997 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8000 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8003 snprintf(buf, buflen, "%5.4gP",
8004 (double)num / ((__u64)1 << 40));
8006 snprintf(buf, buflen, "%5.4gT",
8007 (double)num / (1 << 30));
8009 snprintf(buf, buflen, "%5.4gG",
8010 (double)num / (1 << 20));
8012 snprintf(buf, buflen, "%5.4gM",
8013 (double)num / (1 << 10));
8015 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8019 #ifdef HAVE_NATIVE_CLIENT
8020 /* In the current Lustre implementation, the grace time is either the time
8021 * or the timestamp to be used after some quota ID exceeds the soft limt,
8022 * 48 bits should be enough, its high 16 bits can be used as quota flags.
8024 #define LQUOTA_GRACE_BITS 48
8025 #define LQUOTA_GRACE_MASK ((1ULL << LQUOTA_GRACE_BITS) - 1)
8026 #define LQUOTA_GRACE_MAX LQUOTA_GRACE_MASK
8027 #define LQUOTA_GRACE(t) (t & LQUOTA_GRACE_MASK)
8028 #define LQUOTA_FLAG(t) (t >> LQUOTA_GRACE_BITS)
8029 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8032 #define STRBUF_LEN 24
8033 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8034 int rc, bool h, bool show_default)
8040 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8041 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8042 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8043 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8044 int bover = 0, iover = 0;
8045 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8046 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8048 char strbuf[STRBUF_LEN];
8050 if (dqb->dqb_bhardlimit &&
8051 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8053 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8054 if (dqb->dqb_btime > now)
8060 if (dqb->dqb_ihardlimit &&
8061 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8063 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8064 if (dqb->dqb_itime > now)
8070 if (strlen(mnt) > 15)
8071 printf("%s\n%15s", mnt, "");
8073 printf("%15s", mnt);
8076 diff2str(dqb->dqb_btime, timebuf, now);
8077 else if (show_default)
8078 snprintf(timebuf, sizeof(timebuf), "%llu",
8079 (unsigned long long)dqb->dqb_btime);
8081 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8082 strbuf, sizeof(strbuf), h);
8083 if (rc == -EREMOTEIO)
8084 sprintf(numbuf[0], "%s*", strbuf);
8086 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8087 "%s" : "[%s]", strbuf);
8089 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8090 if (type == QC_GENERAL)
8091 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8092 "%s" : "[%s]", strbuf);
8094 sprintf(numbuf[1], "%s", "-");
8096 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8097 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8098 "%s" : "[%s]", strbuf);
8101 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8103 printf(" %7s%c %6s %7s %7s",
8104 numbuf[0], bover ? '*' : ' ', numbuf[1],
8105 numbuf[2], bover > 1 ? timebuf : "-");
8108 diff2str(dqb->dqb_itime, timebuf, now);
8109 else if (show_default)
8110 snprintf(timebuf, sizeof(timebuf), "%llu",
8111 (unsigned long long)dqb->dqb_itime);
8113 snprintf(numbuf[0], sizeof(numbuf),
8114 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8115 (uintmax_t)dqb->dqb_curinodes);
8117 if (type == QC_GENERAL)
8118 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8120 (uintmax_t)dqb->dqb_isoftlimit);
8122 sprintf(numbuf[1], "%s", "-");
8124 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8125 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8128 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8129 else if (type != QC_OSTIDX)
8130 printf(" %7s%c %6s %7s %7s",
8131 numbuf[0], iover ? '*' : ' ', numbuf[1],
8132 numbuf[2], iover > 1 ? timebuf : "-");
8134 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8136 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8137 qctl->qc_cmd == Q_GETOINFO) {
8141 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8142 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8144 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8145 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8146 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8148 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8150 printf("Block grace time: %s; Inode grace time: %s\n",
8151 bgtimebuf, igtimebuf);
8155 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8159 /* format is "lustre-OST0001" */
8160 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8162 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8167 *idx = strtoul(dash, &endp, 16);
8168 if (*idx > 0xffff) {
8169 fprintf(stderr, "wrong index %s\n", tgtname);
8176 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8177 bool h, __u64 *total)
8179 int rc = 0, rc1 = 0, count = 0, i = 0;
8180 char **list = NULL, *buffer = NULL;
8181 __u32 valid = qctl->qc_valid;
8183 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8186 /* Is it correct for the case OST0000, OST0002, OST0003 -
8187 * we will ask OST0001 that is absent and won't ask OST0003? */
8188 rc = llapi_get_obd_count(mnt, &count, is_mdt);
8190 fprintf(stderr, "can not get %s count: %s\n",
8191 is_mdt ? "mdt" : "ost", strerror(-rc));
8195 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8196 char fname[PATH_MAX];
8197 char fsname[LUSTRE_MAXFSNAME + 1];
8198 int bufsize = sizeof(struct obd_uuid) * count;
8200 rc = llapi_search_fsname(mnt, fsname);
8202 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8206 buffer = malloc(bufsize + sizeof(*list) * count);
8209 list = (char **)(buffer + bufsize);
8210 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8211 count = llapi_get_poolmembers(fname, list, count,
8217 for (i = 0; i < count; i++) {
8218 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8221 if (tgt_name2index(list[i], &index))
8223 qctl->qc_idx = index;
8228 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8229 rc = llapi_quotactl(mnt, qctl);
8231 /* It is remote client case. */
8232 if (rc == -EOPNOTSUPP) {
8239 fprintf(stderr, "quotactl %s%d failed.\n",
8240 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8244 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8245 qctl->qc_valid, 0, h, false);
8246 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8247 qctl->qc_dqblk.dqb_bhardlimit;
8252 qctl->qc_valid = valid;
8256 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8257 int verbose, int quiet, bool human_readable,
8260 int rc1 = 0, rc2 = 0, rc3 = 0;
8261 char *obd_type = (char *)qctl->obd_type;
8262 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8263 __u64 total_ialloc = 0, total_balloc = 0;
8264 bool use_default_for_blk = false;
8265 bool use_default_for_file = false;
8268 rc1 = llapi_quotactl(mnt, qctl);
8272 fprintf(stderr, "%s quotas are not enabled.\n",
8273 qtype_name(qctl->qc_type));
8276 fprintf(stderr, "Permission denied.\n");
8279 /* We already got error message. */
8282 fprintf(stderr, "Unexpected quotactl error: %s\n",
8287 if (!show_default && qctl->qc_id == 0) {
8288 qctl->qc_dqblk.dqb_bhardlimit = 0;
8289 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8290 qctl->qc_dqblk.dqb_ihardlimit = 0;
8291 qctl->qc_dqblk.dqb_isoftlimit = 0;
8292 qctl->qc_dqblk.dqb_btime = 0;
8293 qctl->qc_dqblk.dqb_itime = 0;
8294 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8297 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8298 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8299 use_default_for_blk = true;
8300 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8303 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8304 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8305 use_default_for_file = true;
8306 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8309 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8310 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8311 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8312 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8313 print_quota_title(name, qctl, human_readable, show_default);
8315 if (rc1 && *obd_type)
8316 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8318 if (qctl->qc_valid != QC_GENERAL)
8321 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8322 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8323 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8324 (QIF_LIMITS|QIF_USAGE));
8326 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8328 if (!show_default && verbose &&
8329 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8330 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8331 char strbuf[STRBUF_LEN];
8333 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8335 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8337 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8339 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8340 (uintmax_t)total_ialloc, strbuf);
8343 if (use_default_for_blk)
8344 printf("%cid %u is using default block quota setting\n",
8345 *qtype_name(qctl->qc_type), qctl->qc_id);
8347 if (use_default_for_file)
8348 printf("%cid %u is using default file quota setting\n",
8349 *qtype_name(qctl->qc_type), qctl->qc_id);
8351 if (rc1 || rc2 || rc3 || inacc)
8352 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8366 static int lfs_project(int argc, char **argv)
8368 int ret = 0, err = 0, c, i;
8369 struct project_handle_control phc = { 0 };
8370 enum lfs_project_ops_t op;
8373 phc.assign_projid = false;
8374 /* default action */
8375 op = LFS_PROJECT_LIST;
8377 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8380 if (op != LFS_PROJECT_LIST) {
8382 "%s: cannot specify '-c' '-C' '-s' together\n",
8387 op = LFS_PROJECT_CHECK;
8390 if (op != LFS_PROJECT_LIST) {
8392 "%s: cannot specify '-c' '-C' '-s' together\n",
8397 op = LFS_PROJECT_CLEAR;
8400 if (op != LFS_PROJECT_LIST) {
8402 "%s: cannot specify '-c' '-C' '-s' together\n",
8407 phc.set_inherit = true;
8408 op = LFS_PROJECT_SET;
8414 phc.keep_projid = true;
8417 phc.recursive = true;
8420 if (str2quotaid(&phc.projid, optarg)) {
8422 "Invalid project ID: %s\n",
8427 phc.assign_projid = true;
8431 phc.newline = false;
8434 fprintf(stderr, "%s: invalid option '%c'\n",
8440 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8441 op = LFS_PROJECT_SET;
8442 phc.set_projid = true;
8443 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8444 phc.set_projid = true;
8448 case LFS_PROJECT_CHECK:
8449 if (phc.keep_projid) {
8451 "%s: '-k' is useless together with '-c'\n",
8456 case LFS_PROJECT_CLEAR:
8459 "%s: '-0' is useless together with '-C'\n",
8463 if (phc.assign_projid) {
8465 "%s: '-p' is useless together with '-C'\n",
8470 case LFS_PROJECT_SET:
8473 "%s: '-0' is useless together with '-s'\n",
8477 if (phc.keep_projid) {
8479 "%s: '-k' is useless together with '-s'\n",
8487 "%s: '-0' is useless for list operations\n",
8497 fprintf(stderr, "%s: missing file or directory target(s)\n",
8502 for (i = 0; i < argc; i++) {
8504 case LFS_PROJECT_CHECK:
8505 err = lfs_project_check(argv[i], &phc);
8507 case LFS_PROJECT_LIST:
8508 err = lfs_project_list(argv[i], &phc);
8510 case LFS_PROJECT_CLEAR:
8511 err = lfs_project_clear(argv[i], &phc);
8513 case LFS_PROJECT_SET:
8514 err = lfs_project_set(argv[i], &phc);
8526 static int lfs_quota(int argc, char **argv)
8529 char *mnt, *name = NULL;
8530 struct if_quotactl *qctl;
8532 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8533 __u32 valid = QC_GENERAL, idx = 0;
8534 bool human_readable = false;
8535 bool show_default = false;
8537 bool show_pools = false;
8538 struct option long_opts[] = {
8539 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
8541 char **poollist = NULL;
8545 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8549 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8550 qctl->qc_type = ALLQUOTA;
8551 obd_uuid = (char *)qctl->obd_uuid.uuid;
8553 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8554 long_opts, NULL)) != -1) {
8557 show_default = true;
8562 show_default = true;
8567 show_default = true;
8571 if (qctl->qc_type != ALLQUOTA) {
8573 "%s quota: only one of -u, -g, or -p may be specified\n",
8578 qctl->qc_type = qtype;
8581 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8584 valid = qctl->qc_valid = QC_UUID;
8585 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8588 valid = qctl->qc_valid = QC_MDTIDX;
8589 idx = qctl->qc_idx = atoi(optarg);
8590 if (idx == 0 && *optarg != '0') {
8592 "%s quota: invalid MDT index '%s'\n",
8599 valid = qctl->qc_valid = QC_OSTIDX;
8600 idx = qctl->qc_idx = atoi(optarg);
8601 if (idx == 0 && *optarg != '0') {
8603 "%s quota: invalid OST index '%s'\n",
8616 human_readable = true;
8619 if ((!optarg) && (argv[optind] != NULL) &&
8620 (argv[optind][0] != '-') &&
8621 (argv[optind][0] != '/')) {
8622 optarg = argv[optind++];
8623 if (lfs_verify_poolarg(optarg)) {
8627 strncpy(qctl->qc_poolname, optarg,
8629 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
8630 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
8632 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8636 /* optarg is NULL */
8638 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8641 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8642 progname, argv[optind - 1]);
8648 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8649 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8650 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8651 qctl->qc_type == ALLQUOTA &&
8652 optind == argc - 1 && !show_default) {
8655 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8656 qctl->qc_type = qtype;
8657 qctl->qc_valid = valid;
8658 if (qtype == USRQUOTA) {
8659 qctl->qc_id = geteuid();
8660 rc = uid2name(&name, qctl->qc_id);
8662 qctl->qc_id = getegid();
8663 rc = gid2name(&name, qctl->qc_id);
8664 memset(&qctl->qc_dqblk, 0,
8665 sizeof(qctl->qc_dqblk));
8670 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8671 human_readable, show_default);
8676 /* lfs quota -u username /path/to/lustre/mount */
8677 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8678 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8679 /* options should be followed by u/g-name and mntpoint */
8680 if ((!show_default && optind + 2 != argc) ||
8681 (show_default && optind + 1 != argc) ||
8682 qctl->qc_type == ALLQUOTA) {
8684 "%s quota: name and mount point must be specified\n",
8690 if (!show_default) {
8691 name = argv[optind++];
8692 switch (qctl->qc_type) {
8694 rc = name2uid(&qctl->qc_id, name);
8697 rc = name2gid(&qctl->qc_id, name);
8700 rc = name2projid(&qctl->qc_id, name);
8707 qctl->qc_valid = QC_GENERAL;
8708 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8709 LUSTRE_Q_GETDEFAULT_POOL :
8710 LUSTRE_Q_GETDEFAULT;
8715 if (str2quotaid(&qctl->qc_id, name)) {
8716 fprintf(stderr, "%s quota: invalid id '%s'\n",
8722 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8723 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8734 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
8738 for (i = 0; i < poolcount; i++) {
8739 p = memchr(poollist[i], '.', MAXNAMLEN);
8741 fprintf(stderr, "bad string format %.*s\n",
8742 MAXNAMLEN, poollist[i]);
8747 printf("Quotas for pool: %s\n", p);
8748 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
8749 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8750 human_readable, show_default);
8757 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8758 human_readable, show_default);
8764 #endif /* HAVE_SYS_QUOTA_H! */
8766 static int flushctx_ioctl(char *mp)
8770 fd = open(mp, O_RDONLY);
8772 fprintf(stderr, "flushctx: error open %s: %s\n",
8773 mp, strerror(errno));
8777 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8779 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8780 mp, strerror(errno));
8786 static int lfs_flushctx(int argc, char **argv)
8788 int kdestroy = 0, reap = 0, c;
8789 char mntdir[PATH_MAX] = {'\0'};
8793 while ((c = getopt(argc, argv, "kr")) != -1) {
8803 "error: %s: option '-%c' unrecognized\n",
8810 rc = system("kdestroy > /dev/null");
8812 rc = WEXITSTATUS(rc);
8814 "error destroying tickets: %d, continuing\n",
8819 if (optind >= argc) {
8820 /* flush for all mounted lustre fs. */
8821 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8822 /* Check if we have a mount point */
8823 if (mntdir[0] == '\0')
8826 if (flushctx_ioctl(mntdir))
8829 mntdir[0] = '\0'; /* avoid matching in next loop */
8832 /* flush fs as specified */
8833 while (optind < argc) {
8834 if (flushctx_ioctl(argv[optind++]))
8840 rc = system("keyctl reap > /dev/null");
8842 rc = WEXITSTATUS(rc);
8843 fprintf(stderr, "error reaping keyring: %d\n", rc);
8850 static int lfs_changelog(int argc, char **argv)
8852 void *changelog_priv;
8853 struct changelog_rec *rec;
8854 long long startrec = 0, endrec = 0;
8856 struct option long_opts[] = {
8857 { .val = 'f', .name = "follow", .has_arg = no_argument },
8859 char short_opts[] = "f";
8862 while ((rc = getopt_long(argc, argv, short_opts,
8863 long_opts, NULL)) != -1) {
8870 "%s changelog: unrecognized option '%s'\n",
8871 progname, argv[optind - 1]);
8875 if (optind >= argc) {
8876 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8881 mdd = argv[optind++];
8882 if (argc > optind) {
8884 startrec = strtoll(argv[optind++], NULL, 10);
8885 if (errno != 0 || startrec < 0) {
8887 "%s changelog: bad startrec\n",
8893 if (argc > optind) {
8895 endrec = strtoll(argv[optind++], NULL, 10);
8896 if (errno != 0 || endrec < 0) {
8898 "%s changelog: bad endrec\n",
8904 rc = llapi_changelog_start(&changelog_priv,
8905 CHANGELOG_FLAG_BLOCK |
8906 CHANGELOG_FLAG_JOBID |
8907 CHANGELOG_FLAG_EXTRA_FLAGS |
8908 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8911 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8912 progname, strerror(errno = -rc));
8916 rc = llapi_changelog_set_xflags(changelog_priv,
8917 CHANGELOG_EXTRA_FLAG_UIDGID |
8918 CHANGELOG_EXTRA_FLAG_NID |
8919 CHANGELOG_EXTRA_FLAG_OMODE |
8920 CHANGELOG_EXTRA_FLAG_XATTR);
8923 "%s changelog: cannot set xflags for changelog: %s\n",
8924 progname, strerror(errno = -rc));
8928 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8932 if (endrec && rec->cr_index > endrec) {
8933 llapi_changelog_free(&rec);
8936 if (rec->cr_index < startrec) {
8937 llapi_changelog_free(&rec);
8941 secs = rec->cr_time >> 30;
8942 gmtime_r(&secs, &ts);
8943 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8944 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8945 changelog_type2str(rec->cr_type),
8946 ts.tm_hour, ts.tm_min, ts.tm_sec,
8947 (int)(rec->cr_time & ((1 << 30) - 1)),
8948 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8949 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8951 if (rec->cr_flags & CLF_JOBID) {
8952 struct changelog_ext_jobid *jid =
8953 changelog_rec_jobid(rec);
8955 if (jid->cr_jobid[0] != '\0')
8956 printf(" j=%s", jid->cr_jobid);
8959 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8960 struct changelog_ext_extra_flags *ef =
8961 changelog_rec_extra_flags(rec);
8963 printf(" ef=0x%llx",
8964 (unsigned long long)ef->cr_extra_flags);
8966 if (ef->cr_extra_flags & CLFE_UIDGID) {
8967 struct changelog_ext_uidgid *uidgid =
8968 changelog_rec_uidgid(rec);
8970 printf(" u=%llu:%llu",
8971 (unsigned long long)uidgid->cr_uid,
8972 (unsigned long long)uidgid->cr_gid);
8974 if (ef->cr_extra_flags & CLFE_NID) {
8975 struct changelog_ext_nid *nid =
8976 changelog_rec_nid(rec);
8979 libcfs_nid2str(nid->cr_nid));
8982 if (ef->cr_extra_flags & CLFE_OPEN) {
8983 struct changelog_ext_openmode *omd =
8984 changelog_rec_openmode(rec);
8985 char mode[] = "---";
8987 /* exec mode must be exclusive */
8988 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8991 if (omd->cr_openflags & MDS_FMODE_READ)
8993 if (omd->cr_openflags &
9000 if (strcmp(mode, "---") != 0)
9001 printf(" m=%s", mode);
9004 if (ef->cr_extra_flags & CLFE_XATTR) {
9005 struct changelog_ext_xattr *xattr =
9006 changelog_rec_xattr(rec);
9008 if (xattr->cr_xattr[0] != '\0')
9009 printf(" x=%s", xattr->cr_xattr);
9013 if (!fid_is_zero(&rec->cr_pfid))
9014 printf(" p="DFID, PFID(&rec->cr_pfid));
9015 if (rec->cr_namelen)
9016 printf(" %.*s", rec->cr_namelen,
9017 changelog_rec_name(rec));
9019 if (rec->cr_flags & CLF_RENAME) {
9020 struct changelog_ext_rename *rnm =
9021 changelog_rec_rename(rec);
9023 if (!fid_is_zero(&rnm->cr_sfid))
9024 printf(" s="DFID" sp="DFID" %.*s",
9025 PFID(&rnm->cr_sfid),
9026 PFID(&rnm->cr_spfid),
9027 (int)changelog_rec_snamelen(rec),
9028 changelog_rec_sname(rec));
9032 llapi_changelog_free(&rec);
9035 llapi_changelog_fini(&changelog_priv);
9038 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9039 progname, strerror(errno = -rc));
9041 return (rc == 1 ? 0 : rc);
9044 static int lfs_changelog_clear(int argc, char **argv)
9053 endrec = strtoll(argv[3], NULL, 10);
9054 if (errno != 0 || endrec < 0) {
9056 "%s: bad endrec '%s'\n",
9061 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9064 fprintf(stderr, "%s: record out of range: %llu\n",
9066 else if (rc == -ENOENT)
9067 fprintf(stderr, "%s: no changelog user: %s\n",
9070 fprintf(stderr, "%s error: %s\n", argv[0],
9079 static void rstripc(char *str, int c)
9081 char *end = str + strlen(str);
9083 for (; str < end && end[-1] == c; --end)
9087 static int lfs_fid2path(int argc, char **argv)
9089 struct option long_opts[] = {
9090 { .val = 'c', .name = "cur", .has_arg = no_argument },
9091 { .val = 'c', .name = "current", .has_arg = no_argument },
9092 { .val = 'c', .name = "print-link", .has_arg = no_argument },
9093 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
9094 { .val = 'l', .name = "link", .has_arg = required_argument },
9096 char short_opts[] = "cfl:pr:";
9097 bool print_link = false;
9098 bool print_fid = false;
9100 char mnt_dir[PATH_MAX] = "";
9102 char *path_or_fsname;
9103 long long recno = -1;
9105 char *endptr = NULL;
9110 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9120 linkno = strtol(optarg, &endptr, 10);
9121 if (errno != 0 || *endptr != '\0' || linkno < 0) {
9123 "%s fid2path: invalid linkno '%s'\n",
9129 /* recno is something to do with changelogs
9130 * that was never implemented. We just pass it
9131 * through for the MDT to ignore.
9134 recno = strtoll(optarg, &endptr, 10);
9135 if (errno != 0 || *endptr != '\0' || recno < 0) {
9137 "%s fid2path: invalid recno '%s'\n",
9144 "%s fid2path: unrecognized option '%s'\n",
9145 progname, argv[optind - 1]);
9150 if (argc - optind < 2) {
9152 "Usage: %s fid2path FSNAME|ROOT FID...\n",
9157 path_or_fsname = argv[optind];
9159 if (*path_or_fsname == '/') {
9160 print_mnt_dir = true;
9161 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9163 print_mnt_dir = false;
9164 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9169 "%s fid2path: cannot resolve mount point for '%s': %s\n",
9170 progname, path_or_fsname, strerror(-rc));
9174 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9177 "%s fid2path: cannot open mount point for '%s': %s\n",
9178 progname, path_or_fsname, strerror(-rc));
9182 /* Strip trailing slashes from mnt_dir. */
9183 rstripc(mnt_dir + 1, '/');
9185 for (i = optind + 1; i < argc; i++) {
9186 const char *fid_str = argv[i];
9190 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9193 "%s fid2path: invalid FID '%s'\n",
9201 int linktmp = (linkno >= 0) ? linkno : 0;
9203 int oldtmp = linktmp;
9204 long long rectmp = recno;
9205 char path_buf[PATH_MAX];
9207 rc2 = llapi_fid2path_at(mnt_fd, &fid,
9208 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9211 "%s fid2path: cannot find %s %s: %s\n",
9212 progname, path_or_fsname, fid_str,
9220 printf("%s ", fid_str);
9223 printf("%d ", linktmp);
9225 /* You may think this looks wrong or weird (and it is!)
9226 * but we are actually trying to preserve the old quirky
9227 * behaviors (enforced by our old quirky tests!) that
9228 * make lfs so much fun to work on:
9230 * lustre 0x200000007:0x1:0x0 => "/"
9231 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9233 * Note that llapi_fid2path() returns "" for the root
9237 print_mnt_dir ? mnt_dir : "",
9238 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9242 /* specified linkno */
9245 if (oldtmp == linktmp)
9257 static int lfs_path2fid(int argc, char **argv)
9259 struct option long_opts[] = {
9260 { .val = 'p', .name = "parents", .has_arg = no_argument },
9263 const char short_opts[] = "p";
9264 const char *sep = "";
9267 bool show_parents = false;
9269 while ((rc = getopt_long(argc, argv, short_opts,
9270 long_opts, NULL)) != -1) {
9273 show_parents = true;
9277 "%s path2fid: unrecognized option '%s'\n",
9278 progname, argv[optind - 1]);
9283 if (optind > argc - 1) {
9284 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9287 } else if (optind < argc - 1) {
9292 for (path = argv + optind; *path != NULL; path++) {
9295 if (!show_parents) {
9296 err = llapi_path2fid(*path, &fid);
9298 printf("%s%s"DFID"\n",
9299 *sep != '\0' ? *path : "", sep,
9302 char name[NAME_MAX + 1];
9303 unsigned int linkno = 0;
9305 while ((err = llapi_path2parent(*path, linkno, &fid,
9306 name, sizeof(name))) == 0) {
9307 if (*sep != '\0' && linkno == 0)
9308 printf("%s%s", *path, sep);
9310 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9315 /* err == -ENODATA is end-of-loop */
9316 if (linkno > 0 && err == -ENODATA) {
9324 "%s path2fid: cannot get %sfid for '%s': %s\n",
9325 progname, show_parents ? "parent " : "", *path,
9337 #define MAX_ERRNO 4095
9338 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9340 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9344 rc = llapi_rmfid(device, fa);
9346 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9347 progname, strerror(-rc));
9351 for (k = 0; k < fa->fa_nr; k++) {
9352 rc2 = (__s32)fa->fa_fids[k].f_ver;
9353 if (!IS_ERR_VALUE(rc2))
9359 fa->fa_fids[k].f_ver = 0;
9360 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9361 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9367 static int lfs_rmfid(int argc, char **argv)
9369 char *fidstr, *device;
9370 int rc = 0, rc2, nr;
9371 struct fid_array *fa;
9373 if (optind > argc - 1) {
9374 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9378 device = argv[optind++];
9381 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9387 while (optind < argc) {
9390 fidstr = argv[optind++];
9391 while (*fidstr == '[')
9393 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9395 fprintf(stderr, "unrecognized FID: %s\n",
9400 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9401 /* start another batch */
9402 rc2 = lfs_rmfid_and_show_errors(device, fa);
9409 rc2 = lfs_rmfid_and_show_errors(device, fa);
9417 static int lfs_data_version(int argc, char **argv)
9419 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9427 fprintf(stderr, "%s: FILE must be specified\n",
9432 while ((c = getopt(argc, argv, "hnrw")) != -1) {
9435 data_version_flags = 0;
9438 data_version_flags |= LL_DV_RD_FLUSH;
9441 data_version_flags |= LL_DV_WR_FLUSH;
9445 "%s data_version: unrecognized option '%s'\n",
9446 progname, argv[optind - 1]);
9452 if (optind == argc) {
9453 fprintf(stderr, "%s data_version: FILE must be specified\n",
9458 path = argv[optind];
9459 fd = open(path, O_RDONLY);
9462 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9463 progname, path, strerror(-rc));
9467 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9470 "%s data_version: cannot get version for '%s': %s\n",
9471 progname, path, strerror(-rc));
9473 printf("%ju" "\n", (uintmax_t)data_version);
9479 static int lfs_hsm_state(int argc, char **argv)
9484 struct hsm_user_state hus;
9492 rc = llapi_hsm_state_get(path, &hus);
9494 fprintf(stderr, "can't get hsm state for %s: %s\n",
9495 path, strerror(errno = -rc));
9499 /* Display path name and status flags */
9500 printf("%s: (0x%08x)", path, hus.hus_states);
9502 if (hus.hus_states & HS_RELEASED)
9503 printf(" released");
9504 if (hus.hus_states & HS_EXISTS)
9506 if (hus.hus_states & HS_DIRTY)
9508 if (hus.hus_states & HS_ARCHIVED)
9509 printf(" archived");
9510 /* Display user-settable flags */
9511 if (hus.hus_states & HS_NORELEASE)
9512 printf(" never_release");
9513 if (hus.hus_states & HS_NOARCHIVE)
9514 printf(" never_archive");
9515 if (hus.hus_states & HS_LOST)
9516 printf(" lost_from_hsm");
9518 if (hus.hus_archive_id != 0)
9519 printf(", archive_id:%d", hus.hus_archive_id);
9522 } while (++i < argc);
9527 #define LFS_HSM_SET 0
9528 #define LFS_HSM_CLEAR 1
9531 * Generic function to set or clear HSM flags.
9532 * Used by hsm_set and hsm_clear.
9534 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9536 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9538 struct option long_opts[] = {
9539 { .val = 'A', .name = "archived", .has_arg = no_argument },
9540 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
9541 { .val = 'd', .name = "dirty", .has_arg = no_argument },
9542 { .val = 'e', .name = "exists", .has_arg = no_argument },
9543 { .val = 'h', .name = "help", .has_arg = no_argument },
9544 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
9545 { .val = 'l', .name = "lost", .has_arg = no_argument },
9546 { .val = 'r', .name = "norelease", .has_arg = no_argument },
9551 __u32 archive_id = 0;
9557 while ((c = getopt_long(argc, argv, "aAdehi:lr",
9558 long_opts, NULL)) != -1) {
9564 mask |= HS_NOARCHIVE;
9567 mask |= HS_ARCHIVED;
9570 mask |= HS_NORELEASE;
9580 archive_id = strtol(optarg, &end, 10);
9581 if (errno != 0 || *end != '\0' || archive_id < 0) {
9583 "%s: invalid archive_id: '%s'\n",
9589 fprintf(stderr, "%s: unrecognized option '%s'\n",
9590 progname, argv[optind - 1]);
9597 /* User should have specified a flag */
9601 while (optind < argc) {
9602 path = argv[optind];
9604 /* If mode == 0, this means we apply the mask. */
9605 if (mode == LFS_HSM_SET)
9606 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9608 rc = llapi_hsm_state_set(path, 0, mask, 0);
9611 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9612 path, strerror(errno = -rc));
9621 static int lfs_hsm_action(int argc, char **argv)
9626 struct hsm_current_action hca;
9627 struct hsm_extent he;
9628 enum hsm_user_action hua;
9629 enum hsm_progress_states hps;
9637 rc = llapi_hsm_current_action(path, &hca);
9639 fprintf(stderr, "can't get hsm action for %s: %s\n",
9640 path, strerror(errno = -rc));
9643 he = hca.hca_location;
9644 hua = hca.hca_action;
9645 hps = hca.hca_state;
9647 printf("%s: %s", path, hsm_user_action2name(hua));
9649 /* Skip file without action */
9650 if (hca.hca_action == HUA_NONE) {
9655 printf(" %s ", hsm_progress_state2name(hps));
9657 if ((hps == HPS_RUNNING) &&
9658 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9659 printf("(%llu bytes moved)\n",
9660 (unsigned long long)he.length);
9661 else if ((he.offset + he.length) == LUSTRE_EOF)
9662 printf("(from %llu to EOF)\n",
9663 (unsigned long long)he.offset);
9665 printf("(from %llu to %llu)\n",
9666 (unsigned long long)he.offset,
9667 (unsigned long long)(he.offset + he.length));
9669 } while (++i < argc);
9674 static int lfs_hsm_set(int argc, char **argv)
9676 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9679 static int lfs_hsm_clear(int argc, char **argv)
9681 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9685 * Check file state and return its fid, to be used by lfs_hsm_request().
9687 * \param[in] file Path to file to check
9688 * \param[in,out] fid Pointer to allocated lu_fid struct.
9689 * \param[in,out] last_dev Pointer to last device id used.
9691 * \return 0 on success.
9693 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9699 rc = lstat(file, &st);
9701 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9705 * Checking for regular file as archiving as posix copytool
9706 * rejects archiving files other than regular files
9708 if (!S_ISREG(st.st_mode)) {
9709 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9712 /* A request should be ... */
9713 if (*last_dev != st.st_dev && *last_dev != 0) {
9715 "All files should be on the same filesystem: %s\n",
9719 *last_dev = st.st_dev;
9721 rc = llapi_path2fid(file, fid);
9723 fprintf(stderr, "Cannot read FID of %s: %s\n",
9724 file, strerror(-rc));
9730 /* Fill an HSM HUR item with a given file name.
9732 * If mntpath is set, then the filename is actually a FID, and no
9733 * lookup on the filesystem will be performed.
9735 * \param[in] hur the user request to fill
9736 * \param[in] idx index of the item inside the HUR to fill
9737 * \param[in] mntpath mountpoint of Lustre
9738 * \param[in] fname filename (if mtnpath is NULL)
9739 * or FID (if mntpath is set)
9740 * \param[in] last_dev pointer to last device id used
9742 * \retval 0 on success
9743 * \retval CMD_HELP or a negative errno on error
9745 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9746 const char *mntpath, const char *fname,
9749 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9752 hui->hui_extent.length = -1;
9755 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9757 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9760 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9764 hur->hur_request.hr_itemcount++;
9769 static int lfs_hsm_request(int argc, char **argv, int action)
9771 struct option long_opts[] = {
9772 { .val = 'a', .name = "archive", .has_arg = required_argument },
9773 { .val = 'D', .name = "data", .has_arg = required_argument },
9774 { .val = 'h', .name = "help", .has_arg = no_argument },
9775 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9776 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9779 struct hsm_user_request *hur, *oldhur;
9784 char *filelist = NULL;
9785 char fullpath[PATH_MAX];
9786 char *opaque = NULL;
9790 int nbfile_alloc = 0;
9791 char *some_file = NULL;
9792 char *mntpath = NULL;
9798 while ((c = getopt_long(argc, argv, "a:D:hl:m:",
9799 long_opts, NULL)) != -1) {
9808 if (action != HUA_ARCHIVE &&
9809 action != HUA_REMOVE) {
9811 "error: -a is supported only when archiving or removing\n");
9814 archive_id = atoi(optarg);
9819 some_file = strdup(optarg);
9823 fprintf(stderr, "%s: unrecognized option '%s'\n",
9824 progname, argv[optind - 1]);
9831 /* All remaining args are files, so we have at least nbfile */
9832 nbfile = argc - optind;
9834 if ((nbfile == 0) && (!filelist))
9838 opaque_len = strlen(opaque);
9841 * Alloc the request structure with enough place to store all files
9842 * from command line.
9844 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9846 fprintf(stderr, "Cannot create the request: %s\n",
9850 nbfile_alloc = nbfile;
9852 hur->hur_request.hr_action = action;
9853 hur->hur_request.hr_archive_id = archive_id;
9854 hur->hur_request.hr_flags = 0;
9856 /* All remaining args are files, add them */
9857 if (nbfile != 0 && some_file == NULL)
9858 some_file = strdup(argv[optind]);
9860 for (i = 0; i < nbfile; i++) {
9861 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9867 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9869 /* If a filelist was specified, read the filelist from it. */
9871 fp = fopen(filelist, "r");
9873 fprintf(stderr, "Cannot read the file list %s: %s\n",
9874 filelist, strerror(errno));
9879 while ((rc = getline(&line, &len, fp)) != -1) {
9881 * If allocated buffer was too small, get something
9884 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9887 nbfile_alloc = nbfile_alloc * 2 + 1;
9889 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9893 "hsm: cannot allocate the request: %s\n",
9900 size = hur_len(oldhur);
9903 "hsm: cannot allocate %u files + %u bytes data\n",
9904 oldhur->hur_request.hr_itemcount,
9905 oldhur->hur_request.hr_data_len);
9912 memcpy(hur, oldhur, size);
9917 if (line[strlen(line) - 1] == '\n')
9918 line[strlen(line) - 1] = '\0';
9920 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9921 mntpath, line, &last_dev);
9937 /* If a --data was used, add it to the request */
9938 hur->hur_request.hr_data_len = opaque_len;
9940 memcpy(hur_data(hur), opaque, opaque_len);
9942 /* Send the HSM request */
9943 if (realpath(some_file, fullpath) == NULL) {
9944 fprintf(stderr, "Could not find path '%s': %s\n",
9945 some_file, strerror(errno));
9947 rc = llapi_hsm_request(fullpath, hur);
9949 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9950 some_file, strerror(-rc));
9960 static int lfs_hsm_archive(int argc, char **argv)
9962 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9965 static int lfs_hsm_restore(int argc, char **argv)
9967 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9970 static int lfs_hsm_release(int argc, char **argv)
9972 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9975 static int lfs_hsm_remove(int argc, char **argv)
9977 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9980 static int lfs_hsm_cancel(int argc, char **argv)
9982 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9985 static int lfs_swap_layouts(int argc, char **argv)
9990 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9991 SWAP_LAYOUTS_KEEP_MTIME |
9992 SWAP_LAYOUTS_KEEP_ATIME);
9995 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9997 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9999 int lfs_get_mode(const char *string)
10001 enum lock_mode_user mode;
10003 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10004 if (lock_mode_names[mode] == NULL)
10006 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10013 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10015 enum lu_ladvise_type advice;
10018 advice < ARRAY_SIZE(ladvise_names); advice++) {
10019 if (ladvise_names[advice] == NULL)
10021 if (strcmp(string, ladvise_names[advice]) == 0)
10025 return LU_LADVISE_INVALID;
10028 static int lfs_ladvise(int argc, char **argv)
10030 struct option long_opts[] = {
10031 { .val = 'a', .name = "advice", .has_arg = required_argument },
10032 { .val = 'b', .name = "background", .has_arg = no_argument },
10033 { .val = 'e', .name = "end", .has_arg = required_argument },
10034 { .val = 'h', .name = "help", .has_arg = no_argument },
10035 { .val = 'l', .name = "length", .has_arg = required_argument },
10036 { .val = 'm', .name = "mode", .has_arg = required_argument },
10037 { .val = 's', .name = "start", .has_arg = required_argument },
10038 { .val = 'u', .name = "unset", .has_arg = no_argument },
10039 { .name = NULL } };
10040 struct llapi_lu_ladvise advice;
10041 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10042 unsigned long long start = 0;
10043 unsigned long long end = LUSTRE_EOF;
10044 unsigned long long length = 0;
10045 unsigned long long size_units;
10046 unsigned long long flags = 0;
10052 while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10053 long_opts, NULL)) != -1) {
10056 advice_type = lfs_get_ladvice(optarg);
10057 if (advice_type == LU_LADVISE_INVALID) {
10059 "%s: invalid advice type '%s'\n",
10061 fprintf(stderr, "Valid types:");
10063 for (advice_type = 0;
10064 advice_type < ARRAY_SIZE(ladvise_names);
10066 if (ladvise_names[advice_type] == NULL)
10068 fprintf(stderr, " %s",
10069 ladvise_names[advice_type]);
10071 fprintf(stderr, "\n");
10084 rc = llapi_parse_size(optarg, &end,
10087 fprintf(stderr, "%s: bad end offset '%s'\n",
10094 rc = llapi_parse_size(optarg, &start,
10098 "%s: bad start offset '%s'\n",
10105 rc = llapi_parse_size(optarg, &length,
10108 fprintf(stderr, "%s: bad length '%s'\n",
10114 mode = lfs_get_mode(optarg);
10117 "%s: bad mode '%s', valid modes are READ or WRITE\n",
10123 fprintf(stderr, "%s: unrecognized option '%s'\n",
10124 progname, argv[optind - 1]);
10131 if (advice_type == LU_LADVISE_INVALID) {
10132 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10133 fprintf(stderr, "Valid types:");
10134 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10136 if (ladvise_names[advice_type] == NULL)
10138 fprintf(stderr, " %s", ladvise_names[advice_type]);
10140 fprintf(stderr, "\n");
10144 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10146 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10151 if (argc <= optind) {
10152 fprintf(stderr, "%s: please give one or more file names\n",
10157 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10158 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10163 if (end == LUSTRE_EOF && length != 0)
10164 end = start + length;
10166 if (end <= start) {
10167 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10168 argv[0], start, end);
10172 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10173 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10178 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10179 fprintf(stderr, "%s: mode is required with lockahead\n",
10184 while (optind < argc) {
10187 path = argv[optind++];
10189 fd = open(path, O_RDONLY);
10191 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10192 argv[0], path, strerror(errno));
10197 advice.lla_start = start;
10198 advice.lla_end = end;
10199 advice.lla_advice = advice_type;
10200 advice.lla_value1 = 0;
10201 advice.lla_value2 = 0;
10202 advice.lla_value3 = 0;
10203 advice.lla_value4 = 0;
10204 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10205 advice.lla_lockahead_mode = mode;
10206 advice.lla_peradvice_flags = flags;
10209 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10213 "%s: cannot give advice '%s' to file '%s': %s\n",
10214 argv[0], ladvise_names[advice_type],
10215 path, strerror(errno));
10221 if (rc == 0 && rc2 < 0)
10227 static const char *const heat_names[] = LU_HEAT_NAMES;
10229 static int lfs_heat_get(int argc, char **argv)
10231 struct lu_heat *heat;
10240 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10242 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10247 while (optind < argc) {
10248 path = argv[optind++];
10250 fd = open(path, O_RDONLY);
10252 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10253 argv[0], path, strerror(errno));
10258 heat->lh_count = OBD_HEAT_COUNT;
10259 rc2 = llapi_heat_get(fd, heat);
10263 "%s: cannot get heat of file '%s': %s\n",
10264 argv[0], path, strerror(errno));
10268 printf("flags: %x\n", heat->lh_flags);
10269 for (i = 0; i < heat->lh_count; i++)
10270 printf("%s: %llu\n", heat_names[i],
10271 (unsigned long long)heat->lh_heat[i]);
10273 if (rc == 0 && rc2 < 0)
10281 static int lfs_heat_set(int argc, char **argv)
10283 struct option long_opts[] = {
10284 { .val = 'c', .name = "clear", .has_arg = no_argument },
10285 { .val = 'h', .name = "help", .has_arg = no_argument },
10286 { .val = 'o', .name = "off", .has_arg = no_argument },
10287 { .val = 'O', .name = "on", .has_arg = no_argument },
10288 { .name = NULL } };
10289 enum lu_heat_flag flags = 0;
10299 while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
10302 flags |= LU_HEAT_FLAG_CLEAR;
10305 flags |= LU_HEAT_FLAG_CLEAR;
10306 flags |= LU_HEAT_FLAG_OFF;
10309 flags &= ~LU_HEAT_FLAG_OFF;
10312 fprintf(stderr, "%s: unrecognized option '%s'\n",
10313 progname, argv[optind - 1]);
10320 if (argc <= optind) {
10321 fprintf(stderr, "%s: please give one or more file names\n",
10326 while (optind < argc) {
10327 path = argv[optind++];
10329 fd = open(path, O_RDONLY);
10331 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10332 argv[0], path, strerror(errno));
10337 rc2 = llapi_heat_set(fd, flags);
10341 "%s: cannot setflags heat of file '%s': %s\n",
10342 argv[0], path, strerror(errno));
10346 if (rc == 0 && rc2 < 0)
10353 * The input string contains a comma delimited list of component ids and
10354 * ranges, for example "1,2-4,7".
10356 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10358 bool end_of_loop = false;
10366 while (!end_of_loop) {
10370 char *endptr = NULL;
10373 ptr = strchrnul(arg, ',');
10374 end_of_loop = *ptr == '\0';
10377 start_index = strtol(arg, &endptr, 0);
10378 if (endptr == arg) /* no data at all */
10380 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10382 if (start_index < 0)
10385 end_index = start_index;
10386 if (*endptr == '-') {
10387 end_index = strtol(endptr + 1, &endptr, 0);
10388 if (*endptr != '\0')
10390 if (end_index < start_index)
10394 for (i = start_index; i <= end_index && size > 0; i++) {
10397 /* remove duplicate */
10398 for (j = 0; j < nr; j++) {
10402 if (j == nr) { /* no duplicate */
10408 if (size == 0 && i < end_index)
10415 if (!end_of_loop && ptr)
10418 return rc < 0 ? rc : nr;
10422 * struct verify_mirror_id - Mirror id to be verified.
10423 * @mirror_id: A specified mirror id.
10424 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10426 struct verify_mirror_id {
10432 * compare_mirror_ids() - Compare mirror ids.
10433 * @layout: Mirror component list.
10434 * @cbdata: Callback data in verify_mirror_id structure.
10436 * This is a callback function called by llapi_layout_comp_iterate()
10437 * to compare the specified mirror id with the one in the current
10438 * component of @layout. If they are the same, then the specified
10439 * mirror id is valid.
10441 * Return: a negative error code on failure or
10442 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10443 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
10446 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10448 struct verify_mirror_id *mirror_id_cbdata =
10449 (struct verify_mirror_id *)cbdata;
10450 uint32_t mirror_id;
10453 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10457 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10458 progname, strerror(errno));
10462 if (mirror_id_cbdata->mirror_id == mirror_id) {
10463 mirror_id_cbdata->is_valid_id = true;
10464 return LLAPI_LAYOUT_ITER_STOP;
10467 return LLAPI_LAYOUT_ITER_CONT;
10471 * verify_mirror_ids() - Verify specified mirror ids.
10472 * @fname: Mirrored file name.
10473 * @mirror_ids: Specified mirror ids to be verified.
10474 * @ids_nr: Number of specified mirror ids.
10476 * This function verifies that specified @mirror_ids are valid
10477 * in the mirrored file @fname.
10479 * Return: 0 on success or a negative error code on failure.
10482 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10484 struct llapi_layout *layout = NULL;
10485 struct verify_mirror_id mirror_id_cbdata = { 0 };
10487 uint32_t flr_state;
10496 if (stat(fname, &stbuf) < 0) {
10497 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10498 progname, fname, strerror(errno));
10503 if (!S_ISREG(stbuf.st_mode)) {
10504 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10510 fd = open(fname, O_DIRECT | O_RDONLY);
10512 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10513 progname, fname, strerror(errno));
10518 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10520 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10521 progname, fname, strerror(errno));
10525 layout = llapi_layout_get_by_fd(fd, 0);
10527 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10528 progname, fname, strerror(errno));
10530 llapi_lease_release(fd);
10534 rc = llapi_layout_flags_get(layout, &flr_state);
10536 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10537 progname, fname, strerror(errno));
10542 flr_state &= LCM_FL_FLR_MASK;
10543 switch (flr_state) {
10546 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10547 progname, fname, llapi_layout_flags_string(flr_state));
10554 for (i = 0; i < ids_nr; i++) {
10555 mirror_id_cbdata.mirror_id = mirror_ids[i];
10556 mirror_id_cbdata.is_valid_id = false;
10558 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10559 &mirror_id_cbdata);
10563 "%s: '%s' failed to verify mirror id: %u.\n",
10564 progname, fname, mirror_ids[i]);
10568 if (!mirror_id_cbdata.is_valid_id) {
10571 "%s: '%s' invalid specified mirror id: %u.\n",
10572 progname, fname, mirror_ids[i]);
10578 llapi_layout_free(layout);
10579 llapi_lease_release(fd);
10587 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10588 __u16 *mirror_ids, int ids_nr)
10590 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10591 struct llapi_layout *layout;
10593 uint32_t flr_state;
10602 if (stat(fname, &stbuf) < 0) {
10603 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10604 progname, fname, strerror(errno));
10608 if (!S_ISREG(stbuf.st_mode)) {
10609 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10615 fd = open(fname, O_DIRECT | O_RDWR);
10617 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10618 progname, fname, strerror(errno));
10623 layout = llapi_layout_get_by_fd(fd, 0);
10625 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10626 progname, fname, strerror(errno));
10631 rc = llapi_layout_flags_get(layout, &flr_state);
10633 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10634 progname, fname, strerror(errno));
10639 flr_state &= LCM_FL_FLR_MASK;
10640 if (flr_state == LCM_FL_NONE) {
10642 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10647 /* get stale component info */
10648 comp_size = llapi_mirror_find_stale(layout, comp_array,
10649 ARRAY_SIZE(comp_array),
10650 mirror_ids, ids_nr);
10651 if (comp_size <= 0) {
10656 ioc->lil_mode = LL_LEASE_WRLCK;
10657 ioc->lil_flags = LL_LEASE_RESYNC;
10658 rc = llapi_lease_set(fd, ioc);
10660 if (rc == -EALREADY)
10664 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10665 progname, fname, strerror(-rc));
10669 /* get the read range [start, end) */
10670 start = comp_array[0].lrc_start;
10671 end = comp_array[0].lrc_end;
10672 for (idx = 1; idx < comp_size; idx++) {
10673 if (comp_array[idx].lrc_start < start)
10674 start = comp_array[idx].lrc_start;
10675 if (end < comp_array[idx].lrc_end)
10676 end = comp_array[idx].lrc_end;
10679 rc = llapi_lease_check(fd);
10680 if (rc != LL_LEASE_WRLCK) {
10681 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10686 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10689 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10690 progname, fname, strerror(-rc));
10692 rc = migrate_set_timestamps(fd, &stbuf);
10694 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
10695 progname, fname, strerror(-rc));
10699 /* need to do the lease unlock even resync fails */
10700 ioc->lil_mode = LL_LEASE_UNLCK;
10701 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10702 ioc->lil_count = 0;
10703 for (idx = 0; idx < comp_size; idx++) {
10704 if (comp_array[idx].lrc_synced) {
10705 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10710 rc2 = llapi_lease_set(fd, ioc);
10712 * llapi_lease_set returns lease mode when it request to unlock
10716 /* rc2 == 0 means lost lease lock */
10717 if (rc2 == 0 && rc == 0)
10721 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10723 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10725 llapi_lease_release(fd);
10730 llapi_layout_free(layout);
10737 static inline int lfs_mirror_resync(int argc, char **argv)
10739 struct option long_opts[] = {
10740 { .val = 'h', .name = "help", .has_arg = no_argument },
10741 { .val = 'o', .name = "only", .has_arg = required_argument },
10742 { .name = NULL } };
10743 struct ll_ioc_lease *ioc = NULL;
10744 __u16 mirror_ids[128] = { 0 };
10749 while ((c = getopt_long(argc, argv, "ho:", long_opts, NULL)) >= 0) {
10752 rc = parse_mirror_ids(mirror_ids,
10753 sizeof(mirror_ids) / sizeof(__u16),
10757 "%s: bad mirror ids '%s'.\n",
10764 fprintf(stderr, "%s: unrecognized option '%s'\n",
10765 progname, argv[optind - 1]);
10773 if (argc == optind) {
10774 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10779 if (ids_nr > 0 && argc > optind + 1) {
10781 "%s: option '--only' cannot be used upon multiple files.\n",
10788 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10793 /* set the lease on the file */
10794 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10796 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10797 argv[0], strerror(errno));
10802 for (; optind < argc; optind++) {
10803 rc = lfs_mirror_resync_file(argv[optind], ioc,
10804 mirror_ids, ids_nr);
10805 /* ignore previous file's error, continue with next file */
10808 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10816 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10818 struct llapi_layout *layout;
10821 layout = llapi_layout_get_by_fd(fd, 0);
10823 fprintf(stderr, "could not get layout.\n");
10827 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10829 fprintf(stderr, "failed to iterate layout\n");
10830 llapi_layout_free(layout);
10833 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10834 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10835 llapi_layout_free(layout);
10839 llapi_layout_free(layout);
10845 * Check whether two files are the same file
10846 * \retval 0 same file
10847 * \retval 1 not the same file
10848 * \retval <0 error code
10850 static inline int check_same_file(int fd, const char *f2)
10852 struct stat stbuf1;
10853 struct stat stbuf2;
10855 if (fstat(fd, &stbuf1) < 0)
10858 if (stat(f2, &stbuf2) < 0)
10861 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10862 stbuf1.st_ino == stbuf2.st_ino)
10868 static inline int lfs_mirror_read(int argc, char **argv)
10871 __u16 mirror_id = 0;
10872 const char *outfile = NULL;
10878 const size_t buflen = 4 << 20;
10880 struct option long_opts[] = {
10881 { .val = 'h', .name = "help", .has_arg = no_argument },
10882 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10883 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10884 { .name = NULL } };
10886 while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
10891 unsigned long int id;
10894 id = strtoul(optarg, &end, 0);
10895 if (errno != 0 || *end != '\0' || id == 0 ||
10898 "%s %s: invalid mirror ID '%s'\n",
10899 progname, argv[0], optarg);
10903 mirror_id = (__u16)id;
10910 fprintf(stderr, "%s: unrecognized option '%s'\n",
10911 progname, argv[optind - 1]);
10918 if (argc == optind) {
10919 fprintf(stderr, "%s %s: no mirrored file provided\n",
10920 progname, argv[0]);
10922 } else if (argc > optind + 1) {
10923 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10927 if (mirror_id == 0) {
10928 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10929 progname, argv[0]);
10933 /* open mirror file */
10934 fname = argv[optind];
10935 fd = open(fname, O_DIRECT | O_RDONLY);
10937 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10938 progname, argv[0], fname, strerror(errno));
10942 /* verify mirror id */
10943 rc = verify_mirror_id_by_fd(fd, mirror_id);
10946 "%s %s: cannot find mirror with ID %u in '%s'\n",
10947 progname, argv[0], mirror_id, fname);
10951 /* open output file - O_EXCL ensures output is not the same as input */
10953 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10955 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10956 progname, argv[0], outfile, strerror(errno));
10961 outfd = STDOUT_FILENO;
10964 /* allocate buffer */
10965 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10967 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10968 progname, argv[0], rc);
10974 ssize_t bytes_read;
10975 ssize_t written = 0;
10977 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10978 if (bytes_read < 0) {
10981 "%s %s: fail to read data from mirror %u: %s\n",
10982 progname, argv[0], mirror_id, strerror(-rc));
10987 if (bytes_read == 0)
10990 while (written < bytes_read) {
10993 written2 = write(outfd, buf + written,
10994 bytes_read - written);
10995 if (written2 < 0) {
10997 "%s %s: fail to write %s: %s\n",
10998 progname, argv[0], outfile ? : "STDOUT",
11003 written += written2;
11006 if (written != bytes_read) {
11008 "%s %s: written %ld bytes does not match with %ld read.\n",
11009 progname, argv[0], written, bytes_read);
11031 static inline int lfs_mirror_write(int argc, char **argv)
11034 __u16 mirror_id = 0;
11035 const char *inputfile = NULL;
11041 const size_t buflen = 4 << 20;
11043 size_t page_size = sysconf(_SC_PAGESIZE);
11044 struct ll_ioc_lease_id ioc;
11045 struct option long_opts[] = {
11046 { .val = 'h', .name = "help", .has_arg = no_argument },
11047 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
11048 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11049 { .name = NULL } };
11051 while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11056 unsigned long int id;
11059 id = strtoul(optarg, &end, 0);
11060 if (errno != 0 || *end != '\0' || id == 0 ||
11063 "%s %s: invalid mirror ID '%s'\n",
11064 progname, argv[0], optarg);
11068 mirror_id = (__u16)id;
11072 inputfile = optarg;
11075 fprintf(stderr, "%s: unrecognized option '%s'\n",
11076 progname, argv[optind - 1]);
11083 if (argc == optind) {
11084 fprintf(stderr, "%s %s: no mirrored file provided\n",
11085 progname, argv[0]);
11087 } else if (argc > optind + 1) {
11088 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11092 if (mirror_id == 0) {
11093 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11094 progname, argv[0]);
11098 /* open mirror file */
11099 fname = argv[optind];
11100 fd = open(fname, O_DIRECT | O_WRONLY);
11102 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11103 progname, argv[0], fname, strerror(errno));
11107 /* verify mirror id */
11108 rc = verify_mirror_id_by_fd(fd, mirror_id);
11111 "%s %s: cannot find mirror with ID %u in '%s'\n",
11112 progname, argv[0], mirror_id, fname);
11116 /* open input file */
11118 rc = check_same_file(fd, inputfile);
11121 "%s %s: input file cannot be the mirrored file\n",
11122 progname, argv[0]);
11128 inputfd = open(inputfile, O_RDONLY, 0644);
11130 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11131 progname, argv[0], inputfile, strerror(errno));
11136 inputfd = STDIN_FILENO;
11139 /* allocate buffer */
11140 rc = posix_memalign(&buf, page_size, buflen);
11142 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11143 progname, argv[0], rc);
11144 goto close_inputfd;
11147 /* prepare target mirror components instantiation */
11148 ioc.lil_mode = LL_LEASE_WRLCK;
11149 ioc.lil_flags = LL_LEASE_RESYNC;
11150 ioc.lil_mirror_id = mirror_id;
11151 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11154 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11155 progname, argv[0], fname, strerror(errno));
11161 ssize_t bytes_read;
11165 rc = llapi_lease_check(fd);
11166 if (rc != LL_LEASE_WRLCK) {
11167 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11168 progname, argv[0], fname);
11172 bytes_read = read(inputfd, buf, buflen);
11173 if (bytes_read < 0) {
11176 "%s %s: fail to read data from '%s': %s\n",
11177 progname, argv[0], inputfile ? : "STDIN",
11184 if (bytes_read == 0)
11187 /* round up to page align to make direct IO happy. */
11188 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11190 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11195 "%s %s: fail to write to mirror %u: %s\n",
11196 progname, argv[0], mirror_id,
11204 if (pos & (page_size - 1)) {
11205 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11210 ioc.lil_mode = LL_LEASE_UNLCK;
11211 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11213 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11218 "%s %s: release lease lock of '%s' failed: %s\n",
11219 progname, argv[0], fname, strerror(errno));
11236 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11238 struct llapi_layout *layout;
11239 struct collect_ids_data cid = { .cid_ids = ids,
11241 .cid_exclude = exclude_id, };
11244 layout = llapi_layout_get_by_fd(fd, 0);
11246 fprintf(stderr, "could not get layout\n");
11250 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11252 fprintf(stderr, "failed to iterate layout\n");
11253 llapi_layout_free(layout);
11257 llapi_layout_free(layout);
11259 return cid.cid_count;
11262 #ifndef MIRROR_ID_NEG
11263 #define MIRROR_ID_NEG 0x8000
11266 static inline int lfs_mirror_copy(int argc, char **argv)
11269 __u16 read_mirror_id = 0;
11270 __u16 ids[128] = { 0 };
11272 struct llapi_layout *layout = NULL;
11273 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11280 struct ll_ioc_lease *ioc = NULL;
11281 struct ll_ioc_lease_id *resync_ioc;
11282 struct option long_opts[] = {
11283 { .val = 'h', .name = "help", .has_arg = no_argument },
11284 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
11285 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
11286 { .name = NULL } };
11287 char cmd[PATH_MAX];
11289 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11291 while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
11296 unsigned long int id;
11299 id = strtoul(optarg, &end, 0);
11300 if (errno != 0 || *end != '\0' || id == 0 ||
11303 "%s: invalid read mirror ID '%s'\n",
11308 read_mirror_id = (__u16)id;
11312 if (!strcmp(optarg, "-1")) {
11313 /* specify all other mirrors */
11314 ids[0] = (__u16)-1;
11317 count = parse_mirror_ids((__u16 *)ids,
11325 fprintf(stderr, "%s: unrecognized option '%s'\n",
11326 progname, argv[optind - 1]);
11333 if (argc == optind) {
11334 fprintf(stderr, "%s %s: no mirrored file provided\n",
11335 progname, argv[0]);
11337 } else if (argc > optind + 1) {
11338 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11342 if (read_mirror_id == 0) {
11344 "%s %s: no valid read mirror ID %d is provided\n",
11345 progname, argv[0], read_mirror_id);
11351 "%s %s: no write mirror ID is provided\n",
11352 progname, argv[0]);
11356 for (i = 0; i < count; i++) {
11357 if (read_mirror_id == ids[i]) {
11359 "%s %s: read and write mirror ID cannot be the same\n",
11360 progname, argv[0]);
11365 /* open mirror file */
11366 fname = argv[optind];
11368 fd = open(fname, O_DIRECT | O_RDWR);
11370 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11371 progname, argv[0], fname, strerror(errno));
11375 /* write to all other mirrors */
11376 if (ids[0] == (__u16)-1) {
11377 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11381 "%s %s: failed to get other mirror ids in '%s': %d\n",
11382 progname, argv[0], fname, rc);
11387 /* verify mirror id */
11388 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11391 "%s %s: cannot find mirror with ID %u in '%s'\n",
11392 progname, argv[0], read_mirror_id, fname);
11396 for (i = 0; i < count; i++) {
11397 rc = verify_mirror_id_by_fd(fd, ids[i]);
11400 "%s %s: cannot find mirror with ID %u in '%s'\n",
11401 progname, argv[0], ids[i], fname);
11406 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11409 "%s %s: cannot alloc comp id array for ioc: %s\n",
11410 progname, argv[0], strerror(errno));
11415 /* get stale component info */
11416 layout = llapi_layout_get_by_fd(fd, 0);
11418 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11419 progname, argv[0], fname, strerror(errno));
11423 comp_size = llapi_mirror_find_stale(layout, comp_array,
11424 ARRAY_SIZE(comp_array),
11426 llapi_layout_free(layout);
11427 if (comp_size < 0) {
11432 /* prepare target mirror components instantiation */
11433 resync_ioc = (struct ll_ioc_lease_id *)ioc;
11434 resync_ioc->lil_mode = LL_LEASE_WRLCK;
11435 resync_ioc->lil_flags = LL_LEASE_RESYNC;
11437 resync_ioc->lil_mirror_id = ids[0];
11439 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11440 rc = llapi_lease_set(fd, ioc);
11443 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11444 progname, argv[0], fname, strerror(errno));
11448 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11451 fprintf(stderr, "%s %s: copy error: %d\n",
11452 progname, argv[0], rc);
11456 fprintf(stdout, "mirror copied successfully: ");
11457 for (i = 0; i < copied; i++)
11458 fprintf(stdout, "%d ", ids[i]);
11459 fprintf(stdout, "\n");
11461 ioc->lil_mode = LL_LEASE_UNLCK;
11462 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11463 ioc->lil_count = 0;
11464 for (i = 0; i < comp_size; i++) {
11467 for (j = 0; j < copied; j++) {
11468 if (comp_array[i].lrc_mirror_id != ids[j])
11471 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11475 rc = llapi_lease_set(fd, ioc);
11480 "%s %s: release lease lock of '%s' failed: %s\n",
11481 progname, argv[0], fname, strerror(errno));
11496 * struct verify_chunk - Mirror chunk to be verified.
11497 * @chunk: [start, end) of the chunk.
11498 * @mirror_count: Number of mirror ids in @mirror_id array.
11499 * @mirror_id: Array of valid mirror ids that cover the chunk.
11501 struct verify_chunk {
11502 struct lu_extent chunk;
11503 unsigned int mirror_count;
11504 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11508 * print_chunks() - Print chunk information.
11509 * @fname: Mirrored file name.
11510 * @chunks: Array of chunks.
11511 * @chunk_count: Number of chunks in @chunks array.
11513 * This function prints [start, end) of each chunk in @chunks
11514 * for mirrored file @fname, and also prints the valid mirror ids
11515 * that cover the chunk.
11520 void print_chunks(const char *fname, struct verify_chunk *chunks,
11526 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11527 for (i = 0; i < chunk_count; i++) {
11528 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11530 if (chunks[i].mirror_count == 0)
11531 fprintf(stdout, "\t[");
11533 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11534 for (j = 1; j < chunks[i].mirror_count; j++)
11535 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11537 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11539 fprintf(stdout, "\n");
11543 * print_checksums() - Print CRC-32 checksum values.
11544 * @chunk: A chunk and its corresponding valid mirror ids.
11545 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
11547 * This function prints CRC-32 checksum values on @chunk for
11548 * each valid mirror that covers it.
11553 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
11558 "CRC-32 checksum value for chunk "DEXT":\n",
11559 PEXT(&chunk->chunk));
11560 for (i = 0; i < chunk->mirror_count; i++)
11561 fprintf(stdout, "Mirror %u:\t%#lx\n",
11562 chunk->mirror_id[i], crc[i]);
11563 fprintf(stdout, "\n");
11567 * filter_mirror_id() - Filter specified mirror ids.
11568 * @chunks: Array of chunks.
11569 * @chunk_count: Number of chunks in @chunks array.
11570 * @mirror_ids: Specified mirror ids to be verified.
11571 * @ids_nr: Number of specified mirror ids.
11573 * This function scans valid mirror ids that cover each chunk in @chunks
11574 * and filters specified mirror ids.
11579 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11580 __u16 *mirror_ids, int ids_nr)
11585 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11586 unsigned int valid_count = 0;
11588 for (i = 0; i < chunk_count; i++) {
11589 if (chunks[i].mirror_count == 0)
11593 for (j = 0; j < ids_nr; j++) {
11594 for (k = 0; k < chunks[i].mirror_count; k++) {
11595 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11596 valid_id[valid_count] = mirror_ids[j];
11603 memcpy(chunks[i].mirror_id, valid_id,
11604 sizeof(__u16) * valid_count);
11605 chunks[i].mirror_count = valid_count;
11610 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11611 * @layout: Mirror component list.
11612 * @chunks: Array of chunks.
11613 * @chunks_size: Array size of @chunks.
11615 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11616 * to find out chunk segments and store them in @chunks array.
11618 * The @mirror_id array in each element of @chunks will store the valid
11619 * mirror ids that cover the chunk. If a mirror component covering the
11620 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11621 * will not be stored into the @mirror_id array, and the chunk for that
11622 * mirror will not be verified.
11624 * The @mirror_count in each element of @chunks will store the number of
11625 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11626 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11627 * indicates the chunk is valid in only one mirror. In both cases, the
11628 * chunk will not be verified.
11630 * Here is an example:
11632 * 0 1M 2M 3M 4M EOF
11633 * +------+-------------+--------------------+
11634 * | | | S | mirror1
11635 * +------+------+------+------+-------------+
11636 * | | S | S | | mirror2
11637 * +-------------+------+------+-------------+
11639 * prepared @chunks array will contain 5 elements:
11640 * (([0, 1M), [1, 2], 2),
11641 * ([1M, 2M), [1, 2], 2),
11642 * ([2M, 3M), [1], 1),
11643 * ([3M, 4M], [], 0),
11644 * ([4M, EOF), [2], 1))
11646 * Return: the actual array size of @chunks on success
11647 * or a negative error code on failure.
11650 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11651 struct verify_chunk *chunks,
11652 size_t chunks_size)
11656 uint32_t mirror_id;
11662 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11665 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11668 "%s: move to the first layout component: %s.\n",
11669 progname, strerror(errno));
11675 chunks[idx].chunk.e_end = LUSTRE_EOF;
11677 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11680 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11681 progname, strerror(errno));
11685 if (start > chunks[idx].chunk.e_start ||
11686 end <= chunks[idx].chunk.e_start)
11689 if (end < chunks[idx].chunk.e_end)
11690 chunks[idx].chunk.e_end = end;
11692 rc = llapi_layout_comp_flags_get(layout, &flags);
11695 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11696 progname, strerror(errno));
11700 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11703 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11706 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11707 progname, strerror(errno));
11711 chunks[idx].mirror_id[i] = mirror_id;
11713 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11715 "%s: mirror_id array is too small.\n",
11722 rc = llapi_layout_comp_use(layout,
11723 LLAPI_LAYOUT_COMP_USE_NEXT);
11726 "%s: move to the next layout component: %s.\n",
11727 progname, strerror(errno));
11730 } /* loop through all components */
11732 chunks[idx].mirror_count = i;
11734 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11738 if (idx >= chunks_size) {
11739 fprintf(stderr, "%s: chunks array is too small.\n",
11745 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11749 return rc < 0 ? rc : idx + 1;
11753 * lfs_mirror_verify_chunk() - Verify a chunk.
11754 * @fd: File descriptor of the mirrored file.
11755 * @file_size: Size of the mirrored file.
11756 * @chunk: A chunk and its corresponding valid mirror ids.
11757 * @verbose: Verbose mode.
11759 * This function verifies a @chunk contains exactly the same data
11760 * ammong the mirrors that cover it.
11762 * If @verbose is specified, then the function will print where the
11763 * differences are if the data do not match. Otherwise, it will
11764 * just return an error in that case.
11766 * Return: 0 on success or a negative error code on failure.
11769 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11770 struct verify_chunk *chunk, int verbose)
11772 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11774 size_t page_size = sysconf(_SC_PAGESIZE);
11775 ssize_t bytes_read;
11776 ssize_t bytes_done;
11780 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11784 if (file_size == 0)
11787 rc = posix_memalign(&buf, page_size, buflen);
11788 if (rc) /* error code is returned directly */
11792 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11793 PEXT(&chunk->chunk));
11794 for (i = 0; i < chunk->mirror_count; i++)
11795 fprintf(stdout, " %u", chunk->mirror_id[i]);
11796 fprintf(stdout, "\n");
11800 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11801 pos = chunk->chunk.e_start;
11802 while (bytes_done < count) {
11803 /* compute initial CRC-32 checksum */
11804 crc = crc32(0L, Z_NULL, 0);
11805 memset(crc_array, 0, sizeof(crc_array));
11808 for (i = 0; i < chunk->mirror_count; i++) {
11809 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11811 if (bytes_read < 0) {
11814 "%s: failed to read data from mirror %u: %s.\n",
11815 progname, chunk->mirror_id[i],
11820 /* compute new CRC-32 checksum */
11821 crc_array[i] = crc32(crc, buf, bytes_read);
11825 print_checksums(chunk, crc_array);
11827 /* compare CRC-32 checksum values */
11828 for (i = 1; i < chunk->mirror_count; i++) {
11829 if (crc_array[i] != crc_array[0]) {
11833 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11834 progname, PEXT(&chunk->chunk),
11835 chunk->mirror_id[0],
11836 chunk->mirror_id[i]);
11841 bytes_done += bytes_read;
11844 if (verbose > 1 && rc == 0) {
11845 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11846 PEXT(&chunk->chunk));
11847 for (i = 0; i < chunk->mirror_count; i++)
11848 fprintf(stdout, " %u", chunk->mirror_id[i]);
11849 fprintf(stdout, " PASS\n\n");
11858 * lfs_mirror_verify_file() - Verify a mirrored file.
11859 * @fname: Mirrored file name.
11860 * @mirror_ids: Specified mirror ids to be verified.
11861 * @ids_nr: Number of specified mirror ids.
11862 * @verbose: Verbose mode.
11864 * This function verifies that each SYNC mirror of a mirrored file
11865 * specified by @fname contains exactly the same data.
11867 * If @mirror_ids is specified, then the function will verify the
11868 * mirrors specified by @mirror_ids contain exactly the same data.
11870 * If @verbose is specified, then the function will print where the
11871 * differences are if the data do not match. Otherwise, it will
11872 * just return an error in that case.
11874 * Return: 0 on success or a negative error code on failure.
11877 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11880 struct verify_chunk chunks_array[1024] = { };
11881 struct llapi_layout *layout = NULL;
11883 uint32_t flr_state;
11885 int chunk_count = 0;
11891 if (stat(fname, &stbuf) < 0) {
11892 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11893 progname, fname, strerror(errno));
11898 if (!S_ISREG(stbuf.st_mode)) {
11899 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11905 if (stbuf.st_size == 0) {
11907 fprintf(stdout, "%s: '%s' file size is 0.\n",
11913 fd = open(fname, O_DIRECT | O_RDONLY);
11915 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11916 progname, fname, strerror(errno));
11921 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11923 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11924 progname, fname, strerror(errno));
11928 layout = llapi_layout_get_by_fd(fd, 0);
11930 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11931 progname, fname, strerror(errno));
11933 llapi_lease_release(fd);
11937 rc = llapi_layout_flags_get(layout, &flr_state);
11939 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11940 progname, fname, strerror(errno));
11945 flr_state &= LCM_FL_FLR_MASK;
11946 switch (flr_state) {
11949 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11950 progname, fname, llapi_layout_flags_string(flr_state));
11956 /* find out mirror chunks to be verified */
11957 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11958 ARRAY_SIZE(chunks_array));
11959 if (chunk_count < 0) {
11965 /* filter specified mirror ids */
11966 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11969 print_chunks(fname, chunks_array, chunk_count);
11971 for (idx = 0; idx < chunk_count; idx++) {
11972 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11975 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11977 PEXT(&chunks_array[idx].chunk),
11978 (unsigned long long)stbuf.st_size);
11982 if (chunks_array[idx].mirror_count == 0) {
11984 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11986 PEXT(&chunks_array[idx].chunk));
11988 fprintf(stderr, "skipped\n");
11992 fprintf(stderr, "failed\n");
11996 if (chunks_array[idx].mirror_count == 1) {
11999 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12001 PEXT(&chunks_array[idx].chunk),
12002 chunks_array[idx].mirror_id[0]);
12006 rc = llapi_lease_check(fd);
12007 if (rc != LL_LEASE_RDLCK) {
12008 fprintf(stderr, "%s: '%s' lost lease lock.\n",
12013 /* verify one chunk */
12014 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12015 &chunks_array[idx], verbose);
12029 llapi_layout_free(layout);
12030 llapi_lease_release(fd);
12038 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12039 * @argc: The count of lfs mirror verify command line arguments.
12040 * @argv: Array of strings for lfs mirror verify command line arguments.
12042 * This function parses lfs mirror verify command and verifies the
12043 * specified mirrored file(s).
12045 * Return: 0 on success or a negative error code on failure.
12047 static inline int lfs_mirror_verify(int argc, char **argv)
12049 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12055 char cmd[PATH_MAX];
12057 struct option long_opts[] = {
12058 { .val = 'h', .name = "help", .has_arg = no_argument },
12059 { .val = 'o', .name = "only", .has_arg = required_argument },
12060 { .val = 'v', .name = "verbose", .has_arg = no_argument },
12061 { .name = NULL } };
12063 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12065 while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12068 rc = parse_mirror_ids(mirror_ids,
12069 ARRAY_SIZE(mirror_ids),
12073 "%s: bad mirror ids '%s'.\n",
12080 "%s: at least 2 mirror ids needed with '--only' option.\n",
12090 fprintf(stderr, "%s: unrecognized option '%s'\n",
12091 progname, argv[optind - 1]);
12099 if (argc == optind) {
12100 fprintf(stderr, "%s: no file name given.\n", progname);
12105 if (ids_nr > 0 && argc > optind + 1) {
12107 "%s: '--only' cannot be used upon multiple files.\n",
12114 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12120 for (; optind < argc; optind++) {
12121 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12131 * lfs_mirror() - Parse and execute lfs mirror commands.
12132 * @argc: The count of lfs mirror command line arguments.
12133 * @argv: Array of strings for lfs mirror command line arguments.
12135 * This function parses lfs mirror commands and performs the
12136 * corresponding functions specified in mirror_cmdlist[].
12138 * Return: 0 on success or an error code on failure.
12140 static int lfs_mirror(int argc, char **argv)
12142 char cmd[PATH_MAX];
12145 setlinebuf(stdout);
12147 Parser_init("lfs-mirror > ", mirror_cmdlist);
12149 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12151 program_invocation_short_name = cmd;
12153 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12155 rc = Parser_commands();
12157 return rc < 0 ? -rc : rc;
12160 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12162 #if __BYTE_ORDER == __BIG_ENDIAN
12163 __swab16s(&attrs->lsa_valid);
12164 __swab64s(&attrs->lsa_size);
12165 __swab64s(&attrs->lsa_blocks);
12169 enum lfs_som_type {
12170 LFS_SOM_SIZE = 0x1,
12171 LFS_SOM_BLOCKS = 0x2,
12172 LFS_SOM_FLAGS = 0x4,
12173 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12177 static int lfs_getsom(int argc, char **argv)
12180 struct lustre_som_attrs *attrs;
12181 char buf[sizeof(*attrs) + 64];
12182 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12185 while ((c = getopt(argc, argv, "bfhs")) != -1) {
12188 type = LFS_SOM_BLOCKS;
12191 type = LFS_SOM_FLAGS;
12194 type = LFS_SOM_SIZE;
12197 fprintf(stderr, "%s: unrecognized option '%s'\n",
12198 progname, argv[optind - 1]);
12209 fprintf(stderr, "%s: %s\n",
12210 progname, argc == 0 ? "miss file target" :
12211 "input more than 2 files");
12216 attrs = (void *)buf;
12217 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12220 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12221 argv[0], strerror(errno), errno);
12225 lustre_som_swab(attrs);
12228 case LFS_SOM_ATTR_ALL:
12229 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12230 path, (unsigned long long)attrs->lsa_size,
12231 (unsigned long long)attrs->lsa_blocks,
12235 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12237 case LFS_SOM_BLOCKS:
12238 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12240 case LFS_SOM_FLAGS:
12241 printf("%x\n", attrs->lsa_valid);
12244 fprintf(stderr, "%s: unknown option\n", progname);
12252 * lfs_mirror_list_commands() - List lfs mirror commands.
12253 * @argc: The count of command line arguments.
12254 * @argv: Array of strings for command line arguments.
12256 * This function lists lfs mirror commands defined in mirror_cmdlist[].
12258 * Return: 0 on success.
12260 static int lfs_mirror_list_commands(int argc, char **argv)
12262 char buffer[81] = "";
12264 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12270 static int lfs_pcc_attach(int argc, char **argv)
12272 struct option long_opts[] = {
12273 { .val = 'h', .name = "help", .has_arg = no_argument },
12274 { .val = 'i', .name = "id", .has_arg = required_argument },
12275 { .name = NULL } };
12278 __u32 archive_id = 0;
12281 char fullpath[PATH_MAX];
12282 enum lu_pcc_type type = LU_PCC_READWRITE;
12285 while ((c = getopt_long(argc, argv, "hi:",
12286 long_opts, NULL)) != -1) {
12290 archive_id = strtoul(optarg, &end, 0);
12291 if (errno != 0 || *end != '\0' ||
12292 archive_id == 0 || archive_id > UINT32_MAX) {
12294 "error: %s: bad archive ID '%s'\n",
12300 fprintf(stderr, "%s: unrecognized option '%s'\n",
12301 progname, argv[optind - 1]);
12308 if (archive_id == 0) {
12309 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12313 if (argc <= optind) {
12314 fprintf(stderr, "%s: must specify one or more file names\n",
12319 while (optind < argc) {
12322 path = argv[optind++];
12323 if (!realpath(path, fullpath)) {
12324 fprintf(stderr, "%s: could not find path '%s': %s\n",
12325 argv[0], path, strerror(errno));
12331 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12334 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12335 argv[0], path, archive_id, strerror(-rc2));
12343 static int lfs_pcc_attach_fid(int argc, char **argv)
12345 struct option long_opts[] = {
12346 { .val = 'h', .name = "help", .has_arg = no_argument },
12347 { .val = 'i', .name = "id", .has_arg = required_argument },
12348 { .val = 'm', .name = "mnt", .has_arg = required_argument },
12349 { .name = NULL } };
12352 __u32 archive_id = 0;
12354 const char *mntpath = NULL;
12355 const char *fidstr;
12356 enum lu_pcc_type type = LU_PCC_READWRITE;
12359 while ((c = getopt_long(argc, argv, "hi:m:",
12360 long_opts, NULL)) != -1) {
12364 archive_id = strtoul(optarg, &end, 0);
12365 if (errno != 0 || *end != '\0' ||
12366 archive_id > UINT32_MAX) {
12368 "error: %s: bad archive ID '%s'\n",
12377 fprintf(stderr, "%s: unrecognized option '%s'\n",
12378 progname, argv[optind - 1]);
12385 if (archive_id == 0) {
12386 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12391 fprintf(stderr, "%s: must specify Lustre mount point\n",
12396 if (argc <= optind) {
12397 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12401 while (optind < argc) {
12404 fidstr = argv[optind++];
12406 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12410 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12411 argv[0], fidstr, mntpath, archive_id,
12414 if (rc == 0 && rc2 < 0)
12420 static int lfs_pcc_detach(int argc, char **argv)
12422 struct option long_opts[] = {
12423 { .val = 'h', .name = "help", .has_arg = no_argument },
12424 { .val = 'k', .name = "keep", .has_arg = no_argument },
12425 { .name = NULL } };
12429 char fullpath[PATH_MAX];
12430 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12433 while ((c = getopt_long(argc, argv, "hk",
12434 long_opts, NULL)) != -1) {
12437 detach_opt = PCC_DETACH_OPT_NONE;
12440 fprintf(stderr, "%s: unrecognized option '%s'\n",
12441 progname, argv[optind - 1]);
12448 while (optind < argc) {
12451 path = argv[optind++];
12452 if (!realpath(path, fullpath)) {
12453 fprintf(stderr, "%s: could not find path '%s': %s\n",
12454 argv[0], path, strerror(errno));
12460 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12464 "%s: cannot detach '%s' from PCC: %s\n",
12465 argv[0], path, strerror(errno));
12473 static int lfs_pcc_detach_fid(int argc, char **argv)
12475 struct option long_opts[] = {
12476 { .val = 'h', .name = "help", .has_arg = no_argument },
12477 { .val = 'k', .name = "keep", .has_arg = no_argument },
12478 { .name = NULL } };
12482 const char *mntpath;
12483 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12486 while ((c = getopt_long(argc, argv, "hk",
12487 long_opts, NULL)) != -1) {
12490 detach_opt = PCC_DETACH_OPT_NONE;
12493 fprintf(stderr, "%s: unrecognized option '%s'\n",
12494 progname, argv[optind - 1]);
12501 mntpath = argv[optind++];
12503 while (optind < argc) {
12506 fid = argv[optind++];
12508 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12511 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12512 argv[0], fid, mntpath, strerror(-rc2));
12520 static int lfs_pcc_state(int argc, char **argv)
12524 char fullpath[PATH_MAX];
12525 struct lu_pcc_state state;
12530 fprintf(stderr, "%s: must specify one or more file names\n",
12535 while (optind < argc) {
12538 path = argv[optind++];
12539 if (!realpath(path, fullpath)) {
12540 fprintf(stderr, "%s: could not find path '%s': %s\n",
12541 argv[0], path, strerror(errno));
12547 rc2 = llapi_pcc_state_get(fullpath, &state);
12552 "%s: cannot get PCC state of '%s': %s\n",
12553 argv[0], path, strerror(-rc2));
12557 printf("file: %s", path);
12558 printf(", type: %s", pcc_type2string(state.pccs_type));
12559 if (state.pccs_type == LU_PCC_NONE &&
12560 state.pccs_open_count == 0) {
12565 printf(", PCC file: %s", state.pccs_path);
12566 printf(", user number: %u", state.pccs_open_count);
12567 printf(", flags: %x", state.pccs_flags);
12574 * lfs_pcc_list_commands() - List lfs pcc commands.
12575 * @argc: The count of command line arguments.
12576 * @argv: Array of strings for command line arguments.
12578 * This function lists lfs pcc commands defined in pcc_cmdlist[].
12580 * Return: 0 on success.
12582 static int lfs_pcc_list_commands(int argc, char **argv)
12584 char buffer[81] = "";
12586 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12593 * lfs_pcc() - Parse and execute lfs pcc commands.
12594 * @argc: The count of lfs pcc command line arguments.
12595 * @argv: Array of strings for lfs pcc command line arguments.
12597 * This function parses lfs pcc commands and performs the
12598 * corresponding functions specified in pcc_cmdlist[].
12600 * Return: 0 on success or an error code on failure.
12602 static int lfs_pcc(int argc, char **argv)
12604 char cmd[PATH_MAX];
12607 setlinebuf(stdout);
12609 Parser_init("lfs-pcc > ", pcc_cmdlist);
12611 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12613 program_invocation_short_name = cmd;
12615 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12617 rc = Parser_commands();
12619 return rc < 0 ? -rc : rc;
12622 static int lfs_list_commands(int argc, char **argv)
12624 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12626 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12631 int main(int argc, char **argv)
12635 /* Ensure that liblustreapi constructor has run */
12636 if (!llapi_liblustreapi_initialized())
12637 fprintf(stderr, "liblustreapi was not properly initialized\n");
12639 setlinebuf(stdout);
12642 Parser_init("lfs > ", cmdlist);
12644 progname = program_invocation_short_name; /* Used in error messages */
12646 llapi_set_command_name(argv[1]);
12647 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12648 llapi_clear_command_name();
12650 rc = Parser_commands();
12653 return rc < 0 ? -rc : rc;
12656 #ifdef _LUSTRE_IDL_H_
12657 /* Everything we need here should be included by lustreapi.h. */
12658 # error "lfs should not depend on lustre_idl.h"
12659 #endif /* _LUSTRE_IDL_H_ */