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] [--directory|-d]\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 /* used with "lfs migrate -m" */
3452 { .val = 'd', .name = "directory", .has_arg = no_argument},
3453 /* --non-direct is only valid in migrate mode */
3454 { .val = 'D', .name = "non-direct", .has_arg = no_argument },
3455 { .val = 'E', .name = "comp-end", .has_arg = required_argument},
3456 { .val = 'E', .name = "component-end",
3457 .has_arg = required_argument},
3458 { .val = 'f', .name = "file", .has_arg = required_argument },
3459 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
3460 /* find { .val = 'g', .name = "gid", .has_arg = no_argument }, */
3461 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
3462 { .val = 'h', .name = "help", .has_arg = no_argument },
3463 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument},
3464 { .val = 'i', .name = "stripe-index", .has_arg = required_argument},
3465 { .val = 'i', .name = "stripe_index", .has_arg = required_argument},
3466 { .val = 'I', .name = "comp-id", .has_arg = required_argument},
3467 { .val = 'I', .name = "component-id", .has_arg = required_argument},
3468 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
3469 { .val = 'L', .name = "layout", .has_arg = required_argument },
3470 { .val = 'm', .name = "mdt", .has_arg = required_argument},
3471 { .val = 'm', .name = "mdt-index", .has_arg = required_argument},
3472 { .val = 'm', .name = "mdt_index", .has_arg = required_argument},
3473 /* --non-block is only valid in migrate mode */
3474 { .val = 'n', .name = "non-block", .has_arg = no_argument },
3475 { .val = 'N', .name = "mirror-count", .has_arg = optional_argument},
3476 { .val = 'o', .name = "ost", .has_arg = required_argument },
3477 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3478 { .val = 'o', .name = "ost-list", .has_arg = required_argument },
3479 { .val = 'o', .name = "ost_list", .has_arg = required_argument },
3481 { .val = 'p', .name = "pool", .has_arg = required_argument },
3482 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
3483 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
3484 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
3485 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
3486 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
3487 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
3488 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3489 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
3490 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
3491 /* --verbose is only valid in migrate mode */
3492 { .val = 'v', .name = "verbose", .has_arg = no_argument},
3493 { .val = 'x', .name = "xattr", .has_arg = required_argument },
3494 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3495 { .val = 'y', .name = "yaml", .has_arg = required_argument },
3496 { .val = 'z', .name = "ext-size", .has_arg = required_argument},
3497 { .val = 'z', .name = "extension-size", .has_arg = required_argument},
3500 setstripe_args_init(&lsa);
3502 migrate_mode = (opc == SO_MIGRATE);
3503 mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3504 setstripe_mode = (opc == SO_SETSTRIPE);
3505 if (opc == SO_MIRROR_DELETE) {
3507 mirror_flags = MF_DESTROY;
3510 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3512 while ((c = getopt_long(argc, argv,
3513 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:y:z:",
3514 long_opts, NULL)) >= 0) {
3520 case LFS_COMP_ADD_OPT:
3523 case LFS_COMP_DEL_OPT:
3526 case LFS_COMP_FLAGS_OPT:
3527 result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3528 &lsa.lsa_comp_neg_flags);
3531 if (mirror_mode && lsa.lsa_comp_neg_flags) {
3533 "%s: inverted flags are not supported\n",
3538 case LFS_COMP_SET_OPT:
3541 case LFS_COMP_NO_VERIFY_OPT:
3542 mirror_flags |= MF_NO_VERIFY;
3544 case LFS_MIRROR_ID_OPT: {
3545 unsigned long int id;
3548 id = strtoul(optarg, &end, 0);
3549 if (errno != 0 || *end != '\0' || id == 0 ||
3552 "%s %s: invalid mirror ID '%s'\n",
3553 progname, argv[0], optarg);
3557 mirror_id = (__u16)id;
3560 case LFS_LAYOUT_FLAGS_OPT: {
3563 /* check for numeric flags (foreign and mirror cases) */
3564 if (setstripe_mode && !mirror_mode && !last_mirror) {
3566 flags = strtoul(optarg, &end, 16);
3567 if (errno != 0 || *end != '\0' ||
3568 flags >= UINT32_MAX) {
3570 "%s %s: invalid hex flags '%s'\n",
3571 progname, argv[0], optarg);
3574 if (!foreign_mode) {
3576 "%s %s: hex flags must be specified with --foreign option\n",
3583 if (!mirror_mode || !last_mirror) {
3585 "error: %s: --flags must be specified with --mirror-count|-N option\n",
3590 result = comp_str2flags(optarg, &last_mirror->m_flags,
3597 "%s: inverted flags are not supported\n",
3602 if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3604 "%s: unsupported mirror flags: %s\n",
3611 case LFS_LAYOUT_FOREIGN_OPT:
3613 /* check pure numeric */
3614 type = strtoul(optarg, &end, 0);
3617 type = check_foreign_type_name(optarg);
3618 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3620 "%s %s: unrecognized foreign type '%s'\n",
3625 } else if (type >= UINT32_MAX) {
3627 "%s %s: invalid foreign type '%s'\n",
3628 progname, argv[0], optarg);
3632 foreign_mode = true;
3637 mode = strtoul(mode_opt, &end, 8);
3640 "%s %s: bad mode '%s'\n",
3641 progname, argv[0], mode_opt);
3644 previous_umask = umask(0);
3647 case LFS_LAYOUT_COPY:
3652 if (!migrate_mode) {
3654 "%s %s: -b|--block valid only for migrate command\n",
3658 migration_block = true;
3661 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3663 "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3667 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3671 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3672 if (errno != 0 || *end != '\0'||
3673 lsa.lsa_stripe_count < -1 ||
3674 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3676 "%s %s: invalid stripe count '%s'\n",
3677 progname, argv[0], optarg);
3681 if (lsa.lsa_stripe_count == -1)
3682 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3686 migrate_mdt_param.fp_max_depth = 1;
3688 /* delete the default striping pattern */
3690 if (opc == SO_MIRROR_SPLIT) {
3693 "%s %s: -d cannot used with -f\n",
3697 mirror_flags |= MF_DESTROY;
3702 if (!migrate_mode) {
3704 "%s %s: -D|--non-direct is valid only for migrate command\n",
3708 migration_flags |= MIGRATION_NONDIRECT;
3711 if (lsa.lsa_comp_end != 0) {
3712 result = comp_args_to_layout(lpp, &lsa, true);
3714 fprintf(stderr, "%s: invalid layout\n",
3719 setstripe_args_init_inherit(&lsa);
3722 if (arg_is_eof(optarg)) {
3723 lsa.lsa_comp_end = LUSTRE_EOF;
3725 result = llapi_parse_size(optarg,
3728 /* assume units of KB if too small */
3729 if (lsa.lsa_comp_end < 4096)
3730 lsa.lsa_comp_end *= 1024;
3732 lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3734 "%s %s: invalid component end '%s'\n",
3735 progname, argv[0], optarg);
3741 if (!migrate_mode) {
3743 "--mdt-hash is valid only for migrate command\n");
3747 lsa.lsa_pattern = check_hashtype(optarg);
3748 if (lsa.lsa_pattern == 0) {
3750 "%s %s: bad stripe hash type '%s'\n",
3751 progname, argv[0], optarg);
3757 lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3758 if (errno != 0 || *end != '\0' ||
3759 lsa.lsa_stripe_off < -1 ||
3760 lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3762 "%s %s: invalid stripe offset '%s'\n",
3763 progname, argv[0], optarg);
3766 if (lsa.lsa_stripe_off == -1)
3767 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3770 comp_id = strtoul(optarg, &end, 0);
3771 if (*end != '\0' || comp_id == 0 ||
3772 comp_id > LCME_ID_MAX) {
3774 "%s %s: invalid component ID '%s'\n",
3775 progname, argv[0], optarg);
3780 if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3782 "error: %s: invalid option: %s\n",
3783 progname, argv[optopt + 1]);
3786 if (opc == SO_MIRROR_EXTEND) {
3789 "error: %s: '-N' must exist in front of '%s'\n",
3790 progname, argv[optopt + 1]);
3793 last_mirror->m_file = optarg;
3794 last_mirror->m_count = 1;
3798 mirror_list = lfs_mirror_alloc();
3799 mirror_list->m_file = optarg;
3804 if (strcmp(argv[optind - 1], "mdt") == 0) {
3805 /* Can be only the first component */
3809 "error: 'mdt' layout can be only the first one\n");
3812 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3815 "error: 'mdt' layout size is too big\n");
3818 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3819 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
3820 } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3823 "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3829 if (!migrate_mode) {
3831 "%s %s: -m|--mdt-index is valid only for migrate command\n",
3835 migrate_mdt_mode = true;
3836 lsa.lsa_nr_tgts = parse_targets(tgts,
3837 sizeof(tgts) / sizeof(__u32),
3838 lsa.lsa_nr_tgts, optarg, NULL);
3839 if (lsa.lsa_nr_tgts < 0) {
3841 "%s: invalid MDT target(s) '%s'\n",
3846 lsa.lsa_tgts = tgts;
3847 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3848 lsa.lsa_stripe_off = tgts[0];
3851 if (!migrate_mode) {
3853 "%s %s: -n|--non-block valid only for migrate command\n",
3857 migration_flags |= MIGRATION_NONBLOCK;
3860 if (opc == SO_SETSTRIPE) {
3861 opc = SO_MIRROR_CREATE;
3867 mirror_count = strtoul(optarg, &end, 0);
3868 if (errno != 0 || *end != '\0' ||
3869 mirror_count == 0 ||
3870 mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3872 "error: %s: bad mirror count: %s\n",
3879 new_mirror = lfs_mirror_alloc();
3880 new_mirror->m_count = mirror_count;
3883 mirror_list = new_mirror;
3886 /* wrap up last mirror */
3887 if (!setstripe_args_specified(&lsa))
3888 last_mirror->m_inherit = true;
3889 if (lsa.lsa_comp_end == 0)
3890 lsa.lsa_comp_end = LUSTRE_EOF;
3892 result = comp_args_to_layout(lpp, &lsa, true);
3894 lfs_mirror_free(new_mirror);
3898 setstripe_args_init_inherit(&lsa);
3900 last_mirror->m_next = new_mirror;
3903 last_mirror = new_mirror;
3904 lpp = &last_mirror->m_layout;
3907 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3908 if (strcmp(argv[optind - 1], "--ost-list") == 0)
3910 "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3912 if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3914 "%s %s: -o|--ost incompatible with DoM layout\n",
3919 * -o allows overstriping, and must note it because
3920 * parse_targets is shared with MDT striping, which
3921 * does not allow duplicates
3923 lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3924 lsa.lsa_nr_tgts = parse_targets(tgts,
3925 sizeof(tgts) / sizeof(__u32),
3926 lsa.lsa_nr_tgts, optarg,
3928 if (lsa.lsa_nr_tgts < 0) {
3930 "%s %s: invalid OST target(s) '%s'\n",
3931 progname, argv[0], optarg);
3935 lsa.lsa_tgts = tgts;
3936 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3937 lsa.lsa_stripe_off = tgts[0];
3942 lsa.lsa_pool_name = optarg;
3944 if (strlen(lsa.lsa_pool_name) == 0 ||
3945 strncmp(lsa.lsa_pool_name, "none",
3946 LOV_MAXPOOLNAME) == 0)
3947 lsa.lsa_pool_name = NULL;
3950 result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3952 /* assume units of KB if too small to be valid */
3953 if (lsa.lsa_stripe_size < 4096)
3954 lsa.lsa_stripe_size *= 1024;
3956 lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3958 "%s %s: invalid stripe size '%s'\n",
3959 progname, argv[0], optarg);
3964 if (!migrate_mode) {
3966 "%s %s: -v|--verbose valid only for migrate command\n",
3970 migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3971 migration_flags = MIGRATION_VERBOSE;
3981 result = llapi_parse_size(optarg,
3982 &lsa.lsa_extension_size,
3986 "%s %s: invalid extension size '%s'\n",
3987 progname, argv[0], optarg);
3991 lsa.lsa_extension_comp = true;
3994 fprintf(stderr, "%s: unrecognized option '%s'\n",
3995 progname, argv[optind - 1]);
4001 fname = argv[optind];
4003 if (optind == argc) {
4004 fprintf(stderr, "%s %s: FILE must be specified\n",
4009 /* lfs migrate $filename should keep the file's layout by default */
4010 if (migrate_mode && !layout && !from_yaml &&
4011 !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4014 if (xattr && !foreign_mode) {
4016 * only print a warning as this is harmless and will be ignored
4019 "%s %s: xattr has been specified for non-foreign layout\n",
4021 } else if (foreign_mode && !xattr) {
4023 "%s %s: xattr must be provided in foreign mode\n",
4028 if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4029 comp_set || comp_id || delete || from_copy ||
4030 setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4033 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4038 if (mirror_mode && mirror_count == 0) {
4040 "error: %s: --mirror-count|-N option is required\n",
4047 if (!setstripe_args_specified(&lsa))
4048 last_mirror->m_inherit = true;
4049 if (lsa.lsa_comp_end == 0)
4050 lsa.lsa_comp_end = LUSTRE_EOF;
4053 if (lsa.lsa_comp_end != 0) {
4054 result = comp_args_to_layout(lpp, &lsa, true);
4056 fprintf(stderr, "error: %s: invalid layout\n",
4063 if (mirror_flags & MF_NO_VERIFY) {
4064 if (opc != SO_MIRROR_EXTEND) {
4066 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4070 } else if (!has_m_file) {
4072 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4079 if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4081 "%s %s: --component-set doesn't have component-id set\n",
4086 if ((delete + comp_set + comp_del + comp_add) > 1) {
4088 "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4093 if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4094 lsa.lsa_comp_flags != 0 || layout != NULL)) {
4096 "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4101 if ((comp_set || comp_del) &&
4102 (setstripe_args_specified(&lsa) || layout != NULL)) {
4104 "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4109 if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4111 "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4116 if (comp_add || comp_del) {
4119 result = lstat(fname, &st);
4120 if (result == 0 && S_ISDIR(st.st_mode)) {
4122 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4129 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4138 "%s %s: option -E must be specified with --component-add\n",
4144 if (from_yaml && from_copy) {
4146 "%s: can't specify --yaml and --copy together\n",
4151 if ((from_yaml || from_copy) &&
4152 (setstripe_args_specified(&lsa) || layout != NULL)) {
4154 "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4159 if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4161 "%s %s: options --non-block and --block are mutually exclusive\n",
4166 if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4167 opc != SO_MIRROR_DELETE && comp_id != 0) {
4169 "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4174 if (migrate_mdt_mode) {
4175 struct lmv_user_md *lmu;
4177 /* initialize migrate mdt parameters */
4178 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4179 LMV_USER_MAGIC_SPECIFIC));
4182 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4183 progname, argv[0], strerror(ENOMEM));
4187 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4188 lmu->lum_stripe_count = lsa.lsa_stripe_count;
4189 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4191 "%s %s: migrate should specify MDT index\n",
4196 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4197 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4198 lmu->lum_hash_type = lsa.lsa_pattern;
4200 lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4201 if (lsa.lsa_pool_name) {
4202 strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4203 sizeof(lmu->lum_pool_name) - 1);
4204 lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4206 if (lsa.lsa_nr_tgts > 1) {
4209 if (lsa.lsa_stripe_count > 0 &&
4210 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4211 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4213 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4214 progname, lsa.lsa_stripe_count,
4220 lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4221 lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4222 for (i = 0; i < lsa.lsa_nr_tgts; i++)
4223 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4225 lmu->lum_magic = LMV_USER_MAGIC;
4228 migrate_mdt_param.fp_lmv_md = lmu;
4229 migrate_mdt_param.fp_migrate = 1;
4230 } else if (!layout) {
4231 if (lsa_args_stripe_count_check(&lsa))
4234 /* initialize stripe parameters */
4235 param = calloc(1, offsetof(typeof(*param),
4236 lsp_osts[lsa.lsa_nr_tgts]));
4239 "%s %s: cannot allocate memory for parameters: %s\n",
4240 progname, argv[0], strerror(ENOMEM));
4245 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4246 param->lsp_stripe_size = lsa.lsa_stripe_size;
4247 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4248 if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4249 param->lsp_stripe_count = -1;
4251 param->lsp_stripe_count = lsa.lsa_stripe_count;
4253 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4254 param->lsp_stripe_offset = -1;
4256 param->lsp_stripe_offset = lsa.lsa_stripe_off;
4257 param->lsp_stripe_pattern =
4258 llapi_pattern_to_lov(lsa.lsa_pattern);
4259 if (param->lsp_stripe_pattern == EINVAL) {
4260 fprintf(stderr, "error: %s: invalid stripe pattern\n",
4265 param->lsp_pool = lsa.lsa_pool_name;
4266 param->lsp_is_specific = false;
4268 if (lsa.lsa_nr_tgts > 0) {
4269 param->lsp_is_specific = true;
4270 param->lsp_stripe_count = lsa.lsa_nr_tgts;
4271 memcpy(param->lsp_osts, tgts,
4272 sizeof(*tgts) * lsa.lsa_nr_tgts);
4277 /* generate a layout from a YAML template */
4278 result = lfs_comp_create_from_yaml(template, &layout,
4282 "error: %s: can't create composite layout from template file %s\n",
4288 if (layout != NULL || mirror_list != NULL) {
4290 result = mirror_adjust_first_extents(mirror_list);
4292 result = layout_adjust_first_extent(fname, layout,
4294 if (result == -ENODATA)
4296 else if (result != 0) {
4297 fprintf(stderr, "error: %s: invalid layout\n",
4303 for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4305 layout = llapi_layout_get_by_path(template ?: fname, 0);
4308 "%s: can't create composite layout from file %s: %s\n",
4309 progname, template ?: fname,
4316 if (migrate_mdt_mode) {
4317 result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4318 } else if (migrate_mode) {
4319 result = lfs_migrate(fname, migration_flags, param,
4321 } else if (comp_set != 0) {
4322 result = lfs_component_set(fname, comp_id,
4325 lsa.lsa_comp_neg_flags);
4326 } else if (comp_del != 0) {
4327 result = lfs_component_del(fname, comp_id,
4329 lsa.lsa_comp_neg_flags);
4330 } else if (comp_add != 0) {
4331 result = lfs_component_add(fname, layout);
4332 } else if (opc == SO_MIRROR_CREATE) {
4333 result = mirror_create(fname, mirror_list);
4334 } else if (opc == SO_MIRROR_EXTEND) {
4335 result = mirror_extend(fname, mirror_list,
4337 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4338 if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4340 "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4344 if (lsa.lsa_pool_name)
4345 mirror_flags |= MF_COMP_POOL;
4346 else if (mirror_id != 0)
4347 comp_id = mirror_id;
4349 mirror_flags |= MF_COMP_ID;
4350 if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4352 "%s: the file specified by -f cannot be same as the source file '%s'\n",
4356 result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4358 has_m_file ? mirror_list->m_file :
4360 } else if (layout) {
4361 result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4367 } else if (foreign_mode) {
4368 result = llapi_file_create_foreign(fname, mode, type,
4375 result = llapi_file_open_param(fname,
4384 /* Save the first error encountered. */
4392 umask(previous_umask);
4395 free(migrate_mdt_param.fp_lmv_md);
4396 llapi_layout_free(layout);
4397 lfs_mirror_list_free(mirror_list);
4402 llapi_layout_free(layout);
4403 lfs_mirror_list_free(mirror_list);
4407 static int lfs_poollist(int argc, char **argv)
4412 return llapi_poollist(argv[1]);
4415 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4416 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4421 char *endptr = "AD";
4426 else if (str[0] == '-')
4432 for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4433 long long val = strtoll(timebuf, &endptr, 0);
4438 unit *= 52; /* 52 weeks + 1 day below */
4439 case 'w': /* fallthrough */
4441 if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4442 param->fp_time_margin *= (1 + unit / 52);
4443 unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4444 case '\0': /* days are default unit if none used */
4445 case 'd': /* fallthrough */
4447 case 'h': /* fallthrough */
4449 case 'm': /* fallthrough */
4451 case 's': /* fallthrough */
4453 /* don't need to multiply by 1 for seconds */
4456 "%s find: bad time string '%s': %s\n",
4457 progname, timebuf, strerror(EINVAL));
4461 if (param->fp_time_margin == 0 ||
4462 (*endptr && unit < param->fp_time_margin))
4463 param->fp_time_margin = unit;
4470 fprintf(stderr, "%s find: bad time '%s': too large\n",
4480 static int str2quotaid(__u32 *id, const char *arg)
4482 unsigned long int projid_tmp = 0;
4483 char *endptr = NULL;
4485 projid_tmp = strtoul(arg, &endptr, 10);
4486 if (*endptr != '\0')
4488 /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4489 if (projid_tmp >= UINT32_MAX)
4496 static int name2uid(unsigned int *id, const char *name)
4498 struct passwd *passwd;
4500 passwd = getpwnam(name);
4503 *id = passwd->pw_uid;
4508 static int name2gid(unsigned int *id, const char *name)
4510 struct group *group;
4512 group = getgrnam(name);
4515 *id = group->gr_gid;
4520 static inline int name2projid(unsigned int *id, const char *name)
4525 static int uid2name(char **name, unsigned int id)
4527 struct passwd *passwd;
4529 passwd = getpwuid(id);
4532 *name = passwd->pw_name;
4537 static inline int gid2name(char **name, unsigned int id)
4539 struct group *group;
4541 group = getgrgid(id);
4544 *name = group->gr_name;
4549 static int name2layout(__u32 *layout, char *name)
4551 char *ptr, *layout_name;
4554 for (ptr = name; ; ptr = NULL) {
4555 layout_name = strtok(ptr, ",");
4558 if (strcmp(layout_name, "released") == 0)
4559 *layout |= LOV_PATTERN_F_RELEASED;
4560 else if (strcmp(layout_name, "raid0") == 0)
4561 *layout |= LOV_PATTERN_RAID0;
4562 else if (strcmp(layout_name, "mdt") == 0)
4563 *layout |= LOV_PATTERN_MDT;
4564 else if (strcmp(layout_name, "overstriping") == 0)
4565 *layout |= LOV_PATTERN_OVERSTRIPING;
4572 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4575 int user, group, other;
4580 mode_t previous_flags;
4582 user = group = other = 0;
4602 user = group = other = 1;
4613 who = user || group || other;
4616 usermask = umask(0022);
4621 if (*input == '-' || *input == '+' || *input == '=')
4624 /* operation is required */
4627 /* get the flags in *outmode */
4630 previous_flags = (*outmode & 0700);
4631 perm |= user ? previous_flags : 0;
4632 perm |= group ? (previous_flags >> 3) : 0;
4633 perm |= other ? (previous_flags >> 6) : 0;
4637 previous_flags = (*outmode & 0070);
4638 perm |= user ? (previous_flags << 3) : 0;
4639 perm |= group ? previous_flags : 0;
4640 perm |= other ? (previous_flags >> 3) : 0;
4644 previous_flags = (*outmode & 0007);
4645 perm |= user ? (previous_flags << 6) : 0;
4646 perm |= group ? (previous_flags << 3) : 0;
4647 perm |= other ? previous_flags : 0;
4654 /* this part is optional,
4655 * if empty perm = 0 and *outmode is not modified
4662 perm |= user ? 0400 : 0;
4663 perm |= group ? 0040 : 0;
4664 perm |= other ? 0004 : 0;
4665 /* set read permission for uog except for umask's
4668 perm |= who ? 0 : (0444 & ~usermask);
4671 perm |= user ? 0200 : 0;
4672 perm |= group ? 0020 : 0;
4673 perm |= other ? 0002 : 0;
4674 /* set write permission for uog except for umask'
4677 perm |= who ? 0 : (0222 & ~usermask);
4680 perm |= user ? 0100 : 0;
4681 perm |= group ? 0010 : 0;
4682 perm |= other ? 0001 : 0;
4683 /* set execute permission for uog except for umask'
4686 perm |= who ? 0 : (0111 & ~usermask);
4690 * Adds execute permission to 'u', 'g' and/or 'g' if
4691 * specified and either 'u', 'g' or 'o' already has
4692 * execute permissions.
4694 if ((*outmode & 0111) != 0) {
4695 perm |= user ? 0100 : 0;
4696 perm |= group ? 0010 : 0;
4697 perm |= other ? 0001 : 0;
4698 perm |= !who ? 0111 : 0;
4702 /* s is ignored if o is given, but it's not an error */
4703 if (other && !group && !user)
4705 perm |= user ? S_ISUID : 0;
4706 perm |= group ? S_ISGID : 0;
4709 /* 't' should be used when 'a' is given
4712 perm |= (!who || all) ? S_ISVTX : 0;
4713 /* using ugo with t is not an error */
4724 /* uog flags should be only one character long */
4725 if (previous_flags && (*input != '\0' && *input != ','))
4730 /* remove the flags from outmode */
4734 /* add the flags to outmode */
4738 /* set the flags of outmode to perm */
4748 static int str2mode_t(const char *input, mode_t *outmode)
4755 if (*input >= '0' && *input <= '7') {
4756 /* parse octal representation */
4761 /* look for invalid digits in octal representation */
4762 while (isdigit(*iter))
4767 *outmode = strtoul(input, &end, 8);
4769 if (errno != 0 || *outmode > 07777) {
4774 } else if (*input == '8' || *input == '9') {
4775 /* error: invalid octal number */
4778 /* parse coma seperated list of symbolic representation */
4787 rc = parse_symbolic(input, outmode, &end);
4792 } while (*end == ',');
4800 static int lfs_find(int argc, char **argv)
4805 struct find_param param = {
4808 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4810 struct option long_opts[] = {
4811 { .val = 'A', .name = "atime", .has_arg = required_argument },
4812 { .val = 'b', .name = "blocks", .has_arg = required_argument },
4813 { .val = 'B', .name = "btime", .has_arg = required_argument },
4814 { .val = 'B', .name = "Btime", .has_arg = required_argument },
4815 { .val = LFS_COMP_COUNT_OPT,
4816 .name = "comp-count", .has_arg = required_argument },
4817 { .val = LFS_COMP_COUNT_OPT,
4818 .name = "component-count",
4819 .has_arg = required_argument },
4820 { .val = LFS_COMP_FLAGS_OPT,
4821 .name = "comp-flags", .has_arg = required_argument },
4822 { .val = LFS_COMP_FLAGS_OPT,
4823 .name = "component-flags",
4824 .has_arg = required_argument },
4825 { .val = LFS_COMP_START_OPT,
4826 .name = "comp-start", .has_arg = required_argument },
4827 { .val = LFS_COMP_START_OPT,
4828 .name = "component-start",
4829 .has_arg = required_argument },
4830 { .val = LFS_MIRROR_STATE_OPT,
4831 .name = "mirror-state", .has_arg = required_argument },
4832 { .val = LFS_NEWERXY_OPT,
4833 .name = "newer", .has_arg = required_argument},
4834 { .val = LFS_NEWERXY_OPT,
4835 .name = "neweraa", .has_arg = required_argument},
4836 { .val = LFS_NEWERXY_OPT,
4837 .name = "neweram", .has_arg = required_argument},
4838 { .val = LFS_NEWERXY_OPT,
4839 .name = "newerac", .has_arg = required_argument},
4840 { .val = LFS_NEWERXY_OPT,
4841 .name = "newerab", .has_arg = required_argument},
4842 { .val = LFS_NEWERXY_OPT,
4843 .name = "newerma", .has_arg = required_argument},
4844 { .val = LFS_NEWERXY_OPT,
4845 .name = "newermm", .has_arg = required_argument},
4846 { .val = LFS_NEWERXY_OPT,
4847 .name = "newermc", .has_arg = required_argument},
4848 { .val = LFS_NEWERXY_OPT,
4849 .name = "newermb", .has_arg = required_argument},
4850 { .val = LFS_NEWERXY_OPT,
4851 .name = "newerca", .has_arg = required_argument},
4852 { .val = LFS_NEWERXY_OPT,
4853 .name = "newercm", .has_arg = required_argument},
4854 { .val = LFS_NEWERXY_OPT,
4855 .name = "newercc", .has_arg = required_argument},
4856 { .val = LFS_NEWERXY_OPT,
4857 .name = "newercb", .has_arg = required_argument},
4858 { .val = LFS_NEWERXY_OPT,
4859 .name = "newerba", .has_arg = required_argument},
4860 { .val = LFS_NEWERXY_OPT,
4861 .name = "newerbm", .has_arg = required_argument},
4862 { .val = LFS_NEWERXY_OPT,
4863 .name = "newerbc", .has_arg = required_argument},
4864 { .val = LFS_NEWERXY_OPT,
4865 .name = "newerbb", .has_arg = required_argument},
4866 { .val = LFS_NEWERXY_OPT,
4867 .name = "newerBa", .has_arg = required_argument},
4868 { .val = LFS_NEWERXY_OPT,
4869 .name = "newerBm", .has_arg = required_argument},
4870 { .val = LFS_NEWERXY_OPT,
4871 .name = "newerBc", .has_arg = required_argument},
4872 { .val = LFS_NEWERXY_OPT,
4873 .name = "newerBB", .has_arg = required_argument},
4874 { .val = LFS_NEWERXY_OPT,
4875 .name = "newerat", .has_arg = required_argument},
4876 { .val = LFS_NEWERXY_OPT,
4877 .name = "newermt", .has_arg = required_argument},
4878 { .val = LFS_NEWERXY_OPT,
4879 .name = "newerct", .has_arg = required_argument},
4880 { .val = LFS_NEWERXY_OPT,
4881 .name = "newerbt", .has_arg = required_argument},
4882 { .val = LFS_NEWERXY_OPT,
4883 .name = "newerBt", .has_arg = required_argument},
4884 { .val = 'c', .name = "stripe-count", .has_arg = required_argument },
4885 { .val = 'c', .name = "stripe_count", .has_arg = required_argument },
4886 { .val = 'C', .name = "ctime", .has_arg = required_argument },
4887 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4888 { .val = 'D', .name = "maxdepth", .has_arg = required_argument },
4889 { .val = 'E', .name = "comp-end", .has_arg = required_argument },
4890 { .val = 'E', .name = "component-end",
4891 .has_arg = required_argument },
4892 /* find { .val = 'F', .name = "fid", .has_arg = no_argument }, */
4893 { .val = LFS_LAYOUT_FOREIGN_OPT,
4894 .name = "foreign", .has_arg = optional_argument},
4895 { .val = 'g', .name = "gid", .has_arg = required_argument },
4896 { .val = 'G', .name = "group", .has_arg = required_argument },
4897 { .val = 'h', .name = "help", .has_arg = no_argument },
4898 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
4899 { .val = 'i', .name = "stripe-index", .has_arg = required_argument },
4900 { .val = 'i', .name = "stripe_index", .has_arg = required_argument },
4901 /* getstripe { .val = 'I', .name = "comp-id", .has_arg = required_argument }*/
4902 { .val = 'l', .name = "lazy", .has_arg = no_argument },
4903 { .val = 'L', .name = "layout", .has_arg = required_argument },
4904 { .val = 'm', .name = "mdt", .has_arg = required_argument },
4905 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
4906 { .val = 'm', .name = "mdt_index", .has_arg = required_argument },
4907 { .val = 'M', .name = "mtime", .has_arg = required_argument },
4908 { .val = 'n', .name = "name", .has_arg = required_argument },
4909 { .val = 'N', .name = "mirror-count", .has_arg = required_argument },
4910 /* find { .val = 'o' .name = "or", .has_arg = no_argument }, like find(1) */
4911 { .val = 'O', .name = "obd", .has_arg = required_argument },
4912 { .val = 'O', .name = "ost", .has_arg = required_argument },
4913 /* no short option for pool yet, can be 'p' after 2.18 */
4914 { .val = LFS_POOL_OPT,
4915 .name = "pool", .has_arg = required_argument },
4916 { .val = '0', .name = "print0", .has_arg = no_argument },
4917 { .val = 'P', .name = "print", .has_arg = no_argument },
4918 { .val = LFS_PROJID_OPT,
4919 .name = "projid", .has_arg = required_argument },
4920 /* getstripe { .val = 'q', .name = "quiet", .has_arg = no_argument }, */
4921 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4922 /* getstripe { .val = 'R', .name = "raw", .has_arg = no_argument }, */
4923 { .val = 's', .name = "size", .has_arg = required_argument },
4924 { .val = 'S', .name = "stripe-size", .has_arg = required_argument },
4925 { .val = 'S', .name = "stripe_size", .has_arg = required_argument },
4926 { .val = 't', .name = "type", .has_arg = required_argument },
4927 { .val = LFS_FIND_PERM,
4928 .name = "perm", .has_arg = required_argument },
4929 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
4930 { .val = 'u', .name = "uid", .has_arg = required_argument },
4931 { .val = 'U', .name = "user", .has_arg = required_argument },
4932 { .val = 'z', .name = "extension-size",
4933 .has_arg = required_argument },
4934 { .val = 'z', .name = "ext-size", .has_arg = required_argument },
4935 /* getstripe { .val = 'v', .name = "verbose", .has_arg = no_argument }, */
4936 /* getstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
4950 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4951 while ((c = getopt_long_only(argc, argv,
4952 "-0A:b:B:c:C:D:E:g:G:hH:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4953 long_opts, &optidx)) >= 0) {
4958 /* '!' is part of option */
4960 * when getopt_long_only() finds a string which is not
4961 * an option nor a known option argument it returns 1
4962 * in that case if we already have found pathstart and pathend
4963 * (i.e. we have the list of pathnames),
4964 * the only supported value is "!"
4966 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4967 if (!isoption && pathend != -1) {
4969 "err: %s: filename|dirname must either precede options or follow options\n",
4974 if (!isoption && pathstart == -1)
4975 pathstart = optind - 1;
4976 if (isoption && pathstart != -1 && pathend == -1)
4977 pathend = optind - 2;
4984 * unknown; opt is "!" or path component,
4985 * checking done above.
4987 if (strcmp(optarg, "!") == 0)
4991 xtime = ¶m.fp_atime;
4992 xsign = ¶m.fp_asign;
4993 param.fp_exclude_atime = !!neg_opt;
4994 /* no break, this falls through to 'B' for btime */
4997 xtime = ¶m.fp_btime;
4998 xsign = ¶m.fp_bsign;
4999 param.fp_exclude_btime = !!neg_opt;
5001 /* no break, this falls through to 'C' for ctime */
5004 xtime = ¶m.fp_ctime;
5005 xsign = ¶m.fp_csign;
5006 param.fp_exclude_ctime = !!neg_opt;
5008 /* no break, this falls through to 'M' for mtime */
5011 xtime = ¶m.fp_mtime;
5012 xsign = ¶m.fp_msign;
5013 param.fp_exclude_mtime = !!neg_opt;
5015 rc = set_time(¶m, &t, xtime, optarg);
5016 if (rc == LONG_MAX) {
5024 if (optarg[0] == '+') {
5025 param.fp_blocks_sign = -1;
5027 } else if (optarg[0] == '-') {
5028 param.fp_blocks_sign = 1;
5032 param.fp_blocks_units = 1024;
5033 ret = llapi_parse_size(optarg, ¶m.fp_blocks,
5034 ¶m.fp_blocks_units, 0);
5036 fprintf(stderr, "error: bad blocks '%s'\n",
5040 param.fp_check_blocks = 1;
5041 param.fp_exclude_blocks = !!neg_opt;
5043 case LFS_COMP_COUNT_OPT:
5044 if (optarg[0] == '+') {
5045 param.fp_comp_count_sign = -1;
5047 } else if (optarg[0] == '-') {
5048 param.fp_comp_count_sign = 1;
5053 param.fp_comp_count = strtoul(optarg, &endptr, 0);
5054 if (errno != 0 || *endptr != '\0' ||
5055 param.fp_comp_count > UINT32_MAX) {
5057 "error: bad component count '%s'\n",
5061 param.fp_check_comp_count = 1;
5062 param.fp_exclude_comp_count = !!neg_opt;
5064 case LFS_COMP_FLAGS_OPT:
5065 rc = comp_str2flags(optarg, ¶m.fp_comp_flags,
5066 ¶m.fp_comp_neg_flags);
5069 "error: bad component flags '%s'\n",
5073 param.fp_check_comp_flags = 1;
5075 __u32 flags = param.fp_comp_neg_flags;
5077 param.fp_comp_neg_flags = param.fp_comp_flags;
5078 param.fp_comp_flags = flags;
5081 case LFS_COMP_START_OPT:
5082 if (optarg[0] == '+') {
5083 param.fp_comp_start_sign = -1;
5085 } else if (optarg[0] == '-') {
5086 param.fp_comp_start_sign = 1;
5090 rc = llapi_parse_size(optarg, ¶m.fp_comp_start,
5091 ¶m.fp_comp_start_units, 0);
5094 "error: bad component start '%s'\n",
5098 param.fp_check_comp_start = 1;
5099 param.fp_exclude_comp_start = !!neg_opt;
5101 case LFS_MIRROR_STATE_OPT:
5102 rc = mirror_str2state(optarg, ¶m.fp_mirror_state,
5103 ¶m.fp_mirror_neg_state);
5106 "error: bad mirrored file state '%s'\n",
5110 param.fp_check_mirror_state = 1;
5112 __u16 state = param.fp_mirror_neg_state;
5114 param.fp_mirror_neg_state =
5115 param.fp_mirror_state;
5116 param.fp_mirror_state = state;
5120 if (optarg[0] == '+') {
5121 param.fp_stripe_count_sign = -1;
5123 } else if (optarg[0] == '-') {
5124 param.fp_stripe_count_sign = 1;
5129 param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5130 if (errno != 0 || *endptr != '\0' ||
5131 param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5133 "error: bad stripe_count '%s'\n",
5138 param.fp_check_stripe_count = 1;
5139 param.fp_exclude_stripe_count = !!neg_opt;
5143 param.fp_max_depth = strtol(optarg, 0, 0);
5144 if (errno != 0 || param.fp_max_depth < 0) {
5146 "error: bad maxdepth '%s'\n",
5153 if (optarg[0] == '+') {
5154 param.fp_comp_end_sign = -1;
5156 } else if (optarg[0] == '-') {
5157 param.fp_comp_end_sign = 1;
5161 if (arg_is_eof(optarg)) {
5162 param.fp_comp_end = LUSTRE_EOF;
5163 param.fp_comp_end_units = 1;
5166 rc = llapi_parse_size(optarg,
5168 ¶m.fp_comp_end_units, 0);
5169 /* assume units of KB if too small */
5170 if (param.fp_comp_end < 4096)
5171 param.fp_comp_end *= 1024;
5175 "error: bad component end '%s'\n",
5179 param.fp_check_comp_end = 1;
5180 param.fp_exclude_comp_end = !!neg_opt;
5182 case LFS_LAYOUT_FOREIGN_OPT: {
5183 /* all types by default */
5184 uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5187 /* check pure numeric */
5188 type = strtoul(optarg, &endptr, 0);
5191 type = check_foreign_type_name(optarg);
5192 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5194 "%s %s: unknown foreign type '%s'\n",
5199 } else if (type >= UINT32_MAX) {
5201 "%s %s: invalid foreign type '%s'\n",
5202 progname, argv[0], optarg);
5206 param.fp_foreign_type = type;
5207 param.fp_check_foreign = 1;
5208 param.fp_exclude_foreign = !!neg_opt;
5211 case LFS_NEWERXY_OPT: {
5217 time_t ref = time(NULL);
5219 /* no need to check bad options, they won't get here */
5220 if (strlen(long_opts[optidx].name) == 7) {
5221 x = long_opts[optidx].name[5];
5222 y = long_opts[optidx].name[6];
5226 static const char *const fmts[] = {
5227 "%Y-%m-%d %H:%M:%S",
5230 "%H:%M:%S", /* sometime today */
5239 for (i = 0; fmts[i] != NULL; i++) {
5242 /* Init for times relative to today */
5243 if (strncmp(fmts[i], "%H", 2) == 0)
5244 localtime_r(&ref, &tm);
5246 memset(&tm, 0, sizeof(tm));
5247 ptr = strptime(optarg, fmts[i], &tm);
5249 while (ptr && isspace(*ptr))
5251 if (ptr == optarg + strlen(optarg)) {
5259 "%s: invalid time '%s'\n",
5262 "supported formats are:\n ");
5263 for (i = 0; fmts[i] != NULL; i++)
5264 fprintf(stderr, "'%s', ",
5266 fprintf(stderr, "\n");
5272 } else if (y == 'b' || y == 'B') {
5275 rc = llapi_get_lum_file(optarg, NULL, &stx,
5277 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5278 if (!(stx.stx_mask & STATX_BTIME))
5283 "%s: get btime failed '%s': %s\n",
5289 ref = stx.stx_btime.tv_sec;
5291 struct stat statbuf;
5293 if (stat(optarg, &statbuf) < 0) {
5295 "%s: cannot stat file '%s': %s\n",
5304 ref = statbuf.st_atime;
5307 ref = statbuf.st_mtime;
5310 ref = statbuf.st_ctime;
5314 "%s: invalid Y argument: '%c'\n",
5323 xidx = NEWERXY_ATIME;
5326 xidx = NEWERXY_MTIME;
5329 xidx = NEWERXY_CTIME;
5333 xidx = NEWERXY_BTIME;
5337 "%s: invalid X argument: '%c'\n",
5344 newery = ¶m.fp_newery[xidx][negidx];
5350 *newery = *newery > ref ? ref : *newery;
5352 *newery = *newery > ref ? *newery : ref;
5354 param.fp_newerxy = 1;
5359 rc = name2gid(¶m.fp_gid, optarg);
5361 if (str2quotaid(¶m.fp_gid, optarg)) {
5363 "Group/GID: %s cannot be found.\n",
5369 param.fp_exclude_gid = !!neg_opt;
5370 param.fp_check_gid = 1;
5373 rc = mdthash_input(optarg, ¶m.fp_hash_inflags,
5374 ¶m.fp_hash_exflags,
5375 ¶m.fp_hash_type);
5380 if (param.fp_hash_inflags || param.fp_hash_exflags)
5381 param.fp_check_hash_flag = 1;
5382 param.fp_exclude_hash_type = !!neg_opt;
5388 ret = name2layout(¶m.fp_layout, optarg);
5391 param.fp_exclude_layout = !!neg_opt;
5392 param.fp_check_layout = 1;
5396 rc = name2uid(¶m.fp_uid, optarg);
5398 if (str2quotaid(¶m.fp_uid, optarg)) {
5400 "User/UID: %s cannot be found.\n",
5406 param.fp_exclude_uid = !!neg_opt;
5407 param.fp_check_uid = 1;
5410 param.fp_pattern = (char *)optarg;
5411 param.fp_exclude_pattern = !!neg_opt;
5414 if (optarg[0] == '+') {
5415 param.fp_mirror_count_sign = -1;
5417 } else if (optarg[0] == '-') {
5418 param.fp_mirror_count_sign = 1;
5423 param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5424 if (errno != 0 || *endptr != '\0' ||
5425 param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5427 "error: bad mirror count '%s'\n",
5431 param.fp_check_mirror_count = 1;
5432 param.fp_exclude_mirror_count = !!neg_opt;
5437 char *buf, *token, *next, *p;
5441 buf = strdup(optarg);
5447 param.fp_exclude_obd = !!neg_opt;
5450 while (token && *token) {
5451 token = strchr(token, ',');
5458 param.fp_exclude_mdt = !!neg_opt;
5459 param.fp_num_alloc_mdts += len;
5460 tmp = realloc(param.fp_mdt_uuid,
5461 param.fp_num_alloc_mdts *
5462 sizeof(*param.fp_mdt_uuid));
5468 param.fp_mdt_uuid = tmp;
5470 param.fp_exclude_obd = !!neg_opt;
5471 param.fp_num_alloc_obds += len;
5472 tmp = realloc(param.fp_obd_uuid,
5473 param.fp_num_alloc_obds *
5474 sizeof(*param.fp_obd_uuid));
5480 param.fp_obd_uuid = tmp;
5482 for (token = buf; token && *token; token = next) {
5483 struct obd_uuid *puuid;
5487 ¶m.fp_mdt_uuid[param.fp_num_mdts++];
5490 ¶m.fp_obd_uuid[param.fp_num_obds++];
5492 p = strchr(token, ',');
5499 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5504 strncpy(puuid->uuid, token,
5505 sizeof(puuid->uuid));
5512 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5516 if (strlen(optarg) > LOV_MAXPOOLNAME) {
5518 "Pool name %s is too long (max %d)\n",
5519 optarg, LOV_MAXPOOLNAME);
5524 * We do check for empty pool because empty pool
5525 * is used to find V1 LOV attributes
5527 strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5528 param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5529 param.fp_exclude_pool = !!neg_opt;
5530 param.fp_check_pool = 1;
5532 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5533 case 'p': /* want this for --pool, to match getstripe/find */
5535 "warning: -p deprecated, use --print0 or -0\n");
5538 param.fp_zero_end = 1;
5540 case 'P': /* we always print, this option is a no-op */
5542 case LFS_PROJID_OPT:
5543 rc = name2projid(¶m.fp_projid, optarg);
5545 if (str2quotaid(¶m.fp_projid, optarg)) {
5547 "Invalid project ID: %s\n",
5553 param.fp_exclude_projid = !!neg_opt;
5554 param.fp_check_projid = 1;
5557 if (optarg[0] == '+') {
5558 param.fp_size_sign = -1;
5560 } else if (optarg[0] == '-') {
5561 param.fp_size_sign = 1;
5565 ret = llapi_parse_size(optarg, ¶m.fp_size,
5566 ¶m.fp_size_units, 0);
5568 fprintf(stderr, "error: bad file size '%s'\n",
5572 param.fp_check_size = 1;
5573 param.fp_exclude_size = !!neg_opt;
5576 if (optarg[0] == '+') {
5577 param.fp_stripe_size_sign = -1;
5579 } else if (optarg[0] == '-') {
5580 param.fp_stripe_size_sign = 1;
5584 ret = llapi_parse_size(optarg, ¶m.fp_stripe_size,
5585 ¶m.fp_stripe_size_units, 0);
5586 /* assume units of KB if too small to be valid */
5587 if (param.fp_stripe_size < 4096)
5588 param.fp_stripe_size *= 1024;
5590 fprintf(stderr, "error: bad stripe_size '%s'\n",
5594 param.fp_check_stripe_size = 1;
5595 param.fp_exclude_stripe_size = !!neg_opt;
5598 param.fp_exclude_type = !!neg_opt;
5599 switch (optarg[0]) {
5601 param.fp_type = S_IFBLK;
5604 param.fp_type = S_IFCHR;
5607 param.fp_type = S_IFDIR;
5610 param.fp_type = S_IFREG;
5613 param.fp_type = S_IFLNK;
5616 param.fp_type = S_IFIFO;
5619 param.fp_type = S_IFSOCK;
5622 fprintf(stderr, "%s: bad type '%s'\n",
5629 param.fp_exclude_perm = !!neg_opt;
5630 param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5631 if (*optarg == '/') {
5632 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5634 } else if (*optarg == '-') {
5635 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5639 if (str2mode_t(optarg, ¶m.fp_perm)) {
5640 fprintf(stderr, "error: invalid mode '%s'\n",
5647 if (optarg[0] == '+') {
5648 param.fp_mdt_count_sign = -1;
5650 } else if (optarg[0] == '-') {
5651 param.fp_mdt_count_sign = 1;
5656 param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5657 if (errno != 0 || *endptr != '\0' ||
5658 param.fp_mdt_count >= UINT32_MAX) {
5659 fprintf(stderr, "error: bad mdt_count '%s'\n",
5664 param.fp_check_mdt_count = 1;
5665 param.fp_exclude_mdt_count = !!neg_opt;
5668 if (optarg[0] == '+') {
5669 param.fp_ext_size_sign = -1;
5671 } else if (optarg[0] == '-') {
5672 param.fp_ext_size_sign = 1;
5676 ret = llapi_parse_size(optarg, ¶m.fp_ext_size,
5677 ¶m.fp_ext_size_units, 0);
5679 fprintf(stderr, "error: bad ext-size '%s'\n",
5683 param.fp_ext_size /= SEL_UNIT_SIZE;
5684 param.fp_ext_size_units /= SEL_UNIT_SIZE;
5685 param.fp_check_ext_size = 1;
5686 param.fp_exclude_ext_size = !!neg_opt;
5689 fprintf(stderr, "%s: unrecognized option '%s'\n",
5690 progname, argv[optind - 1]);
5697 if (pathstart == -1) {
5698 fprintf(stderr, "error: %s: no filename|pathname\n",
5702 } else if (pathend == -1) {
5708 rc = llapi_find(argv[pathstart], ¶m);
5711 pathbad = pathstart;
5713 } while (++pathstart < pathend);
5716 fprintf(stderr, "%s: failed for '%s': %s\n",
5717 progname, argv[pathbad], strerror(-rc));
5720 if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5721 free(param.fp_obd_uuid);
5723 if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5724 free(param.fp_mdt_uuid);
5729 static int lfs_getstripe_internal(int argc, char **argv,
5730 struct find_param *param)
5732 struct option long_opts[] = {
5733 /* find { .val = 'A', .name = "atime", .has_arg = required_argument }*/
5734 /* find { .val = 'b', .name = "blocks", .has_arg = required_argument }*/
5735 /* find { .val = 'B', .name = "btime", .has_arg = required_argument }*/
5736 /* find { .val = 'B', .name = "Btime", .has_arg = required_argument }*/
5737 { .val = LFS_COMP_COUNT_OPT,
5738 .name = "comp-count", .has_arg = no_argument },
5739 { .val = LFS_COMP_COUNT_OPT,
5740 .name = "component-count", .has_arg = no_argument },
5741 { .val = LFS_COMP_FLAGS_OPT,
5742 .name = "comp-flags", .has_arg = optional_argument },
5743 { .val = LFS_COMP_FLAGS_OPT,
5744 .name = "component-flags", .has_arg = optional_argument },
5745 { .val = LFS_COMP_START_OPT,
5746 .name = "comp-start", .has_arg = optional_argument },
5747 { .val = LFS_COMP_START_OPT,
5748 .name = "component-start", .has_arg = optional_argument },
5749 { .val = LFS_MIRROR_INDEX_OPT,
5750 .name = "mirror-index", .has_arg = required_argument },
5751 { .val = LFS_MIRROR_ID_OPT,
5752 .name = "mirror-id", .has_arg = required_argument },
5753 { .val = 'c', .name = "stripe-count", .has_arg = no_argument },
5754 { .val = 'c', .name = "stripe_count", .has_arg = no_argument },
5755 /* find { .val = 'C', .name = "ctime", .has_arg = required_argument }*/
5756 { .val = 'd', .name = "directory", .has_arg = no_argument },
5757 { .val = 'D', .name = "default", .has_arg = no_argument },
5758 { .val = 'E', .name = "comp-end", .has_arg = optional_argument },
5759 { .val = 'E', .name = "component-end", .has_arg = optional_argument },
5760 { .val = 'F', .name = "fid", .has_arg = no_argument },
5761 { .val = 'g', .name = "generation", .has_arg = no_argument },
5762 /* find { .val = 'G', .name = "group", .has_arg = required_argument }*/
5763 { .val = 'h', .name = "help", .has_arg = no_argument },
5764 /* dirstripe { .val = 'H', .name = "mdt-hash", .has_arg = required_argument }*/
5765 { .val = 'i', .name = "stripe-index", .has_arg = no_argument },
5766 { .val = 'i', .name = "stripe_index", .has_arg = no_argument },
5767 { .val = 'I', .name = "comp-id", .has_arg = optional_argument },
5768 { .val = 'I', .name = "component-id", .has_arg = optional_argument },
5769 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
5770 { .val = 'L', .name = "layout", .has_arg = no_argument },
5771 { .val = 'm', .name = "mdt", .has_arg = no_argument },
5772 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
5773 { .val = 'm', .name = "mdt_index", .has_arg = no_argument },
5774 /* find { .val = 'M', .name = "mtime", .has_arg = required_argument }*/
5775 /* find { .val = 'n', .name = "name", .has_arg = required_argument }*/
5776 { .val = 'N', .name = "mirror-count", .has_arg = no_argument },
5777 { .val = 'O', .name = "obd", .has_arg = required_argument },
5778 { .val = 'O', .name = "ost", .has_arg = required_argument },
5779 { .val = 'p', .name = "pool", .has_arg = no_argument },
5780 /* find { .val = 'P', .name = "print", .has_arg = no_argument }, */
5781 { .val = 'q', .name = "quiet", .has_arg = no_argument },
5782 { .val = 'r', .name = "recursive", .has_arg = no_argument },
5783 { .val = 'R', .name = "raw", .has_arg = no_argument },
5784 { .val = 'S', .name = "stripe-size", .has_arg = no_argument },
5785 { .val = 'S', .name = "stripe_size", .has_arg = no_argument },
5786 /* find { .val = 't', .name = "type", .has_arg = required_argument }*/
5787 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5788 /* find { .val = 'u', .name = "uid", .has_arg = required_argument }*/
5789 /* find { .val = 'U', .name = "user", .has_arg = required_argument }*/
5790 { .val = 'v', .name = "verbose", .has_arg = no_argument },
5791 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5792 { .val = 'y', .name = "yaml", .has_arg = no_argument },
5793 { .val = 'z', .name = "extension-size", .has_arg = no_argument },
5794 { .val = 'z', .name = "ext-size", .has_arg = no_argument },
5798 int pathstart = -1, pathend = -1;
5802 while ((c = getopt_long(argc, argv,
5803 "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5804 long_opts, NULL)) != -1) {
5808 /* '!' is part of option */
5809 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5810 if (!isoption && pathend != -1) {
5812 "error: %s: filename|dirname must either precede options or follow options\n",
5816 if (!isoption && pathstart == -1)
5817 pathstart = optind - 1;
5818 if (isoption && pathstart != -1 && pathend == -1)
5819 pathend = optind - 2;
5823 /* unknown: opt is "!" */
5824 if (strcmp(optarg, "!") == 0)
5828 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5829 param->fp_verbose |= VERBOSE_COUNT;
5830 param->fp_max_depth = 0;
5833 case LFS_COMP_COUNT_OPT:
5834 param->fp_verbose |= VERBOSE_COMP_COUNT;
5835 param->fp_max_depth = 0;
5837 case LFS_COMP_FLAGS_OPT:
5839 rc = comp_str2flags(optarg,
5840 ¶m->fp_comp_flags,
5841 ¶m->fp_comp_neg_flags);
5844 "error: %s bad component flags '%s'.\n",
5848 param->fp_check_comp_flags = 1;
5850 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5851 param->fp_max_depth = 0;
5854 case LFS_COMP_START_OPT:
5857 if (tmp[0] == '+') {
5858 param->fp_comp_start_sign = -1;
5860 } else if (tmp[0] == '-') {
5861 param->fp_comp_start_sign = 1;
5864 rc = llapi_parse_size(tmp,
5865 ¶m->fp_comp_start,
5866 ¶m->fp_comp_start_units, 0);
5869 "error: %s bad component start '%s'.\n",
5873 param->fp_check_comp_start = 1;
5875 param->fp_verbose |= VERBOSE_COMP_START;
5876 param->fp_max_depth = 0;
5879 case LFS_MIRROR_INDEX_OPT: {
5880 unsigned long int mirror_index;
5882 if (optarg[0] == '+') {
5883 param->fp_mirror_index_sign = -1;
5885 } else if (optarg[0] == '-') {
5886 param->fp_mirror_index_sign = 1;
5891 mirror_index = strtoul(optarg, &end, 0);
5892 if (errno != 0 || *end != '\0' ||
5893 mirror_index > UINT16_MAX || (mirror_index == 0 &&
5894 param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5896 "%s %s: invalid mirror index '%s'\n",
5897 progname, argv[0], optarg);
5901 param->fp_mirror_index = (__u16)mirror_index;
5903 if (param->fp_mirror_id != 0) {
5905 "%s %s: can't specify both mirror index and mirror ID\n",
5909 param->fp_check_mirror_index = 1;
5910 param->fp_exclude_mirror_index = !!neg_opt;
5913 case LFS_MIRROR_ID_OPT: {
5914 unsigned long int mirror_id;
5916 if (optarg[0] == '+') {
5917 param->fp_mirror_id_sign = -1;
5919 } else if (optarg[0] == '-') {
5920 param->fp_mirror_id_sign = 1;
5925 mirror_id = strtoul(optarg, &end, 0);
5926 if (errno != 0 || *end != '\0' ||
5927 mirror_id > UINT16_MAX || (mirror_id == 0 &&
5928 param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5930 "%s %s: invalid mirror ID '%s'\n",
5931 progname, argv[0], optarg);
5935 param->fp_mirror_id = (__u16)mirror_id;
5937 if (param->fp_mirror_index != 0) {
5939 "%s %s: can't specify both mirror index and mirror ID\n",
5943 param->fp_check_mirror_id = 1;
5944 param->fp_exclude_mirror_id = !!neg_opt;
5948 param->fp_max_depth = 0;
5951 param->fp_get_default_lmv = 1;
5956 if (tmp[0] == '+') {
5957 param->fp_comp_end_sign = -1;
5959 } else if (tmp[0] == '-') {
5960 param->fp_comp_end_sign = 1;
5964 if (arg_is_eof(tmp)) {
5965 param->fp_comp_end = LUSTRE_EOF;
5966 param->fp_comp_end_units = 1;
5969 rc = llapi_parse_size(tmp,
5970 ¶m->fp_comp_end,
5971 ¶m->fp_comp_end_units, 0);
5972 /* assume units of KB if too small */
5973 if (param->fp_comp_end < 4096)
5974 param->fp_comp_end *= 1024;
5978 "error: %s bad component end '%s'.\n",
5982 param->fp_check_comp_end = 1;
5984 param->fp_verbose |= VERBOSE_COMP_END;
5985 param->fp_max_depth = 0;
5989 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5990 param->fp_verbose |= VERBOSE_DFID;
5991 param->fp_max_depth = 0;
5995 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5996 param->fp_verbose |= VERBOSE_GENERATION;
5997 param->fp_max_depth = 0;
6001 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6002 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6003 param->fp_max_depth = 0;
6008 param->fp_comp_id = strtoul(optarg, &end, 0);
6009 if (*end != '\0' || param->fp_comp_id == 0 ||
6010 param->fp_comp_id > LCME_ID_MAX) {
6012 "error: %s bad component id '%s'\n",
6016 param->fp_check_comp_id = 1;
6018 param->fp_max_depth = 0;
6019 param->fp_verbose |= VERBOSE_COMP_ID;
6023 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6024 param->fp_verbose |= VERBOSE_PATTERN;
6025 param->fp_max_depth = 0;
6028 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6031 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6034 if (!(param->fp_verbose & VERBOSE_DETAIL))
6035 param->fp_max_depth = 0;
6036 param->fp_verbose |= VERBOSE_MDTINDEX;
6039 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6040 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6041 param->fp_max_depth = 0;
6045 if (param->fp_obd_uuid) {
6047 "error: %s: only one obduuid allowed",
6051 param->fp_obd_uuid = (struct obd_uuid *)optarg;
6054 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6055 param->fp_verbose |= VERBOSE_POOL;
6056 param->fp_max_depth = 0;
6063 param->fp_recursive = 1;
6069 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6070 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6071 param->fp_max_depth = 0;
6075 param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6081 if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6082 param->fp_verbose |= VERBOSE_EXT_SIZE;
6083 param->fp_max_depth = 0;
6087 fprintf(stderr, "%s: unrecognized option '%s'\n",
6088 progname, argv[optind - 1]);
6094 if (pathstart == -1) {
6095 fprintf(stderr, "error: %s: no filename|pathname\n",
6098 } else if (pathend == -1) {
6106 if (param->fp_recursive)
6107 param->fp_max_depth = -1;
6108 else if (param->fp_verbose & VERBOSE_DETAIL)
6109 param->fp_max_depth = 1;
6111 if (!param->fp_verbose)
6112 param->fp_verbose = VERBOSE_DEFAULT;
6113 if (param->fp_quiet)
6114 param->fp_verbose = VERBOSE_OBJID;
6117 rc = llapi_getstripe(argv[pathstart], param);
6118 } while (++pathstart < pathend && !rc);
6121 fprintf(stderr, "error: %s failed for %s.\n",
6122 argv[0], argv[optind - 1]);
6126 static int lfs_tgts(int argc, char **argv)
6128 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6129 struct find_param param;
6130 int index = 0, rc = 0;
6135 if (argc == 2 && !realpath(argv[1], path)) {
6137 fprintf(stderr, "error: invalid path '%s': %s\n",
6138 argv[1], strerror(-rc));
6142 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6143 /* Check if we have a mount point */
6144 if (mntdir[0] == '\0')
6147 memset(¶m, 0, sizeof(param));
6148 if (!strcmp(argv[0], "mdts"))
6149 param.fp_get_lmv = 1;
6151 rc = llapi_ostlist(mntdir, ¶m);
6153 fprintf(stderr, "error: %s: failed on %s\n",
6156 if (path[0] != '\0')
6158 memset(mntdir, 0, PATH_MAX);
6164 static int lfs_getstripe(int argc, char **argv)
6166 struct find_param param = { 0 };
6168 param.fp_max_depth = 1;
6169 return lfs_getstripe_internal(argc, argv, ¶m);
6173 static int lfs_getdirstripe(int argc, char **argv)
6175 struct find_param param = { 0 };
6176 struct option long_opts[] = {
6177 { .val = 'c', .name = "mdt-count", .has_arg = no_argument },
6178 { .val = 'D', .name = "default", .has_arg = no_argument },
6179 { .val = 'h', .name = "help", .has_arg = no_argument },
6180 { .val = 'H', .name = "mdt-hash", .has_arg = no_argument },
6181 { .val = 'i', .name = "mdt-index", .has_arg = no_argument },
6182 { .val = 'm', .name = "mdt-index", .has_arg = no_argument },
6183 { .val = 'O', .name = "obd", .has_arg = required_argument },
6184 { .val = 'r', .name = "recursive", .has_arg = no_argument },
6185 { .val = 'T', .name = "mdt-count", .has_arg = no_argument },
6186 { .val = 'v', .name = "verbose", .has_arg = no_argument },
6187 { .val = 'X', .name = "max-inherit", .has_arg = no_argument },
6188 { .val = 'y', .name = "yaml", .has_arg = no_argument },
6189 { .val = LFS_INHERIT_RR_OPT,
6190 .name = "max-inherit-rr", .has_arg = no_argument },
6194 param.fp_get_lmv = 1;
6196 while ((c = getopt_long(argc, argv,
6197 "cDhHimO:rtTvXy", long_opts, NULL)) != -1) {
6201 param.fp_verbose |= VERBOSE_COUNT;
6204 param.fp_get_default_lmv = 1;
6206 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6209 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6213 param.fp_verbose |= VERBOSE_HASH_TYPE;
6215 case 'i': /* fallthrough */
6217 param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6220 if (param.fp_obd_uuid) {
6222 "%s: only one obduuid allowed",
6226 param.fp_obd_uuid = (struct obd_uuid *)optarg;
6229 param.fp_recursive = 1;
6232 param.fp_verbose |= VERBOSE_DETAIL;
6235 param.fp_verbose |= VERBOSE_INHERIT;
6237 case LFS_INHERIT_RR_OPT:
6238 param.fp_verbose |= VERBOSE_INHERIT_RR;
6244 fprintf(stderr, "%s: unrecognized option '%s'\n",
6245 progname, argv[optind - 1]);
6255 if (param.fp_recursive)
6256 param.fp_max_depth = -1;
6258 if (!param.fp_verbose)
6259 param.fp_verbose = VERBOSE_DEFAULT;
6262 rc = llapi_getstripe(argv[optind], ¶m);
6263 } while (++optind < argc && !rc);
6266 fprintf(stderr, "error: %s failed for %s.\n",
6267 argv[0], argv[optind - 1]);
6272 MNTDF_INODES = 0x0001,
6273 MNTDF_COOKED = 0x0002,
6274 MNTDF_LAZY = 0x0004,
6275 MNTDF_VERBOSE = 0x0008,
6276 MNTDF_SHOW = 0x0010,
6277 MNTDF_DECIMAL = 0x0020,
6280 #define COOK(value, base) \
6283 while (value > base) { \
6291 #define CDF "%11llu"
6292 #define HDF "%8.1f%c"
6296 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6298 double avail, used, ratio = 0;
6301 avail = st->os_ffree;
6302 used = st->os_files - st->os_ffree;
6304 avail = st->os_bavail;
6305 used = st->os_blocks - st->os_bfree;
6307 if (avail + used > 0)
6308 ratio = used / (used + avail) * 100;
6310 /* Round up to match df(1) usage percentage */
6311 return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6315 * This is to identify various problem states for "lfs df" if .osn_err = true,
6316 * so only show flags reflecting those states by default. Informational states
6317 * are only shown with "-v" and use lower-case names to distinguish them.
6318 * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6320 static struct obd_statfs_state_names {
6321 enum obd_statfs_state osn_state;
6322 const char osn_name;
6325 { .osn_state = OS_STATFS_DEGRADED, .osn_name = 'D', .osn_err = true },
6326 { .osn_state = OS_STATFS_READONLY, .osn_name = 'R', .osn_err = true },
6327 { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6328 { .osn_state = OS_STATFS_UNUSED1, .osn_name = '?', .osn_err = true },
6329 { .osn_state = OS_STATFS_UNUSED2, .osn_name = '?', .osn_err = true },
6330 { .osn_state = OS_STATFS_ENOSPC, .osn_name = 'S', .osn_err = true },
6331 { .osn_state = OS_STATFS_ENOINO, .osn_name = 'I', .osn_err = true },
6332 { .osn_state = OS_STATFS_SUM, .osn_name = 'a', /* aggregate */ },
6333 { .osn_state = OS_STATFS_NONROT, .osn_name = 'f', /* flash */ },
6336 static int showdf(char *mntdir, struct obd_statfs *stat,
6337 char *uuid, enum mntdf_flags flags,
6338 char *type, int index, int rc)
6340 long long avail, used, total;
6342 char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6343 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6344 char tbuf[3 * sizeof(__u64)];
6345 char ubuf[3 * sizeof(__u64)];
6346 char abuf[3 * sizeof(__u64)];
6347 char rbuf[3 * sizeof(__u64)];
6354 if (flags & MNTDF_INODES) {
6355 avail = stat->os_ffree;
6356 used = stat->os_files - stat->os_ffree;
6357 total = stat->os_files;
6359 int shift = flags & MNTDF_COOKED ? 0 : 10;
6361 avail = (stat->os_bavail * stat->os_bsize) >> shift;
6362 used = ((stat->os_blocks - stat->os_bfree) *
6363 stat->os_bsize) >> shift;
6364 total = (stat->os_blocks * stat->os_bsize) >> shift;
6367 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6369 if (flags & MNTDF_COOKED) {
6370 int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6374 cook_val = (double)total;
6375 i = COOK(cook_val, base);
6377 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6380 snprintf(tbuf, sizeof(tbuf), CDF, total);
6382 cook_val = (double)used;
6383 i = COOK(cook_val, base);
6385 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6388 snprintf(ubuf, sizeof(ubuf), CDF, used);
6390 cook_val = (double)avail;
6391 i = COOK(cook_val, base);
6393 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6396 snprintf(abuf, sizeof(abuf), CDF, avail);
6398 snprintf(tbuf, sizeof(tbuf), CDF, total);
6399 snprintf(ubuf, sizeof(tbuf), CDF, used);
6400 snprintf(abuf, sizeof(tbuf), CDF, avail);
6403 sprintf(rbuf, RDF, ratio);
6404 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6405 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6407 printf("[%s:%d]", type, index);
6409 if (stat->os_state) {
6413 for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6414 if (oss_names[i].osn_state & stat->os_state &&
6415 (oss_names[i].osn_err ||
6416 flags & MNTDF_VERBOSE))
6417 printf("%c", oss_names[i].osn_name);
6424 printf(UUF": inactive device\n", uuid);
6427 printf(UUF": %s\n", uuid, strerror(-rc));
6434 struct ll_stat_type {
6439 #define LL_STATFS_MAX LOV_MAX_STRIPE_COUNT
6441 struct ll_statfs_data {
6443 struct obd_statfs sd_st;
6446 struct ll_statfs_buf {
6448 struct ll_statfs_data sb_buf[LL_STATFS_MAX];
6451 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6452 int ops, struct ll_statfs_buf *lsb)
6454 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6455 struct obd_uuid uuid_buf;
6456 char *poolname = NULL;
6457 struct ll_stat_type types[] = {
6458 { .st_op = LL_STATFS_LMV, .st_name = "MDT" },
6459 { .st_op = LL_STATFS_LOV, .st_name = "OST" },
6460 { .st_name = NULL } };
6461 struct ll_stat_type *tp;
6462 __u64 ost_files = 0;
6463 __u64 ost_ffree = 0;
6471 poolname = strchr(pool, '.');
6473 if (strncmp(fsname, pool, strlen(fsname))) {
6474 fprintf(stderr, "filesystem name incorrect\n");
6482 fd = open(mntdir, O_RDONLY);
6485 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6490 if (flags & MNTDF_SHOW) {
6491 if (flags & MNTDF_INODES)
6492 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6493 "UUID", "Inodes", "IUsed", "IFree",
6494 "IUse%", "Mounted on");
6496 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6498 flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6499 "Used", "Available", "Use%", "Mounted on");
6502 for (tp = types; tp->st_name != NULL; tp++) {
6503 bool have_ost = false;
6505 if (!(tp->st_op & ops))
6508 for (index = 0; ; index++) {
6509 memset(&stat_buf, 0, sizeof(struct obd_statfs));
6510 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6511 type = flags & MNTDF_LAZY ?
6512 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6513 rc2 = llapi_obd_fstatfs(fd, type, index,
6514 &stat_buf, &uuid_buf);
6519 if (rc2 == -ENODATA) { /* Inactive device, OK. */
6520 if (!(flags & MNTDF_VERBOSE))
6522 } else if (rc2 < 0 && rc == 0) {
6527 * If we have OSTs then don't report MDT block counts.
6528 * For MDT-only filesystems the expectation is that all
6529 * layouts have a DoM component. For filesystems with
6530 * OSTs, files are not necessarily going to store data
6531 * on MDTs, and MDT space is limited to a fraction of
6532 * OST space, so don't include it in the summary.
6534 if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6541 if (poolname && tp->st_op == LL_STATFS_LOV &&
6542 llapi_search_ost(fsname, poolname,
6543 obd_uuid2str(&uuid_buf)) != 1)
6547 * the llapi_obd_fstatfs() call may have returned with
6548 * an error, but if it filled in uuid_buf we will at
6549 * lease use that to print out a message for that OBD.
6550 * If we didn't get anything in the uuid_buf, then fill
6551 * it in so that we can print an error message.
6553 if (uuid_buf.uuid[0] == '\0')
6554 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6555 "%s%04x", tp->st_name, index);
6557 lsb->sb_buf[lsb->sb_count].sd_index = index;
6558 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6561 if (flags & MNTDF_SHOW)
6562 showdf(mntdir, &stat_buf,
6563 obd_uuid2str(&uuid_buf), flags,
6564 tp->st_name, index, rc2);
6569 if (tp->st_op == LL_STATFS_LMV) {
6570 sum.os_ffree += stat_buf.os_ffree;
6571 sum.os_files += stat_buf.os_files;
6572 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6573 ost_files += stat_buf.os_files;
6574 ost_ffree += stat_buf.os_ffree;
6576 sum.os_blocks += stat_buf.os_blocks *
6578 sum.os_bfree += stat_buf.os_bfree *
6580 sum.os_bavail += stat_buf.os_bavail *
6588 * If we have _some_ OSTs, but don't have as many free objects on the
6589 * OST as inodes on the MDTs, reduce the reported number of inodes
6590 * to compensate, so that the "inodes in use" number is correct.
6591 * This should be kept in sync with ll_statfs_internal().
6593 if (ost_files && ost_ffree < sum.os_ffree) {
6594 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6595 sum.os_ffree = ost_ffree;
6597 if (flags & MNTDF_SHOW) {
6599 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6607 LAYOUT_INHERIT_UNSET = -2,
6611 static int lfs_setdirstripe(int argc, char **argv)
6614 struct lfs_setstripe_args lsa = { 0 };
6615 struct llapi_stripe_param *param = NULL;
6616 __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6619 char *mode_opt = NULL;
6620 bool default_stripe = false;
6621 bool delete = false;
6622 bool foreign_mode = false;
6623 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6624 mode_t previous_mode = 0;
6626 __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6627 int max_inherit = LAYOUT_INHERIT_UNSET;
6628 int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6629 struct option long_opts[] = {
6630 { .val = 'c', .name = "count", .has_arg = required_argument },
6631 { .val = 'c', .name = "mdt-count", .has_arg = required_argument },
6632 { .val = 'd', .name = "delete", .has_arg = no_argument },
6633 { .val = 'D', .name = "default", .has_arg = no_argument },
6634 { .val = 'D', .name = "default_stripe", .has_arg = no_argument },
6635 { .val = LFS_LAYOUT_FLAGS_OPT,
6636 .name = "flags", .has_arg = required_argument },
6637 { .val = LFS_LAYOUT_FOREIGN_OPT,
6638 .name = "foreign", .has_arg = optional_argument},
6639 { .val = 'h', .name = "help", .has_arg = no_argument },
6640 { .val = 'H', .name = "mdt-hash", .has_arg = required_argument },
6641 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6642 { .val = 'i', .name = "mdt-index", .has_arg = required_argument },
6643 { .val = 'i', .name = "mdt", .has_arg = required_argument },
6645 /* find { .val = 'l', .name = "lazy", .has_arg = no_argument }, */
6646 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
6647 { .val = 'm', .name = "mdt", .has_arg = required_argument },
6649 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6650 { .val = 'i', .name = "index", .has_arg = required_argument },
6652 { .val = 'o', .name = "mode", .has_arg = required_argument },
6653 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6654 { .val = 't', .name = "hash-type", .has_arg = required_argument },
6656 { .val = 'T', .name = "mdt-count", .has_arg = required_argument },
6657 { .val = 'x', .name = "xattr", .has_arg = required_argument },
6658 { .val = 'X', .name = "max-inherit", .has_arg = required_argument },
6659 { .val = LFS_INHERIT_RR_OPT,
6660 .name = "max-inherit-rr", .has_arg = required_argument},
6661 /* setstripe { .val = 'y', .name = "yaml", .has_arg = no_argument }, */
6665 setstripe_args_init(&lsa);
6667 while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
6668 long_opts, NULL)) >= 0) {
6676 lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6677 if (errno != 0 || *end != '\0' ||
6678 lsa.lsa_stripe_count < -1 ||
6679 lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6681 "%s: invalid stripe count '%s'\n",
6688 default_stripe = true;
6691 default_stripe = true;
6693 case LFS_LAYOUT_FOREIGN_OPT:
6695 /* check pure numeric */
6696 type = strtoul(optarg, &end, 0);
6699 type = check_foreign_type_name(optarg);
6700 if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6702 "%s %s: unknown foreign type '%s'\n",
6707 } else if (type >= UINT32_MAX) {
6709 "%s %s: invalid foreign type '%s'\n",
6710 progname, argv[0], optarg);
6714 foreign_mode = true;
6716 case LFS_LAYOUT_FLAGS_OPT:
6718 flags = strtoul(optarg, &end, 16);
6719 if (errno != 0 || *end != '\0' ||
6720 flags >= UINT32_MAX) {
6722 "%s %s: invalid hex flags '%s'\n",
6723 progname, argv[0], optarg);
6726 if (!foreign_mode) {
6728 "%s %s: hex flags must be specified with --foreign option\n",
6733 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6736 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6740 lsa.lsa_pattern = check_hashtype(optarg);
6741 if (lsa.lsa_pattern == 0) {
6743 "%s %s: bad stripe hash type '%s'\n",
6744 progname, argv[0], optarg);
6749 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6752 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6753 if (strcmp(argv[optind - 1], "--index") == 0)
6755 "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6758 lsa.lsa_nr_tgts = parse_targets(mdts,
6759 sizeof(mdts) / sizeof(__u32),
6760 lsa.lsa_nr_tgts, optarg, NULL);
6761 if (lsa.lsa_nr_tgts < 0) {
6763 "%s %s: invalid MDT target(s) '%s'\n",
6764 progname, argv[0], optarg);
6768 lsa.lsa_tgts = mdts;
6769 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6770 lsa.lsa_stripe_off = mdts[0];
6772 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6775 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6784 if (!default_stripe) {
6786 "%s %s: '--max-inherit' must be specified with '-D'\n",
6791 max_inherit = strtol(optarg, &end, 10);
6792 if (errno != 0 || *end != '\0' || max_inherit < -2) {
6794 "%s %s: invalid max-inherit '%s'\n",
6795 progname, argv[0], optarg);
6798 if (max_inherit == 0) {
6799 max_inherit = LMV_INHERIT_NONE;
6800 } else if (max_inherit == -1) {
6801 max_inherit = LMV_INHERIT_UNLIMITED;
6802 } else if (max_inherit > LMV_INHERIT_MAX) {
6804 "%s %s: max-inherit %d exceeds maximum %u\n",
6805 progname, argv[0], max_inherit,
6810 case LFS_INHERIT_RR_OPT:
6811 if (!default_stripe) {
6813 "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6818 max_inherit_rr = strtol(optarg, &end, 10);
6819 if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6821 "%s %s: invalid max-inherit-rr '%s'\n",
6822 progname, argv[0], optarg);
6825 if (max_inherit_rr == 0) {
6826 max_inherit_rr = LMV_INHERIT_RR_NONE;
6827 } else if (max_inherit_rr == -1) {
6828 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6829 } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6831 "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6832 progname, argv[0], max_inherit_rr,
6833 LMV_INHERIT_RR_MAX);
6838 fprintf(stderr, "%s: unrecognized option '%s'\n",
6839 progname, argv[optind - 1]);
6846 if (optind == argc) {
6847 fprintf(stderr, "%s %s: DIR must be specified\n",
6852 if (xattr && !foreign_mode) {
6854 * only print a warning as this is armless and will be
6858 "%s %s: xattr has been specified for non-foreign layout\n",
6860 } else if (foreign_mode && !xattr) {
6862 "%s %s: xattr must be provided in foreign mode\n",
6867 if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6868 lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6870 "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6875 if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6876 lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6877 /* if no parameters set, create directory on least-used MDTs */
6878 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
6879 lsa.lsa_stripe_count = 1;
6883 (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6884 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6886 "%s %s: cannot specify -d with -c or -i options\n",
6892 mode = strtoul(mode_opt, &end, 8);
6895 "%s %s: bad MODE '%s'\n",
6896 progname, argv[0], mode_opt);
6899 previous_mode = umask(0);
6902 if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6903 lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6904 lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
6906 "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
6907 progname, argv[0], lsa.lsa_stripe_off);
6911 /* foreign LMV/dir case */
6913 if (argc > optind + 1) {
6915 "%s %s: cannot specify multiple foreign dirs\n",
6920 dname = argv[optind];
6921 result = llapi_dir_create_foreign(dname, mode, type, flags,
6925 "%s mkdir: can't create foreign dir '%s': %s\n",
6926 progname, dname, strerror(-result));
6931 * initialize stripe parameters, in case param is converted to specific,
6932 * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6934 param = calloc(1, offsetof(typeof(*param),
6935 lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6936 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6939 "%s %s: cannot allocate memory for parameters: %s\n",
6940 progname, argv[0], strerror(ENOMEM));
6944 /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6945 if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
6946 (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6947 lsa.lsa_stripe_count == 0))
6948 lsa.lsa_stripe_count = 1;
6949 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6950 param->lsp_stripe_count = lsa.lsa_stripe_count;
6951 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6952 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
6954 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6955 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6956 param->lsp_stripe_pattern = lsa.lsa_pattern;
6958 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6959 param->lsp_pool = lsa.lsa_pool_name;
6960 param->lsp_is_specific = false;
6961 if (default_stripe) {
6962 if (max_inherit == LAYOUT_INHERIT_UNSET)
6963 max_inherit = LMV_INHERIT_DEFAULT;
6964 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
6965 max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
6966 param->lsp_max_inherit = max_inherit;
6967 param->lsp_max_inherit_rr = max_inherit_rr;
6969 if (strcmp(argv[0], "mkdir") == 0)
6970 param->lsp_is_create = true;
6971 if (lsa.lsa_nr_tgts > 1) {
6972 if (lsa.lsa_stripe_count > 0 &&
6973 lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6974 lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6976 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6977 argv[0], lsa.lsa_stripe_count,
6983 param->lsp_is_specific = true;
6984 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6985 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6988 dname = argv[optind];
6990 if (default_stripe) {
6991 result = llapi_dir_set_default_lmv(dname, param);
6994 "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
6995 progname, dname, strerror(-result));
6999 result = llapi_dir_create(dname, mode, param);
7002 "%s setdirstripe: cannot create dir '%s': %s\n",
7003 progname, dname, strerror(-result));
7004 } while (!result && (dname = argv[++optind]));
7007 umask(previous_mode);
7014 static int lfs_rmentry(int argc, char **argv)
7021 fprintf(stderr, "error: %s: missing dirname\n",
7027 dname = argv[index];
7029 result = llapi_direntry_remove(dname);
7032 "error: %s: remove dir entry '%s' failed\n",
7036 dname = argv[++index];
7041 static int lfs_unlink_foreign(int argc, char **argv)
7048 fprintf(stderr, "error: %s: missing pathname\n",
7055 while (name != NULL) {
7056 result = llapi_unlink_foreign(name);
7059 "error: %s: unlink foreign entry '%s' failed\n",
7063 name = argv[++index];
7068 static int lfs_mv(int argc, char **argv)
7070 struct lmv_user_md lmu = { LMV_USER_MAGIC };
7071 struct find_param param = {
7078 struct option long_opts[] = {
7079 { .val = 'm', .name = "mdt", .has_arg = required_argument },
7080 { .val = 'm', .name = "mdt-index", .has_arg = required_argument },
7081 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7084 while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7086 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7089 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7093 lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7094 if (errno != 0 || *end != '\0' ||
7095 lmu.lum_stripe_offset >= UINT32_MAX) {
7096 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7102 param.fp_verbose = VERBOSE_DETAIL;
7105 fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7106 progname, argv[optind - 1]);
7111 if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7112 fprintf(stderr, "%s mv: MDT index must be specified\n",
7117 if (optind >= argc) {
7118 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7122 lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7124 /* initialize migrate mdt parameters */
7125 param.fp_lmv_md = &lmu;
7126 param.fp_migrate = 1;
7127 rc = llapi_migrate_mdt(argv[optind], ¶m);
7129 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7130 progname, argv[optind], lmu.lum_stripe_offset,
7135 static int lfs_osts(int argc, char **argv)
7137 return lfs_tgts(argc, argv);
7140 static int lfs_mdts(int argc, char **argv)
7142 return lfs_tgts(argc, argv);
7145 static int lfs_df(int argc, char **argv)
7147 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7148 enum mntdf_flags flags = MNTDF_SHOW;
7149 int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7150 int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7151 char fsname[PATH_MAX] = "", *pool_name = NULL;
7152 struct option long_opts[] = {
7153 { .val = 'h', .name = "human-readable", .has_arg = no_argument },
7154 { .val = 'H', .name = "si", .has_arg = no_argument },
7155 { .val = 'i', .name = "inodes", .has_arg = no_argument },
7156 { .val = 'l', .name = "lazy", .has_arg = no_argument },
7157 { .val = 'p', .name = "pool", .has_arg = required_argument },
7158 { .val = 'v', .name = "verbose", .has_arg = no_argument },
7161 while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7164 flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7167 flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7170 flags |= MNTDF_INODES;
7173 flags |= MNTDF_LAZY;
7179 flags |= MNTDF_VERBOSE;
7182 fprintf(stderr, "%s: unrecognized option '%s'\n",
7183 progname, argv[optind - 1]);
7188 /* Handle case where path is not specified */
7189 if (optind == argc) {
7190 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7191 /* Check if we have a mount point */
7192 if (mntdir[0] == '\0')
7195 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7196 if (rc || path[0] != '\0')
7199 fsname[0] = '\0'; /* avoid matching in next loop */
7200 mntdir[0] = '\0'; /* avoid matching in next loop */
7201 path[0] = '\0'; /* clean for next loop */
7206 /* Loop through all the remaining arguments. These are Lustre FS
7209 for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7212 fsname[0] = '\0'; /* start clean */
7213 mntdir[0] = '\0'; /* start clean */
7214 path[0] = '\0'; /* start clean */
7216 /* path does not exists at all */
7217 if (!realpath(argv[arg_idx], path)) {
7219 fprintf(stderr, "error: invalid path '%s': %s\n",
7220 argv[arg_idx], strerror(-rc));
7221 /* save first seen error */
7228 /* path exists but may not be a Lustre filesystem */
7229 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7230 /* Check if we have a mount point */
7231 if (mntdir[0] == '\0')
7234 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7235 if (rc || path[0] != '\0') {
7238 /* save first seen error */
7246 llapi_printf(LLAPI_MSG_ERROR,
7247 "%s:%s Not a Lustre filesystem\n",
7248 argv[0], argv[arg_idx]);
7249 /* save first seen error */
7258 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7259 bool opt_instance, bool opt_fsname, bool opt_mntdir)
7263 if (opt_fsname == opt_instance) { /* both true or both false */
7264 rc = llapi_getname(mntdir, buf, buflen);
7265 } else if (opt_fsname) {
7267 * llapi_search_mounts() fills @buf with fsname, but that is not
7268 * called if explicit paths are specified on the command-line
7271 rc = llapi_get_fsname(mntdir, buf, buflen);
7272 } else /* if (opt_instance) */ {
7273 rc = llapi_get_instance(mntdir, buf, buflen);
7277 fprintf(stderr, "cannot get instance for '%s': %s\n",
7278 mntdir, strerror(-rc));
7283 printf("%s %s\n", buf, mntdir);
7285 printf("%s\n", buf);
7290 static int lfs_getname(int argc, char **argv)
7292 struct option long_opts[] = {
7293 { .val = 'h', .name = "help", .has_arg = no_argument },
7294 { .val = 'i', .name = "instance", .has_arg = no_argument },
7295 { .val = 'n', .name = "fsname", .has_arg = no_argument },
7297 bool opt_instance = false, opt_fsname = false;
7298 char fsname[PATH_MAX] = "";
7301 while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7304 opt_instance = true;
7310 fprintf(stderr, "%s: unrecognized option '%s'\n",
7311 progname, argv[optind - 1]);
7318 if (optind == argc) { /* no paths specified, get all paths. */
7319 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7322 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7323 rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7324 opt_instance, opt_fsname, true);
7327 path[0] = fsname[0] = mntdir[0] = '\0';
7329 } else { /* paths specified, only attempt to search these. */
7332 /* if only one path is given, print only requested info */
7333 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7335 for (; optind < argc; optind++) {
7336 rc2 = print_instance(argv[optind], fsname,
7337 sizeof(fsname), opt_instance,
7338 opt_fsname, opt_mntdir);
7348 static int lfs_check(int argc, char **argv)
7350 char mntdir[PATH_MAX] = {'\0'};
7359 fprintf(stderr, "%s check: server type must be specified\n",
7364 obd_types[0] = obd_type1;
7365 obd_types[1] = obd_type2;
7366 obd_types[2] = obd_type3;
7368 if (strcmp(argv[1], "osts") == 0) {
7369 strcpy(obd_types[0], "osc");
7370 } else if (strcmp(argv[1], "mdts") == 0 ||
7371 strcmp(argv[1], "mds") == 0) {
7372 strcpy(obd_types[0], "mdc");
7373 } else if (strcmp(argv[1], "mgts") == 0) {
7374 strcpy(obd_types[0], "mgc");
7375 } else if (strcmp(argv[1], "all") == 0 ||
7376 strcmp(argv[1], "servers") == 0) {
7378 strcpy(obd_types[0], "osc");
7379 strcpy(obd_types[1], "mdc");
7380 strcpy(obd_types[2], "mgc");
7382 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7387 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
7388 if (rc < 0 || mntdir[0] == '\0') {
7390 "%s check: cannot find mounted Lustre filesystem: %s\n",
7391 progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7395 rc = llapi_target_check(num_types, obd_types, mntdir);
7397 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7398 progname, argv[1], strerror(-rc));
7403 #ifdef HAVE_SYS_QUOTA_H
7404 #define ADD_OVERFLOW(a, b) \
7405 ((((a) + (b)) < (a)) ? \
7406 ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7408 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7409 * returns the value or ULONG_MAX on integer overflow or incorrect format
7411 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7412 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7413 * 3. empty integer value is interpreted as 0
7415 static unsigned long str2sec(const char *timestr)
7417 const char spec[] = "smhdw";
7418 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7419 unsigned long val = 0;
7422 if (strpbrk(timestr, spec) == NULL) {
7424 * no specifiers inside the time string,
7425 * should treat it as an integer value
7427 val = strtoul(timestr, &tail, 10);
7428 return *tail ? ULONG_MAX : val;
7431 /* format string is XXwXXdXXhXXmXXs */
7437 v = strtoul(timestr, &tail, 10);
7438 if (v == ULONG_MAX || *tail == '\0')
7440 * value too large (ULONG_MAX or more)
7441 * or missing specifier
7445 ptr = strchr(spec, *tail);
7447 /* unknown specifier */
7452 /* check if product will overflow the type */
7453 if (!(v < ULONG_MAX / mult[ind]))
7456 ADD_OVERFLOW(val, mult[ind] * v);
7457 if (val == ULONG_MAX)
7469 #define ARG2ULL(nr, str, def_units) \
7471 unsigned long long limit, units = def_units; \
7474 rc = llapi_parse_size(str, &limit, &units, 1); \
7476 fprintf(stderr, "%s: invalid limit '%s'\n", \
7483 static inline int has_times_option(int argc, char **argv)
7487 for (i = 1; i < argc; i++)
7488 if (!strcmp(argv[i], "-t"))
7494 static inline int lfs_verify_poolarg(char *pool)
7496 if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7498 "Pool name '%.*s' is longer than %d\n",
7499 LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7505 /* special grace time, only notify the user when its quota is over soft limit
7506 * but doesn't block new writes until the hard limit is reached.
7508 #define NOTIFY_GRACE "notify"
7509 #define NOTIFY_GRACE_TIME LQUOTA_GRACE_MASK
7512 static inline __u64 lustre_stoqb(size_t space)
7514 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7517 #define lustre_stoqb toqb
7520 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7523 char *mnt, *obd_type = (char *)qctl->obd_type;
7524 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7525 struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7526 struct option long_opts[] = {
7527 { .val = 'b', .name = "block-grace", .has_arg = required_argument },
7528 { .val = 'g', .name = "group", .has_arg = no_argument },
7529 { .val = 'h', .name = "help", .has_arg = no_argument },
7530 { .val = 'i', .name = "inode-grace", .has_arg = required_argument },
7531 { .val = 'p', .name = "projid", .has_arg = no_argument },
7532 { .val = 't', .name = "times", .has_arg = no_argument },
7533 { .val = 'u', .name = "user", .has_arg = no_argument },
7534 { .val = LFS_POOL_OPT,
7535 .name = "pool", .has_arg = required_argument },
7539 qctl->qc_cmd = LUSTRE_Q_SETINFO;
7540 qctl->qc_type = ALLQUOTA;
7542 while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7543 long_opts, NULL)) != -1) {
7554 if (qctl->qc_type != ALLQUOTA) {
7556 "%s: -u/g/p cannot be used more than once\n",
7560 qctl->qc_type = qtype;
7563 if (strncmp(optarg, NOTIFY_GRACE,
7564 strlen(NOTIFY_GRACE)) == 0) {
7565 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7567 dqi->dqi_bgrace = str2sec(optarg);
7568 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7570 "%s: bad block-grace: %s\n",
7575 dqb->dqb_valid |= QIF_BTIME;
7578 if (strncmp(optarg, NOTIFY_GRACE,
7579 strlen(NOTIFY_GRACE)) == 0) {
7580 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7582 dqi->dqi_igrace = str2sec(optarg);
7583 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7585 "%s: bad inode-grace: %s\n",
7590 dqb->dqb_valid |= QIF_ITIME;
7592 case 't': /* Yes, of course! */
7595 if (lfs_verify_poolarg(optarg))
7597 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7598 qctl->qc_cmd = LUSTRE_Q_SETINFOPOOL;
7600 /* getopt prints error message for us when opterr != 0 */
7602 fprintf(stderr, "%s: unrecognized option '%s'\n",
7603 progname, argv[optind - 1]);
7610 if (qctl->qc_type == ALLQUOTA) {
7611 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7616 if (optind != argc - 1) {
7617 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7618 progname, argv[optind + 1]);
7623 rc = llapi_quotactl(mnt, qctl);
7626 fprintf(stderr, "%s %s ", obd_type,
7627 obd_uuid2str(&qctl->obd_uuid));
7628 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7635 #define BSLIMIT (1 << 0)
7636 #define BHLIMIT (1 << 1)
7637 #define ISLIMIT (1 << 2)
7638 #define IHLIMIT (1 << 3)
7640 int lfs_setquota(int argc, char **argv)
7643 struct if_quotactl *qctl;
7644 char *mnt, *obd_type;
7645 struct obd_dqblk *dqb;
7646 struct option long_opts[] = {
7647 { .val = 'b', .name = "block-softlimit",
7648 .has_arg = required_argument },
7649 { .val = 'B', .name = "block-hardlimit",
7650 .has_arg = required_argument },
7651 { .val = 'd', .name = "default", .has_arg = no_argument },
7652 { .val = 'g', .name = "group", .has_arg = required_argument },
7653 { .val = 'G', .name = "default-grp", .has_arg = no_argument },
7654 { .val = 'h', .name = "help", .has_arg = no_argument },
7655 { .val = 'i', .name = "inode-softlimit",
7656 .has_arg = required_argument },
7657 { .val = 'I', .name = "inode-hardlimit",
7658 .has_arg = required_argument },
7659 { .val = 'p', .name = "projid", .has_arg = required_argument },
7660 { .val = 'P', .name = "default-prj", .has_arg = no_argument },
7661 { .val = 'u', .name = "user", .has_arg = required_argument },
7662 { .val = 'U', .name = "default-usr", .has_arg = no_argument },
7663 { .val = LFS_POOL_OPT,
7664 .name = "pool", .has_arg = required_argument },
7666 unsigned int limit_mask = 0;
7667 bool use_default = false;
7668 int qtype, qctl_len;
7670 qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7671 qctl = malloc(qctl_len);
7675 memset(qctl, 0, qctl_len);
7676 obd_type = (char *)qctl->obd_type;
7677 dqb = &qctl->qc_dqblk;
7679 if (has_times_option(argc, argv)) {
7680 rc = lfs_setquota_times(argc, argv, qctl);
7684 qctl->qc_cmd = LUSTRE_Q_SETQUOTA;
7685 qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7686 * so it can be used as a marker that qc_type
7687 * isn't reinitialized from command line
7690 while ((c = getopt_long(argc, argv, "b:B:dg:Ghi:I:p:Pu:U",
7691 long_opts, NULL)) != -1) {
7694 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7697 goto quota_type_def;
7700 rc = name2uid(&qctl->qc_id, optarg);
7703 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7706 goto quota_type_def;
7709 rc = name2gid(&qctl->qc_id, optarg);
7712 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7715 goto quota_type_def;
7718 rc = name2projid(&qctl->qc_id, optarg);
7721 if (str2quotaid(&qctl->qc_id, optarg)) {
7723 "%s setquota: invalid id '%s'\n",
7730 if (qctl->qc_id == 0) {
7732 "%s setquota: can't set quota for root usr/group/project.\n",
7739 if (qctl->qc_type != ALLQUOTA) {
7741 "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7746 qctl->qc_type = qtype;
7749 qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7753 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7754 dqb->dqb_bsoftlimit >>= 10;
7755 limit_mask |= BSLIMIT;
7756 if (dqb->dqb_bsoftlimit &&
7757 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7759 "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7761 (unsigned long long)dqb->dqb_bsoftlimit,
7765 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7766 dqb->dqb_bhardlimit >>= 10;
7767 limit_mask |= BHLIMIT;
7768 if (dqb->dqb_bhardlimit &&
7769 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7771 "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7772 "See '%s help setquota' or Lustre manual for details\n",
7774 (unsigned long long)dqb->dqb_bhardlimit,
7778 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7779 limit_mask |= ISLIMIT;
7780 if (dqb->dqb_isoftlimit &&
7781 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7783 "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7785 (unsigned long long)dqb->dqb_isoftlimit,
7789 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7790 limit_mask |= IHLIMIT;
7791 if (dqb->dqb_ihardlimit &&
7792 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7794 "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7796 (unsigned long long)dqb->dqb_ihardlimit,
7800 if (lfs_verify_poolarg(optarg)) {
7804 strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7805 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
7806 LUSTRE_Q_SETDEFAULT_POOL :
7807 LUSTRE_Q_SETQUOTAPOOL;
7811 "%s setquota: unrecognized option '%s'\n",
7812 progname, argv[optind - 1]);
7820 if (qctl->qc_type == ALLQUOTA) {
7822 "%s setquota: either -u or -g must be specified\n",
7828 if (!use_default && limit_mask == 0) {
7830 "%s setquota: at least one limit must be specified\n",
7836 if (use_default && limit_mask != 0) {
7838 "%s setquota: limits should not be specified when using default quota\n",
7844 if (use_default && qctl->qc_id == 0) {
7846 "%s setquota: can not set default quota for root user/group/project\n",
7852 if (optind != argc - 1) {
7854 "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7855 progname, argv[optind]);
7863 dqb->dqb_bhardlimit = 0;
7864 dqb->dqb_bsoftlimit = 0;
7865 dqb->dqb_ihardlimit = 0;
7866 dqb->dqb_isoftlimit = 0;
7869 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7870 /* do not set inode limits for Pool Quotas */
7871 if (qctl->qc_cmd == LUSTRE_Q_SETDEFAULT_POOL)
7872 dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
7873 } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7874 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7875 /* sigh, we can't just set blimits/ilimits */
7876 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
7877 .qc_type = qctl->qc_type,
7878 .qc_id = qctl->qc_id};
7880 rc = llapi_quotactl(mnt, &tmp_qctl);
7884 if (!(limit_mask & BHLIMIT))
7885 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7886 if (!(limit_mask & BSLIMIT))
7887 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7888 if (!(limit_mask & IHLIMIT))
7889 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7890 if (!(limit_mask & ISLIMIT))
7891 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7893 /* Keep grace times if we have got no softlimit arguments */
7894 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7895 dqb->dqb_valid |= QIF_BTIME;
7896 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7899 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7900 dqb->dqb_valid |= QIF_ITIME;
7901 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7905 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7906 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7908 rc = llapi_quotactl(mnt, qctl);
7912 "%s setquota: cannot quotactl '%s' '%s': %s\n",
7914 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7917 "%s setquota: quotactl failed: %s\n",
7918 progname, strerror(-rc));
7922 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7928 /* Converts seconds value into format string
7929 * result is returned in buf
7931 * 1. result is in descenting order: 1w2d3h4m5s
7932 * 2. zero fields are not filled (except for p. 3): 5d1s
7933 * 3. zero seconds value is presented as "0s"
7935 static char *__sec2str(time_t seconds, char *buf)
7937 const char spec[] = "smhdw";
7938 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7943 for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7944 c = seconds / mult[i];
7946 if (c > 0 || (i == 0 && buf == tail))
7947 tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7956 static void sec2str(time_t seconds, char *buf, int rc)
7963 tail = __sec2str(seconds, tail);
7965 if (rc && tail - buf < 39) {
7971 static void diff2str(time_t seconds, char *buf, time_t now)
7976 if (seconds <= now) {
7977 strcpy(buf, "none");
7980 __sec2str(seconds - now, buf);
7983 static void print_quota_title(char *name, struct if_quotactl *qctl,
7984 bool human_readable, bool show_default)
7987 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7988 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7989 "Filesystem", "bquota", "blimit", "bgrace",
7990 "iquota", "ilimit", "igrace");
7992 printf("Disk quotas for %s %s (%cid %u):\n",
7993 qtype_name(qctl->qc_type), name,
7994 *qtype_name(qctl->qc_type), qctl->qc_id);
7995 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7996 "Filesystem", human_readable ? "used" : "kbytes",
7997 "quota", "limit", "grace",
7998 "files", "quota", "limit", "grace");
8002 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8005 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8008 snprintf(buf, buflen, "%5.4gP",
8009 (double)num / ((__u64)1 << 40));
8011 snprintf(buf, buflen, "%5.4gT",
8012 (double)num / (1 << 30));
8014 snprintf(buf, buflen, "%5.4gG",
8015 (double)num / (1 << 20));
8017 snprintf(buf, buflen, "%5.4gM",
8018 (double)num / (1 << 10));
8020 snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8024 #ifdef HAVE_NATIVE_CLIENT
8025 /* In the current Lustre implementation, the grace time is either the time
8026 * or the timestamp to be used after some quota ID exceeds the soft limt,
8027 * 48 bits should be enough, its high 16 bits can be used as quota flags.
8029 #define LQUOTA_GRACE_BITS 48
8030 #define LQUOTA_GRACE_MASK ((1ULL << LQUOTA_GRACE_BITS) - 1)
8031 #define LQUOTA_GRACE_MAX LQUOTA_GRACE_MASK
8032 #define LQUOTA_GRACE(t) (t & LQUOTA_GRACE_MASK)
8033 #define LQUOTA_FLAG(t) (t >> LQUOTA_GRACE_BITS)
8034 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8037 #define STRBUF_LEN 24
8038 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8039 int rc, bool h, bool show_default)
8045 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8046 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8047 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8048 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8049 int bover = 0, iover = 0;
8050 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8051 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8053 char strbuf[STRBUF_LEN];
8055 if (dqb->dqb_bhardlimit &&
8056 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8058 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8059 if (dqb->dqb_btime > now)
8065 if (dqb->dqb_ihardlimit &&
8066 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8068 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8069 if (dqb->dqb_itime > now)
8075 if (strlen(mnt) > 15)
8076 printf("%s\n%15s", mnt, "");
8078 printf("%15s", mnt);
8081 diff2str(dqb->dqb_btime, timebuf, now);
8082 else if (show_default)
8083 snprintf(timebuf, sizeof(timebuf), "%llu",
8084 (unsigned long long)dqb->dqb_btime);
8086 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8087 strbuf, sizeof(strbuf), h);
8088 if (rc == -EREMOTEIO)
8089 sprintf(numbuf[0], "%s*", strbuf);
8091 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8092 "%s" : "[%s]", strbuf);
8094 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8095 if (type == QC_GENERAL)
8096 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8097 "%s" : "[%s]", strbuf);
8099 sprintf(numbuf[1], "%s", "-");
8101 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8102 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8103 "%s" : "[%s]", strbuf);
8106 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8108 printf(" %7s%c %6s %7s %7s",
8109 numbuf[0], bover ? '*' : ' ', numbuf[1],
8110 numbuf[2], bover > 1 ? timebuf : "-");
8113 diff2str(dqb->dqb_itime, timebuf, now);
8114 else if (show_default)
8115 snprintf(timebuf, sizeof(timebuf), "%llu",
8116 (unsigned long long)dqb->dqb_itime);
8118 snprintf(numbuf[0], sizeof(numbuf),
8119 (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8120 (uintmax_t)dqb->dqb_curinodes);
8122 if (type == QC_GENERAL)
8123 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8125 (uintmax_t)dqb->dqb_isoftlimit);
8127 sprintf(numbuf[1], "%s", "-");
8129 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8130 "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8133 printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8134 else if (type != QC_OSTIDX)
8135 printf(" %7s%c %6s %7s %7s",
8136 numbuf[0], iover ? '*' : ' ', numbuf[1],
8137 numbuf[2], iover > 1 ? timebuf : "-");
8139 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8141 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8142 qctl->qc_cmd == Q_GETOINFO) {
8146 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8147 strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8149 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8150 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8151 strncpy(igtimebuf, NOTIFY_GRACE, 40);
8153 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8155 printf("Block grace time: %s; Inode grace time: %s\n",
8156 bgtimebuf, igtimebuf);
8160 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8164 /* format is "lustre-OST0001" */
8165 dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8167 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8172 *idx = strtoul(dash, &endp, 16);
8173 if (*idx > 0xffff) {
8174 fprintf(stderr, "wrong index %s\n", tgtname);
8181 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8182 bool h, __u64 *total)
8184 int rc = 0, rc1 = 0, count = 0, i = 0;
8185 char **list = NULL, *buffer = NULL;
8186 __u32 valid = qctl->qc_valid;
8188 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8191 /* Is it correct for the case OST0000, OST0002, OST0003 -
8192 * we will ask OST0001 that is absent and won't ask OST0003? */
8193 rc = llapi_get_obd_count(mnt, &count, is_mdt);
8195 fprintf(stderr, "can not get %s count: %s\n",
8196 is_mdt ? "mdt" : "ost", strerror(-rc));
8200 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8201 char fname[PATH_MAX];
8202 char fsname[LUSTRE_MAXFSNAME + 1];
8203 int bufsize = sizeof(struct obd_uuid) * count;
8205 rc = llapi_search_fsname(mnt, fsname);
8207 fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8211 buffer = malloc(bufsize + sizeof(*list) * count);
8214 list = (char **)(buffer + bufsize);
8215 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8216 count = llapi_get_poolmembers(fname, list, count,
8222 for (i = 0; i < count; i++) {
8223 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8226 if (tgt_name2index(list[i], &index))
8228 qctl->qc_idx = index;
8233 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8234 rc = llapi_quotactl(mnt, qctl);
8236 /* It is remote client case. */
8237 if (rc == -EOPNOTSUPP) {
8244 fprintf(stderr, "quotactl %s%d failed.\n",
8245 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8249 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8250 qctl->qc_valid, 0, h, false);
8251 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8252 qctl->qc_dqblk.dqb_bhardlimit;
8257 qctl->qc_valid = valid;
8261 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8262 int verbose, int quiet, bool human_readable,
8265 int rc1 = 0, rc2 = 0, rc3 = 0;
8266 char *obd_type = (char *)qctl->obd_type;
8267 char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8268 __u64 total_ialloc = 0, total_balloc = 0;
8269 bool use_default_for_blk = false;
8270 bool use_default_for_file = false;
8273 rc1 = llapi_quotactl(mnt, qctl);
8277 fprintf(stderr, "%s quotas are not enabled.\n",
8278 qtype_name(qctl->qc_type));
8281 fprintf(stderr, "Permission denied.\n");
8284 /* We already got error message. */
8287 fprintf(stderr, "Unexpected quotactl error: %s\n",
8292 if (!show_default && qctl->qc_id == 0) {
8293 qctl->qc_dqblk.dqb_bhardlimit = 0;
8294 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8295 qctl->qc_dqblk.dqb_ihardlimit = 0;
8296 qctl->qc_dqblk.dqb_isoftlimit = 0;
8297 qctl->qc_dqblk.dqb_btime = 0;
8298 qctl->qc_dqblk.dqb_itime = 0;
8299 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8302 if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8303 LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8304 use_default_for_blk = true;
8305 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8308 if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8309 LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8310 use_default_for_file = true;
8311 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8314 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8315 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8316 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8317 qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8318 print_quota_title(name, qctl, human_readable, show_default);
8320 if (rc1 && *obd_type)
8321 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8323 if (qctl->qc_valid != QC_GENERAL)
8326 inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8327 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8328 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8329 (QIF_LIMITS|QIF_USAGE));
8331 print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8333 if (!show_default && verbose &&
8334 qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8335 qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8336 char strbuf[STRBUF_LEN];
8338 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8340 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8342 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8344 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8345 (uintmax_t)total_ialloc, strbuf);
8348 if (use_default_for_blk)
8349 printf("%cid %u is using default block quota setting\n",
8350 *qtype_name(qctl->qc_type), qctl->qc_id);
8352 if (use_default_for_file)
8353 printf("%cid %u is using default file quota setting\n",
8354 *qtype_name(qctl->qc_type), qctl->qc_id);
8356 if (rc1 || rc2 || rc3 || inacc)
8357 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8371 static int lfs_project(int argc, char **argv)
8373 int ret = 0, err = 0, c, i;
8374 struct project_handle_control phc = { 0 };
8375 enum lfs_project_ops_t op;
8378 phc.assign_projid = false;
8379 /* default action */
8380 op = LFS_PROJECT_LIST;
8382 while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8385 if (op != LFS_PROJECT_LIST) {
8387 "%s: cannot specify '-c' '-C' '-s' together\n",
8392 op = LFS_PROJECT_CHECK;
8395 if (op != LFS_PROJECT_LIST) {
8397 "%s: cannot specify '-c' '-C' '-s' together\n",
8402 op = LFS_PROJECT_CLEAR;
8405 if (op != LFS_PROJECT_LIST) {
8407 "%s: cannot specify '-c' '-C' '-s' together\n",
8412 phc.set_inherit = true;
8413 op = LFS_PROJECT_SET;
8419 phc.keep_projid = true;
8422 phc.recursive = true;
8425 if (str2quotaid(&phc.projid, optarg)) {
8427 "Invalid project ID: %s\n",
8432 phc.assign_projid = true;
8436 phc.newline = false;
8439 fprintf(stderr, "%s: invalid option '%c'\n",
8445 if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8446 op = LFS_PROJECT_SET;
8447 phc.set_projid = true;
8448 } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8449 phc.set_projid = true;
8453 case LFS_PROJECT_CHECK:
8454 if (phc.keep_projid) {
8456 "%s: '-k' is useless together with '-c'\n",
8461 case LFS_PROJECT_CLEAR:
8464 "%s: '-0' is useless together with '-C'\n",
8468 if (phc.assign_projid) {
8470 "%s: '-p' is useless together with '-C'\n",
8475 case LFS_PROJECT_SET:
8478 "%s: '-0' is useless together with '-s'\n",
8482 if (phc.keep_projid) {
8484 "%s: '-k' is useless together with '-s'\n",
8492 "%s: '-0' is useless for list operations\n",
8502 fprintf(stderr, "%s: missing file or directory target(s)\n",
8507 for (i = 0; i < argc; i++) {
8509 case LFS_PROJECT_CHECK:
8510 err = lfs_project_check(argv[i], &phc);
8512 case LFS_PROJECT_LIST:
8513 err = lfs_project_list(argv[i], &phc);
8515 case LFS_PROJECT_CLEAR:
8516 err = lfs_project_clear(argv[i], &phc);
8518 case LFS_PROJECT_SET:
8519 err = lfs_project_set(argv[i], &phc);
8531 static int lfs_quota(int argc, char **argv)
8534 char *mnt, *name = NULL;
8535 struct if_quotactl *qctl;
8537 int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8538 __u32 valid = QC_GENERAL, idx = 0;
8539 bool human_readable = false;
8540 bool show_default = false;
8542 bool show_pools = false;
8543 struct option long_opts[] = {
8544 { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
8546 char **poollist = NULL;
8550 qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8554 qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8555 qctl->qc_type = ALLQUOTA;
8556 obd_uuid = (char *)qctl->obd_uuid.uuid;
8558 while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8559 long_opts, NULL)) != -1) {
8562 show_default = true;
8567 show_default = true;
8572 show_default = true;
8576 if (qctl->qc_type != ALLQUOTA) {
8578 "%s quota: only one of -u, -g, or -p may be specified\n",
8583 qctl->qc_type = qtype;
8586 qctl->qc_cmd = LUSTRE_Q_GETINFO;
8589 valid = qctl->qc_valid = QC_UUID;
8590 snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8593 valid = qctl->qc_valid = QC_MDTIDX;
8594 idx = qctl->qc_idx = atoi(optarg);
8595 if (idx == 0 && *optarg != '0') {
8597 "%s quota: invalid MDT index '%s'\n",
8604 valid = qctl->qc_valid = QC_OSTIDX;
8605 idx = qctl->qc_idx = atoi(optarg);
8606 if (idx == 0 && *optarg != '0') {
8608 "%s quota: invalid OST index '%s'\n",
8621 human_readable = true;
8624 if ((!optarg) && (argv[optind] != NULL) &&
8625 (argv[optind][0] != '-') &&
8626 (argv[optind][0] != '/')) {
8627 optarg = argv[optind++];
8628 if (lfs_verify_poolarg(optarg)) {
8632 strncpy(qctl->qc_poolname, optarg,
8634 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
8635 qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
8637 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8641 /* optarg is NULL */
8643 qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8646 fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8647 progname, argv[optind - 1]);
8653 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8654 if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8655 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8656 qctl->qc_type == ALLQUOTA &&
8657 optind == argc - 1 && !show_default) {
8660 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8661 qctl->qc_type = qtype;
8662 qctl->qc_valid = valid;
8663 if (qtype == USRQUOTA) {
8664 qctl->qc_id = geteuid();
8665 rc = uid2name(&name, qctl->qc_id);
8667 qctl->qc_id = getegid();
8668 rc = gid2name(&name, qctl->qc_id);
8669 memset(&qctl->qc_dqblk, 0,
8670 sizeof(qctl->qc_dqblk));
8675 rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8676 human_readable, show_default);
8681 /* lfs quota -u username /path/to/lustre/mount */
8682 } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8683 qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8684 /* options should be followed by u/g-name and mntpoint */
8685 if ((!show_default && optind + 2 != argc) ||
8686 (show_default && optind + 1 != argc) ||
8687 qctl->qc_type == ALLQUOTA) {
8689 "%s quota: name and mount point must be specified\n",
8695 if (!show_default) {
8696 name = argv[optind++];
8697 switch (qctl->qc_type) {
8699 rc = name2uid(&qctl->qc_id, name);
8702 rc = name2gid(&qctl->qc_id, name);
8705 rc = name2projid(&qctl->qc_id, name);
8712 qctl->qc_valid = QC_GENERAL;
8713 qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8714 LUSTRE_Q_GETDEFAULT_POOL :
8715 LUSTRE_Q_GETDEFAULT;
8720 if (str2quotaid(&qctl->qc_id, name)) {
8721 fprintf(stderr, "%s quota: invalid id '%s'\n",
8727 } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8728 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8739 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
8743 for (i = 0; i < poolcount; i++) {
8744 p = memchr(poollist[i], '.', MAXNAMLEN);
8746 fprintf(stderr, "bad string format %.*s\n",
8747 MAXNAMLEN, poollist[i]);
8752 printf("Quotas for pool: %s\n", p);
8753 strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
8754 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8755 human_readable, show_default);
8762 rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8763 human_readable, show_default);
8769 #endif /* HAVE_SYS_QUOTA_H! */
8771 static int flushctx_ioctl(char *mp)
8775 fd = open(mp, O_RDONLY);
8777 fprintf(stderr, "flushctx: error open %s: %s\n",
8778 mp, strerror(errno));
8782 rc = ioctl(fd, LL_IOC_FLUSHCTX);
8784 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8785 mp, strerror(errno));
8791 static int lfs_flushctx(int argc, char **argv)
8793 int kdestroy = 0, reap = 0, c;
8794 char mntdir[PATH_MAX] = {'\0'};
8798 while ((c = getopt(argc, argv, "kr")) != -1) {
8808 "error: %s: option '-%c' unrecognized\n",
8815 rc = system("kdestroy > /dev/null");
8817 rc = WEXITSTATUS(rc);
8819 "error destroying tickets: %d, continuing\n",
8824 if (optind >= argc) {
8825 /* flush for all mounted lustre fs. */
8826 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8827 /* Check if we have a mount point */
8828 if (mntdir[0] == '\0')
8831 if (flushctx_ioctl(mntdir))
8834 mntdir[0] = '\0'; /* avoid matching in next loop */
8837 /* flush fs as specified */
8838 while (optind < argc) {
8839 if (flushctx_ioctl(argv[optind++]))
8845 rc = system("keyctl reap > /dev/null");
8847 rc = WEXITSTATUS(rc);
8848 fprintf(stderr, "error reaping keyring: %d\n", rc);
8855 static int lfs_changelog(int argc, char **argv)
8857 void *changelog_priv;
8858 struct changelog_rec *rec;
8859 long long startrec = 0, endrec = 0;
8861 struct option long_opts[] = {
8862 { .val = 'f', .name = "follow", .has_arg = no_argument },
8864 char short_opts[] = "f";
8867 while ((rc = getopt_long(argc, argv, short_opts,
8868 long_opts, NULL)) != -1) {
8875 "%s changelog: unrecognized option '%s'\n",
8876 progname, argv[optind - 1]);
8880 if (optind >= argc) {
8881 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8886 mdd = argv[optind++];
8887 if (argc > optind) {
8889 startrec = strtoll(argv[optind++], NULL, 10);
8890 if (errno != 0 || startrec < 0) {
8892 "%s changelog: bad startrec\n",
8898 if (argc > optind) {
8900 endrec = strtoll(argv[optind++], NULL, 10);
8901 if (errno != 0 || endrec < 0) {
8903 "%s changelog: bad endrec\n",
8909 rc = llapi_changelog_start(&changelog_priv,
8910 CHANGELOG_FLAG_BLOCK |
8911 CHANGELOG_FLAG_JOBID |
8912 CHANGELOG_FLAG_EXTRA_FLAGS |
8913 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8916 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8917 progname, strerror(errno = -rc));
8921 rc = llapi_changelog_set_xflags(changelog_priv,
8922 CHANGELOG_EXTRA_FLAG_UIDGID |
8923 CHANGELOG_EXTRA_FLAG_NID |
8924 CHANGELOG_EXTRA_FLAG_OMODE |
8925 CHANGELOG_EXTRA_FLAG_XATTR);
8928 "%s changelog: cannot set xflags for changelog: %s\n",
8929 progname, strerror(errno = -rc));
8933 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8937 if (endrec && rec->cr_index > endrec) {
8938 llapi_changelog_free(&rec);
8941 if (rec->cr_index < startrec) {
8942 llapi_changelog_free(&rec);
8946 secs = rec->cr_time >> 30;
8947 gmtime_r(&secs, &ts);
8948 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8949 "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8950 changelog_type2str(rec->cr_type),
8951 ts.tm_hour, ts.tm_min, ts.tm_sec,
8952 (int)(rec->cr_time & ((1 << 30) - 1)),
8953 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8954 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8956 if (rec->cr_flags & CLF_JOBID) {
8957 struct changelog_ext_jobid *jid =
8958 changelog_rec_jobid(rec);
8960 if (jid->cr_jobid[0] != '\0')
8961 printf(" j=%s", jid->cr_jobid);
8964 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8965 struct changelog_ext_extra_flags *ef =
8966 changelog_rec_extra_flags(rec);
8968 printf(" ef=0x%llx",
8969 (unsigned long long)ef->cr_extra_flags);
8971 if (ef->cr_extra_flags & CLFE_UIDGID) {
8972 struct changelog_ext_uidgid *uidgid =
8973 changelog_rec_uidgid(rec);
8975 printf(" u=%llu:%llu",
8976 (unsigned long long)uidgid->cr_uid,
8977 (unsigned long long)uidgid->cr_gid);
8979 if (ef->cr_extra_flags & CLFE_NID) {
8980 struct changelog_ext_nid *nid =
8981 changelog_rec_nid(rec);
8984 libcfs_nid2str(nid->cr_nid));
8987 if (ef->cr_extra_flags & CLFE_OPEN) {
8988 struct changelog_ext_openmode *omd =
8989 changelog_rec_openmode(rec);
8990 char mode[] = "---";
8992 /* exec mode must be exclusive */
8993 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8996 if (omd->cr_openflags & MDS_FMODE_READ)
8998 if (omd->cr_openflags &
9005 if (strcmp(mode, "---") != 0)
9006 printf(" m=%s", mode);
9009 if (ef->cr_extra_flags & CLFE_XATTR) {
9010 struct changelog_ext_xattr *xattr =
9011 changelog_rec_xattr(rec);
9013 if (xattr->cr_xattr[0] != '\0')
9014 printf(" x=%s", xattr->cr_xattr);
9018 if (!fid_is_zero(&rec->cr_pfid))
9019 printf(" p="DFID, PFID(&rec->cr_pfid));
9020 if (rec->cr_namelen)
9021 printf(" %.*s", rec->cr_namelen,
9022 changelog_rec_name(rec));
9024 if (rec->cr_flags & CLF_RENAME) {
9025 struct changelog_ext_rename *rnm =
9026 changelog_rec_rename(rec);
9028 if (!fid_is_zero(&rnm->cr_sfid))
9029 printf(" s="DFID" sp="DFID" %.*s",
9030 PFID(&rnm->cr_sfid),
9031 PFID(&rnm->cr_spfid),
9032 (int)changelog_rec_snamelen(rec),
9033 changelog_rec_sname(rec));
9037 llapi_changelog_free(&rec);
9040 llapi_changelog_fini(&changelog_priv);
9043 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9044 progname, strerror(errno = -rc));
9046 return (rc == 1 ? 0 : rc);
9049 static int lfs_changelog_clear(int argc, char **argv)
9058 endrec = strtoll(argv[3], NULL, 10);
9059 if (errno != 0 || endrec < 0) {
9061 "%s: bad endrec '%s'\n",
9066 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9069 fprintf(stderr, "%s: record out of range: %llu\n",
9071 else if (rc == -ENOENT)
9072 fprintf(stderr, "%s: no changelog user: %s\n",
9075 fprintf(stderr, "%s error: %s\n", argv[0],
9084 static void rstripc(char *str, int c)
9086 char *end = str + strlen(str);
9088 for (; str < end && end[-1] == c; --end)
9092 static int lfs_fid2path(int argc, char **argv)
9094 struct option long_opts[] = {
9095 { .val = 'c', .name = "cur", .has_arg = no_argument },
9096 { .val = 'c', .name = "current", .has_arg = no_argument },
9097 { .val = 'c', .name = "print-link", .has_arg = no_argument },
9098 { .val = 'f', .name = "print-fid", .has_arg = no_argument },
9099 { .val = 'l', .name = "link", .has_arg = required_argument },
9101 char short_opts[] = "cfl:pr:";
9102 bool print_link = false;
9103 bool print_fid = false;
9105 char mnt_dir[PATH_MAX] = "";
9107 char *path_or_fsname;
9108 long long recno = -1;
9110 char *endptr = NULL;
9115 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9125 linkno = strtol(optarg, &endptr, 10);
9126 if (errno != 0 || *endptr != '\0' || linkno < 0) {
9128 "%s fid2path: invalid linkno '%s'\n",
9134 /* recno is something to do with changelogs
9135 * that was never implemented. We just pass it
9136 * through for the MDT to ignore.
9139 recno = strtoll(optarg, &endptr, 10);
9140 if (errno != 0 || *endptr != '\0' || recno < 0) {
9142 "%s fid2path: invalid recno '%s'\n",
9149 "%s fid2path: unrecognized option '%s'\n",
9150 progname, argv[optind - 1]);
9155 if (argc - optind < 2) {
9157 "Usage: %s fid2path FSNAME|ROOT FID...\n",
9162 path_or_fsname = argv[optind];
9164 if (*path_or_fsname == '/') {
9165 print_mnt_dir = true;
9166 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9168 print_mnt_dir = false;
9169 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9174 "%s fid2path: cannot resolve mount point for '%s': %s\n",
9175 progname, path_or_fsname, strerror(-rc));
9179 mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9182 "%s fid2path: cannot open mount point for '%s': %s\n",
9183 progname, path_or_fsname, strerror(-rc));
9187 /* Strip trailing slashes from mnt_dir. */
9188 rstripc(mnt_dir + 1, '/');
9190 for (i = optind + 1; i < argc; i++) {
9191 const char *fid_str = argv[i];
9195 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9198 "%s fid2path: invalid FID '%s'\n",
9206 int linktmp = (linkno >= 0) ? linkno : 0;
9208 int oldtmp = linktmp;
9209 long long rectmp = recno;
9210 char path_buf[PATH_MAX];
9212 rc2 = llapi_fid2path_at(mnt_fd, &fid,
9213 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9216 "%s fid2path: cannot find %s %s: %s\n",
9217 progname, path_or_fsname, fid_str,
9225 printf("%s ", fid_str);
9228 printf("%d ", linktmp);
9230 /* You may think this looks wrong or weird (and it is!)
9231 * but we are actually trying to preserve the old quirky
9232 * behaviors (enforced by our old quirky tests!) that
9233 * make lfs so much fun to work on:
9235 * lustre 0x200000007:0x1:0x0 => "/"
9236 * /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9238 * Note that llapi_fid2path() returns "" for the root
9242 print_mnt_dir ? mnt_dir : "",
9243 (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9247 /* specified linkno */
9250 if (oldtmp == linktmp)
9262 static int lfs_path2fid(int argc, char **argv)
9264 struct option long_opts[] = {
9265 { .val = 'p', .name = "parents", .has_arg = no_argument },
9268 const char short_opts[] = "p";
9269 const char *sep = "";
9272 bool show_parents = false;
9274 while ((rc = getopt_long(argc, argv, short_opts,
9275 long_opts, NULL)) != -1) {
9278 show_parents = true;
9282 "%s path2fid: unrecognized option '%s'\n",
9283 progname, argv[optind - 1]);
9288 if (optind > argc - 1) {
9289 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9292 } else if (optind < argc - 1) {
9297 for (path = argv + optind; *path != NULL; path++) {
9300 if (!show_parents) {
9301 err = llapi_path2fid(*path, &fid);
9303 printf("%s%s"DFID"\n",
9304 *sep != '\0' ? *path : "", sep,
9307 char name[NAME_MAX + 1];
9308 unsigned int linkno = 0;
9310 while ((err = llapi_path2parent(*path, linkno, &fid,
9311 name, sizeof(name))) == 0) {
9312 if (*sep != '\0' && linkno == 0)
9313 printf("%s%s", *path, sep);
9315 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9320 /* err == -ENODATA is end-of-loop */
9321 if (linkno > 0 && err == -ENODATA) {
9329 "%s path2fid: cannot get %sfid for '%s': %s\n",
9330 progname, show_parents ? "parent " : "", *path,
9342 #define MAX_ERRNO 4095
9343 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9345 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9349 rc = llapi_rmfid(device, fa);
9351 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9352 progname, strerror(-rc));
9356 for (k = 0; k < fa->fa_nr; k++) {
9357 rc2 = (__s32)fa->fa_fids[k].f_ver;
9358 if (!IS_ERR_VALUE(rc2))
9364 fa->fa_fids[k].f_ver = 0;
9365 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9366 progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9372 static int lfs_rmfid(int argc, char **argv)
9374 char *fidstr, *device;
9375 int rc = 0, rc2, nr;
9376 struct fid_array *fa;
9378 if (optind > argc - 1) {
9379 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9383 device = argv[optind++];
9386 fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9392 while (optind < argc) {
9395 fidstr = argv[optind++];
9396 while (*fidstr == '[')
9398 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9400 fprintf(stderr, "unrecognized FID: %s\n",
9405 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9406 /* start another batch */
9407 rc2 = lfs_rmfid_and_show_errors(device, fa);
9414 rc2 = lfs_rmfid_and_show_errors(device, fa);
9422 static int lfs_data_version(int argc, char **argv)
9424 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9432 fprintf(stderr, "%s: FILE must be specified\n",
9437 while ((c = getopt(argc, argv, "hnrw")) != -1) {
9440 data_version_flags = 0;
9443 data_version_flags |= LL_DV_RD_FLUSH;
9446 data_version_flags |= LL_DV_WR_FLUSH;
9450 "%s data_version: unrecognized option '%s'\n",
9451 progname, argv[optind - 1]);
9457 if (optind == argc) {
9458 fprintf(stderr, "%s data_version: FILE must be specified\n",
9463 path = argv[optind];
9464 fd = open(path, O_RDONLY);
9467 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9468 progname, path, strerror(-rc));
9472 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9475 "%s data_version: cannot get version for '%s': %s\n",
9476 progname, path, strerror(-rc));
9478 printf("%ju" "\n", (uintmax_t)data_version);
9484 static int lfs_hsm_state(int argc, char **argv)
9489 struct hsm_user_state hus;
9497 rc = llapi_hsm_state_get(path, &hus);
9499 fprintf(stderr, "can't get hsm state for %s: %s\n",
9500 path, strerror(errno = -rc));
9504 /* Display path name and status flags */
9505 printf("%s: (0x%08x)", path, hus.hus_states);
9507 if (hus.hus_states & HS_RELEASED)
9508 printf(" released");
9509 if (hus.hus_states & HS_EXISTS)
9511 if (hus.hus_states & HS_DIRTY)
9513 if (hus.hus_states & HS_ARCHIVED)
9514 printf(" archived");
9515 /* Display user-settable flags */
9516 if (hus.hus_states & HS_NORELEASE)
9517 printf(" never_release");
9518 if (hus.hus_states & HS_NOARCHIVE)
9519 printf(" never_archive");
9520 if (hus.hus_states & HS_LOST)
9521 printf(" lost_from_hsm");
9523 if (hus.hus_archive_id != 0)
9524 printf(", archive_id:%d", hus.hus_archive_id);
9527 } while (++i < argc);
9532 #define LFS_HSM_SET 0
9533 #define LFS_HSM_CLEAR 1
9536 * Generic function to set or clear HSM flags.
9537 * Used by hsm_set and hsm_clear.
9539 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9541 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9543 struct option long_opts[] = {
9544 { .val = 'A', .name = "archived", .has_arg = no_argument },
9545 { .val = 'a', .name = "noarchive", .has_arg = no_argument },
9546 { .val = 'd', .name = "dirty", .has_arg = no_argument },
9547 { .val = 'e', .name = "exists", .has_arg = no_argument },
9548 { .val = 'h', .name = "help", .has_arg = no_argument },
9549 { .val = 'i', .name = "archive-id", .has_arg = required_argument },
9550 { .val = 'l', .name = "lost", .has_arg = no_argument },
9551 { .val = 'r', .name = "norelease", .has_arg = no_argument },
9556 __u32 archive_id = 0;
9562 while ((c = getopt_long(argc, argv, "aAdehi:lr",
9563 long_opts, NULL)) != -1) {
9569 mask |= HS_NOARCHIVE;
9572 mask |= HS_ARCHIVED;
9575 mask |= HS_NORELEASE;
9585 archive_id = strtol(optarg, &end, 10);
9586 if (errno != 0 || *end != '\0' || archive_id < 0) {
9588 "%s: invalid archive_id: '%s'\n",
9594 fprintf(stderr, "%s: unrecognized option '%s'\n",
9595 progname, argv[optind - 1]);
9602 /* User should have specified a flag */
9606 while (optind < argc) {
9607 path = argv[optind];
9609 /* If mode == 0, this means we apply the mask. */
9610 if (mode == LFS_HSM_SET)
9611 rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9613 rc = llapi_hsm_state_set(path, 0, mask, 0);
9616 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9617 path, strerror(errno = -rc));
9626 static int lfs_hsm_action(int argc, char **argv)
9631 struct hsm_current_action hca;
9632 struct hsm_extent he;
9633 enum hsm_user_action hua;
9634 enum hsm_progress_states hps;
9642 rc = llapi_hsm_current_action(path, &hca);
9644 fprintf(stderr, "can't get hsm action for %s: %s\n",
9645 path, strerror(errno = -rc));
9648 he = hca.hca_location;
9649 hua = hca.hca_action;
9650 hps = hca.hca_state;
9652 printf("%s: %s", path, hsm_user_action2name(hua));
9654 /* Skip file without action */
9655 if (hca.hca_action == HUA_NONE) {
9660 printf(" %s ", hsm_progress_state2name(hps));
9662 if ((hps == HPS_RUNNING) &&
9663 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9664 printf("(%llu bytes moved)\n",
9665 (unsigned long long)he.length);
9666 else if ((he.offset + he.length) == LUSTRE_EOF)
9667 printf("(from %llu to EOF)\n",
9668 (unsigned long long)he.offset);
9670 printf("(from %llu to %llu)\n",
9671 (unsigned long long)he.offset,
9672 (unsigned long long)(he.offset + he.length));
9674 } while (++i < argc);
9679 static int lfs_hsm_set(int argc, char **argv)
9681 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9684 static int lfs_hsm_clear(int argc, char **argv)
9686 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9690 * Check file state and return its fid, to be used by lfs_hsm_request().
9692 * \param[in] file Path to file to check
9693 * \param[in,out] fid Pointer to allocated lu_fid struct.
9694 * \param[in,out] last_dev Pointer to last device id used.
9696 * \return 0 on success.
9698 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9704 rc = lstat(file, &st);
9706 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9710 * Checking for regular file as archiving as posix copytool
9711 * rejects archiving files other than regular files
9713 if (!S_ISREG(st.st_mode)) {
9714 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9717 /* A request should be ... */
9718 if (*last_dev != st.st_dev && *last_dev != 0) {
9720 "All files should be on the same filesystem: %s\n",
9724 *last_dev = st.st_dev;
9726 rc = llapi_path2fid(file, fid);
9728 fprintf(stderr, "Cannot read FID of %s: %s\n",
9729 file, strerror(-rc));
9735 /* Fill an HSM HUR item with a given file name.
9737 * If mntpath is set, then the filename is actually a FID, and no
9738 * lookup on the filesystem will be performed.
9740 * \param[in] hur the user request to fill
9741 * \param[in] idx index of the item inside the HUR to fill
9742 * \param[in] mntpath mountpoint of Lustre
9743 * \param[in] fname filename (if mtnpath is NULL)
9744 * or FID (if mntpath is set)
9745 * \param[in] last_dev pointer to last device id used
9747 * \retval 0 on success
9748 * \retval CMD_HELP or a negative errno on error
9750 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9751 const char *mntpath, const char *fname,
9754 struct hsm_user_item *hui = &hur->hur_user_item[idx];
9757 hui->hui_extent.length = -1;
9760 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9762 fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9765 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9769 hur->hur_request.hr_itemcount++;
9774 static int lfs_hsm_request(int argc, char **argv, int action)
9776 struct option long_opts[] = {
9777 { .val = 'a', .name = "archive", .has_arg = required_argument },
9778 { .val = 'D', .name = "data", .has_arg = required_argument },
9779 { .val = 'h', .name = "help", .has_arg = no_argument },
9780 { .val = 'l', .name = "filelist", .has_arg = required_argument },
9781 { .val = 'm', .name = "mntpath", .has_arg = required_argument },
9784 struct hsm_user_request *hur, *oldhur;
9789 char *filelist = NULL;
9790 char fullpath[PATH_MAX];
9791 char *opaque = NULL;
9795 int nbfile_alloc = 0;
9796 char *some_file = NULL;
9797 char *mntpath = NULL;
9803 while ((c = getopt_long(argc, argv, "a:D:hl:m:",
9804 long_opts, NULL)) != -1) {
9813 if (action != HUA_ARCHIVE &&
9814 action != HUA_REMOVE) {
9816 "error: -a is supported only when archiving or removing\n");
9819 archive_id = atoi(optarg);
9824 some_file = strdup(optarg);
9828 fprintf(stderr, "%s: unrecognized option '%s'\n",
9829 progname, argv[optind - 1]);
9836 /* All remaining args are files, so we have at least nbfile */
9837 nbfile = argc - optind;
9839 if ((nbfile == 0) && (!filelist))
9843 opaque_len = strlen(opaque);
9846 * Alloc the request structure with enough place to store all files
9847 * from command line.
9849 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9851 fprintf(stderr, "Cannot create the request: %s\n",
9855 nbfile_alloc = nbfile;
9857 hur->hur_request.hr_action = action;
9858 hur->hur_request.hr_archive_id = archive_id;
9859 hur->hur_request.hr_flags = 0;
9861 /* All remaining args are files, add them */
9862 if (nbfile != 0 && some_file == NULL)
9863 some_file = strdup(argv[optind]);
9865 for (i = 0; i < nbfile; i++) {
9866 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9872 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9874 /* If a filelist was specified, read the filelist from it. */
9876 fp = fopen(filelist, "r");
9878 fprintf(stderr, "Cannot read the file list %s: %s\n",
9879 filelist, strerror(errno));
9884 while ((rc = getline(&line, &len, fp)) != -1) {
9886 * If allocated buffer was too small, get something
9889 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9892 nbfile_alloc = nbfile_alloc * 2 + 1;
9894 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9898 "hsm: cannot allocate the request: %s\n",
9905 size = hur_len(oldhur);
9908 "hsm: cannot allocate %u files + %u bytes data\n",
9909 oldhur->hur_request.hr_itemcount,
9910 oldhur->hur_request.hr_data_len);
9917 memcpy(hur, oldhur, size);
9922 if (line[strlen(line) - 1] == '\n')
9923 line[strlen(line) - 1] = '\0';
9925 rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9926 mntpath, line, &last_dev);
9942 /* If a --data was used, add it to the request */
9943 hur->hur_request.hr_data_len = opaque_len;
9945 memcpy(hur_data(hur), opaque, opaque_len);
9947 /* Send the HSM request */
9948 if (realpath(some_file, fullpath) == NULL) {
9949 fprintf(stderr, "Could not find path '%s': %s\n",
9950 some_file, strerror(errno));
9952 rc = llapi_hsm_request(fullpath, hur);
9954 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9955 some_file, strerror(-rc));
9965 static int lfs_hsm_archive(int argc, char **argv)
9967 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9970 static int lfs_hsm_restore(int argc, char **argv)
9972 return lfs_hsm_request(argc, argv, HUA_RESTORE);
9975 static int lfs_hsm_release(int argc, char **argv)
9977 return lfs_hsm_request(argc, argv, HUA_RELEASE);
9980 static int lfs_hsm_remove(int argc, char **argv)
9982 return lfs_hsm_request(argc, argv, HUA_REMOVE);
9985 static int lfs_hsm_cancel(int argc, char **argv)
9987 return lfs_hsm_request(argc, argv, HUA_CANCEL);
9990 static int lfs_swap_layouts(int argc, char **argv)
9995 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9996 SWAP_LAYOUTS_KEEP_MTIME |
9997 SWAP_LAYOUTS_KEEP_ATIME);
10000 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
10002 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
10004 int lfs_get_mode(const char *string)
10006 enum lock_mode_user mode;
10008 for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10009 if (lock_mode_names[mode] == NULL)
10011 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10018 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10020 enum lu_ladvise_type advice;
10023 advice < ARRAY_SIZE(ladvise_names); advice++) {
10024 if (ladvise_names[advice] == NULL)
10026 if (strcmp(string, ladvise_names[advice]) == 0)
10030 return LU_LADVISE_INVALID;
10033 static int lfs_ladvise(int argc, char **argv)
10035 struct option long_opts[] = {
10036 { .val = 'a', .name = "advice", .has_arg = required_argument },
10037 { .val = 'b', .name = "background", .has_arg = no_argument },
10038 { .val = 'e', .name = "end", .has_arg = required_argument },
10039 { .val = 'h', .name = "help", .has_arg = no_argument },
10040 { .val = 'l', .name = "length", .has_arg = required_argument },
10041 { .val = 'm', .name = "mode", .has_arg = required_argument },
10042 { .val = 's', .name = "start", .has_arg = required_argument },
10043 { .val = 'u', .name = "unset", .has_arg = no_argument },
10044 { .name = NULL } };
10045 struct llapi_lu_ladvise advice;
10046 enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10047 unsigned long long start = 0;
10048 unsigned long long end = LUSTRE_EOF;
10049 unsigned long long length = 0;
10050 unsigned long long size_units;
10051 unsigned long long flags = 0;
10057 while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10058 long_opts, NULL)) != -1) {
10061 advice_type = lfs_get_ladvice(optarg);
10062 if (advice_type == LU_LADVISE_INVALID) {
10064 "%s: invalid advice type '%s'\n",
10066 fprintf(stderr, "Valid types:");
10068 for (advice_type = 0;
10069 advice_type < ARRAY_SIZE(ladvise_names);
10071 if (ladvise_names[advice_type] == NULL)
10073 fprintf(stderr, " %s",
10074 ladvise_names[advice_type]);
10076 fprintf(stderr, "\n");
10089 rc = llapi_parse_size(optarg, &end,
10092 fprintf(stderr, "%s: bad end offset '%s'\n",
10099 rc = llapi_parse_size(optarg, &start,
10103 "%s: bad start offset '%s'\n",
10110 rc = llapi_parse_size(optarg, &length,
10113 fprintf(stderr, "%s: bad length '%s'\n",
10119 mode = lfs_get_mode(optarg);
10122 "%s: bad mode '%s', valid modes are READ or WRITE\n",
10128 fprintf(stderr, "%s: unrecognized option '%s'\n",
10129 progname, argv[optind - 1]);
10136 if (advice_type == LU_LADVISE_INVALID) {
10137 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10138 fprintf(stderr, "Valid types:");
10139 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10141 if (ladvise_names[advice_type] == NULL)
10143 fprintf(stderr, " %s", ladvise_names[advice_type]);
10145 fprintf(stderr, "\n");
10149 if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10151 "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10156 if (argc <= optind) {
10157 fprintf(stderr, "%s: please give one or more file names\n",
10162 if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10163 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10168 if (end == LUSTRE_EOF && length != 0)
10169 end = start + length;
10171 if (end <= start) {
10172 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10173 argv[0], start, end);
10177 if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10178 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10183 if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10184 fprintf(stderr, "%s: mode is required with lockahead\n",
10189 while (optind < argc) {
10192 path = argv[optind++];
10194 fd = open(path, O_RDONLY);
10196 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10197 argv[0], path, strerror(errno));
10202 advice.lla_start = start;
10203 advice.lla_end = end;
10204 advice.lla_advice = advice_type;
10205 advice.lla_value1 = 0;
10206 advice.lla_value2 = 0;
10207 advice.lla_value3 = 0;
10208 advice.lla_value4 = 0;
10209 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10210 advice.lla_lockahead_mode = mode;
10211 advice.lla_peradvice_flags = flags;
10214 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10218 "%s: cannot give advice '%s' to file '%s': %s\n",
10219 argv[0], ladvise_names[advice_type],
10220 path, strerror(errno));
10226 if (rc == 0 && rc2 < 0)
10232 static const char *const heat_names[] = LU_HEAT_NAMES;
10234 static int lfs_heat_get(int argc, char **argv)
10236 struct lu_heat *heat;
10245 heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10247 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10252 while (optind < argc) {
10253 path = argv[optind++];
10255 fd = open(path, O_RDONLY);
10257 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10258 argv[0], path, strerror(errno));
10263 heat->lh_count = OBD_HEAT_COUNT;
10264 rc2 = llapi_heat_get(fd, heat);
10268 "%s: cannot get heat of file '%s': %s\n",
10269 argv[0], path, strerror(errno));
10273 printf("flags: %x\n", heat->lh_flags);
10274 for (i = 0; i < heat->lh_count; i++)
10275 printf("%s: %llu\n", heat_names[i],
10276 (unsigned long long)heat->lh_heat[i]);
10278 if (rc == 0 && rc2 < 0)
10286 static int lfs_heat_set(int argc, char **argv)
10288 struct option long_opts[] = {
10289 { .val = 'c', .name = "clear", .has_arg = no_argument },
10290 { .val = 'h', .name = "help", .has_arg = no_argument },
10291 { .val = 'o', .name = "off", .has_arg = no_argument },
10292 { .val = 'O', .name = "on", .has_arg = no_argument },
10293 { .name = NULL } };
10294 enum lu_heat_flag flags = 0;
10304 while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
10307 flags |= LU_HEAT_FLAG_CLEAR;
10310 flags |= LU_HEAT_FLAG_CLEAR;
10311 flags |= LU_HEAT_FLAG_OFF;
10314 flags &= ~LU_HEAT_FLAG_OFF;
10317 fprintf(stderr, "%s: unrecognized option '%s'\n",
10318 progname, argv[optind - 1]);
10325 if (argc <= optind) {
10326 fprintf(stderr, "%s: please give one or more file names\n",
10331 while (optind < argc) {
10332 path = argv[optind++];
10334 fd = open(path, O_RDONLY);
10336 fprintf(stderr, "%s: cannot open file '%s': %s\n",
10337 argv[0], path, strerror(errno));
10342 rc2 = llapi_heat_set(fd, flags);
10346 "%s: cannot setflags heat of file '%s': %s\n",
10347 argv[0], path, strerror(errno));
10351 if (rc == 0 && rc2 < 0)
10358 * The input string contains a comma delimited list of component ids and
10359 * ranges, for example "1,2-4,7".
10361 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10363 bool end_of_loop = false;
10371 while (!end_of_loop) {
10375 char *endptr = NULL;
10378 ptr = strchrnul(arg, ',');
10379 end_of_loop = *ptr == '\0';
10382 start_index = strtol(arg, &endptr, 0);
10383 if (endptr == arg) /* no data at all */
10385 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10387 if (start_index < 0)
10390 end_index = start_index;
10391 if (*endptr == '-') {
10392 end_index = strtol(endptr + 1, &endptr, 0);
10393 if (*endptr != '\0')
10395 if (end_index < start_index)
10399 for (i = start_index; i <= end_index && size > 0; i++) {
10402 /* remove duplicate */
10403 for (j = 0; j < nr; j++) {
10407 if (j == nr) { /* no duplicate */
10413 if (size == 0 && i < end_index)
10420 if (!end_of_loop && ptr)
10423 return rc < 0 ? rc : nr;
10427 * struct verify_mirror_id - Mirror id to be verified.
10428 * @mirror_id: A specified mirror id.
10429 * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10431 struct verify_mirror_id {
10437 * compare_mirror_ids() - Compare mirror ids.
10438 * @layout: Mirror component list.
10439 * @cbdata: Callback data in verify_mirror_id structure.
10441 * This is a callback function called by llapi_layout_comp_iterate()
10442 * to compare the specified mirror id with the one in the current
10443 * component of @layout. If they are the same, then the specified
10444 * mirror id is valid.
10446 * Return: a negative error code on failure or
10447 * LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10448 * LLAPI_LAYOUT_ITER_STOP: Stop iteration
10451 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10453 struct verify_mirror_id *mirror_id_cbdata =
10454 (struct verify_mirror_id *)cbdata;
10455 uint32_t mirror_id;
10458 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10462 "%s: llapi_layout_mirror_id_get failed: %s.\n",
10463 progname, strerror(errno));
10467 if (mirror_id_cbdata->mirror_id == mirror_id) {
10468 mirror_id_cbdata->is_valid_id = true;
10469 return LLAPI_LAYOUT_ITER_STOP;
10472 return LLAPI_LAYOUT_ITER_CONT;
10476 * verify_mirror_ids() - Verify specified mirror ids.
10477 * @fname: Mirrored file name.
10478 * @mirror_ids: Specified mirror ids to be verified.
10479 * @ids_nr: Number of specified mirror ids.
10481 * This function verifies that specified @mirror_ids are valid
10482 * in the mirrored file @fname.
10484 * Return: 0 on success or a negative error code on failure.
10487 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10489 struct llapi_layout *layout = NULL;
10490 struct verify_mirror_id mirror_id_cbdata = { 0 };
10492 uint32_t flr_state;
10501 if (stat(fname, &stbuf) < 0) {
10502 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10503 progname, fname, strerror(errno));
10508 if (!S_ISREG(stbuf.st_mode)) {
10509 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10515 fd = open(fname, O_DIRECT | O_RDONLY);
10517 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10518 progname, fname, strerror(errno));
10523 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10525 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10526 progname, fname, strerror(errno));
10530 layout = llapi_layout_get_by_fd(fd, 0);
10532 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10533 progname, fname, strerror(errno));
10535 llapi_lease_release(fd);
10539 rc = llapi_layout_flags_get(layout, &flr_state);
10541 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10542 progname, fname, strerror(errno));
10547 flr_state &= LCM_FL_FLR_MASK;
10548 switch (flr_state) {
10551 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10552 progname, fname, llapi_layout_flags_string(flr_state));
10559 for (i = 0; i < ids_nr; i++) {
10560 mirror_id_cbdata.mirror_id = mirror_ids[i];
10561 mirror_id_cbdata.is_valid_id = false;
10563 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10564 &mirror_id_cbdata);
10568 "%s: '%s' failed to verify mirror id: %u.\n",
10569 progname, fname, mirror_ids[i]);
10573 if (!mirror_id_cbdata.is_valid_id) {
10576 "%s: '%s' invalid specified mirror id: %u.\n",
10577 progname, fname, mirror_ids[i]);
10583 llapi_layout_free(layout);
10584 llapi_lease_release(fd);
10592 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10593 __u16 *mirror_ids, int ids_nr)
10595 struct llapi_resync_comp comp_array[1024] = { { 0 } };
10596 struct llapi_layout *layout;
10598 uint32_t flr_state;
10607 if (stat(fname, &stbuf) < 0) {
10608 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10609 progname, fname, strerror(errno));
10613 if (!S_ISREG(stbuf.st_mode)) {
10614 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10620 fd = open(fname, O_DIRECT | O_RDWR);
10622 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10623 progname, fname, strerror(errno));
10628 layout = llapi_layout_get_by_fd(fd, 0);
10630 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10631 progname, fname, strerror(errno));
10636 rc = llapi_layout_flags_get(layout, &flr_state);
10638 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10639 progname, fname, strerror(errno));
10644 flr_state &= LCM_FL_FLR_MASK;
10645 if (flr_state == LCM_FL_NONE) {
10647 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10652 /* get stale component info */
10653 comp_size = llapi_mirror_find_stale(layout, comp_array,
10654 ARRAY_SIZE(comp_array),
10655 mirror_ids, ids_nr);
10656 if (comp_size <= 0) {
10661 ioc->lil_mode = LL_LEASE_WRLCK;
10662 ioc->lil_flags = LL_LEASE_RESYNC;
10663 rc = llapi_lease_set(fd, ioc);
10665 if (rc == -EALREADY)
10669 "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10670 progname, fname, strerror(-rc));
10674 /* get the read range [start, end) */
10675 start = comp_array[0].lrc_start;
10676 end = comp_array[0].lrc_end;
10677 for (idx = 1; idx < comp_size; idx++) {
10678 if (comp_array[idx].lrc_start < start)
10679 start = comp_array[idx].lrc_start;
10680 if (end < comp_array[idx].lrc_end)
10681 end = comp_array[idx].lrc_end;
10684 rc = llapi_lease_check(fd);
10685 if (rc != LL_LEASE_WRLCK) {
10686 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10691 rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10694 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10695 progname, fname, strerror(-rc));
10697 rc = migrate_set_timestamps(fd, &stbuf);
10699 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
10700 progname, fname, strerror(-rc));
10704 /* need to do the lease unlock even resync fails */
10705 ioc->lil_mode = LL_LEASE_UNLCK;
10706 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10707 ioc->lil_count = 0;
10708 for (idx = 0; idx < comp_size; idx++) {
10709 if (comp_array[idx].lrc_synced) {
10710 ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10715 rc2 = llapi_lease_set(fd, ioc);
10717 * llapi_lease_set returns lease mode when it request to unlock
10721 /* rc2 == 0 means lost lease lock */
10722 if (rc2 == 0 && rc == 0)
10726 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10728 rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10730 llapi_lease_release(fd);
10735 llapi_layout_free(layout);
10742 static inline int lfs_mirror_resync(int argc, char **argv)
10744 struct option long_opts[] = {
10745 { .val = 'h', .name = "help", .has_arg = no_argument },
10746 { .val = 'o', .name = "only", .has_arg = required_argument },
10747 { .name = NULL } };
10748 struct ll_ioc_lease *ioc = NULL;
10749 __u16 mirror_ids[128] = { 0 };
10754 while ((c = getopt_long(argc, argv, "ho:", long_opts, NULL)) >= 0) {
10757 rc = parse_mirror_ids(mirror_ids,
10758 sizeof(mirror_ids) / sizeof(__u16),
10762 "%s: bad mirror ids '%s'.\n",
10769 fprintf(stderr, "%s: unrecognized option '%s'\n",
10770 progname, argv[optind - 1]);
10778 if (argc == optind) {
10779 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10784 if (ids_nr > 0 && argc > optind + 1) {
10786 "%s: option '--only' cannot be used upon multiple files.\n",
10793 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10798 /* set the lease on the file */
10799 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10801 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10802 argv[0], strerror(errno));
10807 for (; optind < argc; optind++) {
10808 rc = lfs_mirror_resync_file(argv[optind], ioc,
10809 mirror_ids, ids_nr);
10810 /* ignore previous file's error, continue with next file */
10813 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10821 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10823 struct llapi_layout *layout;
10826 layout = llapi_layout_get_by_fd(fd, 0);
10828 fprintf(stderr, "could not get layout.\n");
10832 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10834 fprintf(stderr, "failed to iterate layout\n");
10835 llapi_layout_free(layout);
10838 } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10839 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10840 llapi_layout_free(layout);
10844 llapi_layout_free(layout);
10850 * Check whether two files are the same file
10851 * \retval 0 same file
10852 * \retval 1 not the same file
10853 * \retval <0 error code
10855 static inline int check_same_file(int fd, const char *f2)
10857 struct stat stbuf1;
10858 struct stat stbuf2;
10860 if (fstat(fd, &stbuf1) < 0)
10863 if (stat(f2, &stbuf2) < 0)
10866 if (stbuf1.st_rdev == stbuf2.st_rdev &&
10867 stbuf1.st_ino == stbuf2.st_ino)
10873 static inline int lfs_mirror_read(int argc, char **argv)
10876 __u16 mirror_id = 0;
10877 const char *outfile = NULL;
10883 const size_t buflen = 4 << 20;
10885 struct option long_opts[] = {
10886 { .val = 'h', .name = "help", .has_arg = no_argument },
10887 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
10888 { .val = 'o', .name = "outfile", .has_arg = required_argument },
10889 { .name = NULL } };
10891 while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
10896 unsigned long int id;
10899 id = strtoul(optarg, &end, 0);
10900 if (errno != 0 || *end != '\0' || id == 0 ||
10903 "%s %s: invalid mirror ID '%s'\n",
10904 progname, argv[0], optarg);
10908 mirror_id = (__u16)id;
10915 fprintf(stderr, "%s: unrecognized option '%s'\n",
10916 progname, argv[optind - 1]);
10923 if (argc == optind) {
10924 fprintf(stderr, "%s %s: no mirrored file provided\n",
10925 progname, argv[0]);
10927 } else if (argc > optind + 1) {
10928 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10932 if (mirror_id == 0) {
10933 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10934 progname, argv[0]);
10938 /* open mirror file */
10939 fname = argv[optind];
10940 fd = open(fname, O_DIRECT | O_RDONLY);
10942 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10943 progname, argv[0], fname, strerror(errno));
10947 /* verify mirror id */
10948 rc = verify_mirror_id_by_fd(fd, mirror_id);
10951 "%s %s: cannot find mirror with ID %u in '%s'\n",
10952 progname, argv[0], mirror_id, fname);
10956 /* open output file - O_EXCL ensures output is not the same as input */
10958 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10960 fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10961 progname, argv[0], outfile, strerror(errno));
10966 outfd = STDOUT_FILENO;
10969 /* allocate buffer */
10970 rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10972 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10973 progname, argv[0], rc);
10979 ssize_t bytes_read;
10980 ssize_t written = 0;
10982 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10983 if (bytes_read < 0) {
10986 "%s %s: fail to read data from mirror %u: %s\n",
10987 progname, argv[0], mirror_id, strerror(-rc));
10992 if (bytes_read == 0)
10995 while (written < bytes_read) {
10998 written2 = write(outfd, buf + written,
10999 bytes_read - written);
11000 if (written2 < 0) {
11002 "%s %s: fail to write %s: %s\n",
11003 progname, argv[0], outfile ? : "STDOUT",
11008 written += written2;
11011 if (written != bytes_read) {
11013 "%s %s: written %ld bytes does not match with %ld read.\n",
11014 progname, argv[0], written, bytes_read);
11036 static inline int lfs_mirror_write(int argc, char **argv)
11039 __u16 mirror_id = 0;
11040 const char *inputfile = NULL;
11046 const size_t buflen = 4 << 20;
11048 size_t page_size = sysconf(_SC_PAGESIZE);
11049 struct ll_ioc_lease_id ioc;
11050 struct option long_opts[] = {
11051 { .val = 'h', .name = "help", .has_arg = no_argument },
11052 { .val = 'i', .name = "inputfile", .has_arg = required_argument },
11053 { .val = 'N', .name = "mirror-id", .has_arg = required_argument },
11054 { .name = NULL } };
11056 while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11061 unsigned long int id;
11064 id = strtoul(optarg, &end, 0);
11065 if (errno != 0 || *end != '\0' || id == 0 ||
11068 "%s %s: invalid mirror ID '%s'\n",
11069 progname, argv[0], optarg);
11073 mirror_id = (__u16)id;
11077 inputfile = optarg;
11080 fprintf(stderr, "%s: unrecognized option '%s'\n",
11081 progname, argv[optind - 1]);
11088 if (argc == optind) {
11089 fprintf(stderr, "%s %s: no mirrored file provided\n",
11090 progname, argv[0]);
11092 } else if (argc > optind + 1) {
11093 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11097 if (mirror_id == 0) {
11098 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11099 progname, argv[0]);
11103 /* open mirror file */
11104 fname = argv[optind];
11105 fd = open(fname, O_DIRECT | O_WRONLY);
11107 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11108 progname, argv[0], fname, strerror(errno));
11112 /* verify mirror id */
11113 rc = verify_mirror_id_by_fd(fd, mirror_id);
11116 "%s %s: cannot find mirror with ID %u in '%s'\n",
11117 progname, argv[0], mirror_id, fname);
11121 /* open input file */
11123 rc = check_same_file(fd, inputfile);
11126 "%s %s: input file cannot be the mirrored file\n",
11127 progname, argv[0]);
11133 inputfd = open(inputfile, O_RDONLY, 0644);
11135 fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11136 progname, argv[0], inputfile, strerror(errno));
11141 inputfd = STDIN_FILENO;
11144 /* allocate buffer */
11145 rc = posix_memalign(&buf, page_size, buflen);
11147 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11148 progname, argv[0], rc);
11149 goto close_inputfd;
11152 /* prepare target mirror components instantiation */
11153 ioc.lil_mode = LL_LEASE_WRLCK;
11154 ioc.lil_flags = LL_LEASE_RESYNC;
11155 ioc.lil_mirror_id = mirror_id;
11156 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11159 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11160 progname, argv[0], fname, strerror(errno));
11166 ssize_t bytes_read;
11170 rc = llapi_lease_check(fd);
11171 if (rc != LL_LEASE_WRLCK) {
11172 fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11173 progname, argv[0], fname);
11177 bytes_read = read(inputfd, buf, buflen);
11178 if (bytes_read < 0) {
11181 "%s %s: fail to read data from '%s': %s\n",
11182 progname, argv[0], inputfile ? : "STDIN",
11189 if (bytes_read == 0)
11192 /* round up to page align to make direct IO happy. */
11193 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11195 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11200 "%s %s: fail to write to mirror %u: %s\n",
11201 progname, argv[0], mirror_id,
11209 if (pos & (page_size - 1)) {
11210 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11215 ioc.lil_mode = LL_LEASE_UNLCK;
11216 ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11218 rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11223 "%s %s: release lease lock of '%s' failed: %s\n",
11224 progname, argv[0], fname, strerror(errno));
11241 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11243 struct llapi_layout *layout;
11244 struct collect_ids_data cid = { .cid_ids = ids,
11246 .cid_exclude = exclude_id, };
11249 layout = llapi_layout_get_by_fd(fd, 0);
11251 fprintf(stderr, "could not get layout\n");
11255 rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11257 fprintf(stderr, "failed to iterate layout\n");
11258 llapi_layout_free(layout);
11262 llapi_layout_free(layout);
11264 return cid.cid_count;
11267 #ifndef MIRROR_ID_NEG
11268 #define MIRROR_ID_NEG 0x8000
11271 static inline int lfs_mirror_copy(int argc, char **argv)
11274 __u16 read_mirror_id = 0;
11275 __u16 ids[128] = { 0 };
11277 struct llapi_layout *layout = NULL;
11278 struct llapi_resync_comp comp_array[1024] = { { 0 } };
11285 struct ll_ioc_lease *ioc = NULL;
11286 struct ll_ioc_lease_id *resync_ioc;
11287 struct option long_opts[] = {
11288 { .val = 'h', .name = "help", .has_arg = no_argument },
11289 { .val = 'i', .name = "read-mirror", .has_arg = required_argument },
11290 { .val = 'o', .name = "write-mirror", .has_arg = required_argument },
11291 { .name = NULL } };
11292 char cmd[PATH_MAX];
11294 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11296 while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
11301 unsigned long int id;
11304 id = strtoul(optarg, &end, 0);
11305 if (errno != 0 || *end != '\0' || id == 0 ||
11308 "%s: invalid read mirror ID '%s'\n",
11313 read_mirror_id = (__u16)id;
11317 if (!strcmp(optarg, "-1")) {
11318 /* specify all other mirrors */
11319 ids[0] = (__u16)-1;
11322 count = parse_mirror_ids((__u16 *)ids,
11330 fprintf(stderr, "%s: unrecognized option '%s'\n",
11331 progname, argv[optind - 1]);
11338 if (argc == optind) {
11339 fprintf(stderr, "%s %s: no mirrored file provided\n",
11340 progname, argv[0]);
11342 } else if (argc > optind + 1) {
11343 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11347 if (read_mirror_id == 0) {
11349 "%s %s: no valid read mirror ID %d is provided\n",
11350 progname, argv[0], read_mirror_id);
11356 "%s %s: no write mirror ID is provided\n",
11357 progname, argv[0]);
11361 for (i = 0; i < count; i++) {
11362 if (read_mirror_id == ids[i]) {
11364 "%s %s: read and write mirror ID cannot be the same\n",
11365 progname, argv[0]);
11370 /* open mirror file */
11371 fname = argv[optind];
11373 fd = open(fname, O_DIRECT | O_RDWR);
11375 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11376 progname, argv[0], fname, strerror(errno));
11380 /* write to all other mirrors */
11381 if (ids[0] == (__u16)-1) {
11382 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11386 "%s %s: failed to get other mirror ids in '%s': %d\n",
11387 progname, argv[0], fname, rc);
11392 /* verify mirror id */
11393 rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11396 "%s %s: cannot find mirror with ID %u in '%s'\n",
11397 progname, argv[0], read_mirror_id, fname);
11401 for (i = 0; i < count; i++) {
11402 rc = verify_mirror_id_by_fd(fd, ids[i]);
11405 "%s %s: cannot find mirror with ID %u in '%s'\n",
11406 progname, argv[0], ids[i], fname);
11411 ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11414 "%s %s: cannot alloc comp id array for ioc: %s\n",
11415 progname, argv[0], strerror(errno));
11420 /* get stale component info */
11421 layout = llapi_layout_get_by_fd(fd, 0);
11423 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11424 progname, argv[0], fname, strerror(errno));
11428 comp_size = llapi_mirror_find_stale(layout, comp_array,
11429 ARRAY_SIZE(comp_array),
11431 llapi_layout_free(layout);
11432 if (comp_size < 0) {
11437 /* prepare target mirror components instantiation */
11438 resync_ioc = (struct ll_ioc_lease_id *)ioc;
11439 resync_ioc->lil_mode = LL_LEASE_WRLCK;
11440 resync_ioc->lil_flags = LL_LEASE_RESYNC;
11442 resync_ioc->lil_mirror_id = ids[0];
11444 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11445 rc = llapi_lease_set(fd, ioc);
11448 "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11449 progname, argv[0], fname, strerror(errno));
11453 copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11456 fprintf(stderr, "%s %s: copy error: %d\n",
11457 progname, argv[0], rc);
11461 fprintf(stdout, "mirror copied successfully: ");
11462 for (i = 0; i < copied; i++)
11463 fprintf(stdout, "%d ", ids[i]);
11464 fprintf(stdout, "\n");
11466 ioc->lil_mode = LL_LEASE_UNLCK;
11467 ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11468 ioc->lil_count = 0;
11469 for (i = 0; i < comp_size; i++) {
11472 for (j = 0; j < copied; j++) {
11473 if (comp_array[i].lrc_mirror_id != ids[j])
11476 ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11480 rc = llapi_lease_set(fd, ioc);
11485 "%s %s: release lease lock of '%s' failed: %s\n",
11486 progname, argv[0], fname, strerror(errno));
11501 * struct verify_chunk - Mirror chunk to be verified.
11502 * @chunk: [start, end) of the chunk.
11503 * @mirror_count: Number of mirror ids in @mirror_id array.
11504 * @mirror_id: Array of valid mirror ids that cover the chunk.
11506 struct verify_chunk {
11507 struct lu_extent chunk;
11508 unsigned int mirror_count;
11509 __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11513 * print_chunks() - Print chunk information.
11514 * @fname: Mirrored file name.
11515 * @chunks: Array of chunks.
11516 * @chunk_count: Number of chunks in @chunks array.
11518 * This function prints [start, end) of each chunk in @chunks
11519 * for mirrored file @fname, and also prints the valid mirror ids
11520 * that cover the chunk.
11525 void print_chunks(const char *fname, struct verify_chunk *chunks,
11531 fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11532 for (i = 0; i < chunk_count; i++) {
11533 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11535 if (chunks[i].mirror_count == 0)
11536 fprintf(stdout, "\t[");
11538 fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11539 for (j = 1; j < chunks[i].mirror_count; j++)
11540 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11542 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11544 fprintf(stdout, "\n");
11548 * print_checksums() - Print CRC-32 checksum values.
11549 * @chunk: A chunk and its corresponding valid mirror ids.
11550 * @crc: CRC-32 checksum values on the chunk for each valid mirror.
11552 * This function prints CRC-32 checksum values on @chunk for
11553 * each valid mirror that covers it.
11558 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
11563 "CRC-32 checksum value for chunk "DEXT":\n",
11564 PEXT(&chunk->chunk));
11565 for (i = 0; i < chunk->mirror_count; i++)
11566 fprintf(stdout, "Mirror %u:\t%#lx\n",
11567 chunk->mirror_id[i], crc[i]);
11568 fprintf(stdout, "\n");
11572 * filter_mirror_id() - Filter specified mirror ids.
11573 * @chunks: Array of chunks.
11574 * @chunk_count: Number of chunks in @chunks array.
11575 * @mirror_ids: Specified mirror ids to be verified.
11576 * @ids_nr: Number of specified mirror ids.
11578 * This function scans valid mirror ids that cover each chunk in @chunks
11579 * and filters specified mirror ids.
11584 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11585 __u16 *mirror_ids, int ids_nr)
11590 __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11591 unsigned int valid_count = 0;
11593 for (i = 0; i < chunk_count; i++) {
11594 if (chunks[i].mirror_count == 0)
11598 for (j = 0; j < ids_nr; j++) {
11599 for (k = 0; k < chunks[i].mirror_count; k++) {
11600 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11601 valid_id[valid_count] = mirror_ids[j];
11608 memcpy(chunks[i].mirror_id, valid_id,
11609 sizeof(__u16) * valid_count);
11610 chunks[i].mirror_count = valid_count;
11615 * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11616 * @layout: Mirror component list.
11617 * @chunks: Array of chunks.
11618 * @chunks_size: Array size of @chunks.
11620 * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11621 * to find out chunk segments and store them in @chunks array.
11623 * The @mirror_id array in each element of @chunks will store the valid
11624 * mirror ids that cover the chunk. If a mirror component covering the
11625 * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11626 * will not be stored into the @mirror_id array, and the chunk for that
11627 * mirror will not be verified.
11629 * The @mirror_count in each element of @chunks will store the number of
11630 * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11631 * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11632 * indicates the chunk is valid in only one mirror. In both cases, the
11633 * chunk will not be verified.
11635 * Here is an example:
11637 * 0 1M 2M 3M 4M EOF
11638 * +------+-------------+--------------------+
11639 * | | | S | mirror1
11640 * +------+------+------+------+-------------+
11641 * | | S | S | | mirror2
11642 * +-------------+------+------+-------------+
11644 * prepared @chunks array will contain 5 elements:
11645 * (([0, 1M), [1, 2], 2),
11646 * ([1M, 2M), [1, 2], 2),
11647 * ([2M, 3M), [1], 1),
11648 * ([3M, 4M], [], 0),
11649 * ([4M, EOF), [2], 1))
11651 * Return: the actual array size of @chunks on success
11652 * or a negative error code on failure.
11655 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11656 struct verify_chunk *chunks,
11657 size_t chunks_size)
11661 uint32_t mirror_id;
11667 memset(chunks, 0, sizeof(*chunks) * chunks_size);
11670 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11673 "%s: move to the first layout component: %s.\n",
11674 progname, strerror(errno));
11680 chunks[idx].chunk.e_end = LUSTRE_EOF;
11682 rc = llapi_layout_comp_extent_get(layout, &start, &end);
11685 "%s: llapi_layout_comp_extent_get failed: %s.\n",
11686 progname, strerror(errno));
11690 if (start > chunks[idx].chunk.e_start ||
11691 end <= chunks[idx].chunk.e_start)
11694 if (end < chunks[idx].chunk.e_end)
11695 chunks[idx].chunk.e_end = end;
11697 rc = llapi_layout_comp_flags_get(layout, &flags);
11700 "%s: llapi_layout_comp_flags_get failed: %s.\n",
11701 progname, strerror(errno));
11705 if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11708 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11711 "%s: llapi_layout_mirror_id_get failed: %s.\n",
11712 progname, strerror(errno));
11716 chunks[idx].mirror_id[i] = mirror_id;
11718 if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11720 "%s: mirror_id array is too small.\n",
11727 rc = llapi_layout_comp_use(layout,
11728 LLAPI_LAYOUT_COMP_USE_NEXT);
11731 "%s: move to the next layout component: %s.\n",
11732 progname, strerror(errno));
11735 } /* loop through all components */
11737 chunks[idx].mirror_count = i;
11739 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11743 if (idx >= chunks_size) {
11744 fprintf(stderr, "%s: chunks array is too small.\n",
11750 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11754 return rc < 0 ? rc : idx + 1;
11758 * lfs_mirror_verify_chunk() - Verify a chunk.
11759 * @fd: File descriptor of the mirrored file.
11760 * @file_size: Size of the mirrored file.
11761 * @chunk: A chunk and its corresponding valid mirror ids.
11762 * @verbose: Verbose mode.
11764 * This function verifies a @chunk contains exactly the same data
11765 * ammong the mirrors that cover it.
11767 * If @verbose is specified, then the function will print where the
11768 * differences are if the data do not match. Otherwise, it will
11769 * just return an error in that case.
11771 * Return: 0 on success or a negative error code on failure.
11774 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11775 struct verify_chunk *chunk, int verbose)
11777 const size_t buflen = 4 * 1024 * 1024; /* 4M */
11779 size_t page_size = sysconf(_SC_PAGESIZE);
11780 ssize_t bytes_read;
11781 ssize_t bytes_done;
11785 unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11789 if (file_size == 0)
11792 rc = posix_memalign(&buf, page_size, buflen);
11793 if (rc) /* error code is returned directly */
11797 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11798 PEXT(&chunk->chunk));
11799 for (i = 0; i < chunk->mirror_count; i++)
11800 fprintf(stdout, " %u", chunk->mirror_id[i]);
11801 fprintf(stdout, "\n");
11805 count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11806 pos = chunk->chunk.e_start;
11807 while (bytes_done < count) {
11808 /* compute initial CRC-32 checksum */
11809 crc = crc32(0L, Z_NULL, 0);
11810 memset(crc_array, 0, sizeof(crc_array));
11813 for (i = 0; i < chunk->mirror_count; i++) {
11814 bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11816 if (bytes_read < 0) {
11819 "%s: failed to read data from mirror %u: %s.\n",
11820 progname, chunk->mirror_id[i],
11825 /* compute new CRC-32 checksum */
11826 crc_array[i] = crc32(crc, buf, bytes_read);
11830 print_checksums(chunk, crc_array);
11832 /* compare CRC-32 checksum values */
11833 for (i = 1; i < chunk->mirror_count; i++) {
11834 if (crc_array[i] != crc_array[0]) {
11838 "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11839 progname, PEXT(&chunk->chunk),
11840 chunk->mirror_id[0],
11841 chunk->mirror_id[i]);
11846 bytes_done += bytes_read;
11849 if (verbose > 1 && rc == 0) {
11850 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11851 PEXT(&chunk->chunk));
11852 for (i = 0; i < chunk->mirror_count; i++)
11853 fprintf(stdout, " %u", chunk->mirror_id[i]);
11854 fprintf(stdout, " PASS\n\n");
11863 * lfs_mirror_verify_file() - Verify a mirrored file.
11864 * @fname: Mirrored file name.
11865 * @mirror_ids: Specified mirror ids to be verified.
11866 * @ids_nr: Number of specified mirror ids.
11867 * @verbose: Verbose mode.
11869 * This function verifies that each SYNC mirror of a mirrored file
11870 * specified by @fname contains exactly the same data.
11872 * If @mirror_ids is specified, then the function will verify the
11873 * mirrors specified by @mirror_ids contain exactly the same data.
11875 * If @verbose is specified, then the function will print where the
11876 * differences are if the data do not match. Otherwise, it will
11877 * just return an error in that case.
11879 * Return: 0 on success or a negative error code on failure.
11882 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11885 struct verify_chunk chunks_array[1024] = { };
11886 struct llapi_layout *layout = NULL;
11888 uint32_t flr_state;
11890 int chunk_count = 0;
11896 if (stat(fname, &stbuf) < 0) {
11897 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11898 progname, fname, strerror(errno));
11903 if (!S_ISREG(stbuf.st_mode)) {
11904 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11910 if (stbuf.st_size == 0) {
11912 fprintf(stdout, "%s: '%s' file size is 0.\n",
11918 fd = open(fname, O_DIRECT | O_RDONLY);
11920 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11921 progname, fname, strerror(errno));
11926 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11928 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11929 progname, fname, strerror(errno));
11933 layout = llapi_layout_get_by_fd(fd, 0);
11935 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11936 progname, fname, strerror(errno));
11938 llapi_lease_release(fd);
11942 rc = llapi_layout_flags_get(layout, &flr_state);
11944 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11945 progname, fname, strerror(errno));
11950 flr_state &= LCM_FL_FLR_MASK;
11951 switch (flr_state) {
11954 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11955 progname, fname, llapi_layout_flags_string(flr_state));
11961 /* find out mirror chunks to be verified */
11962 chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11963 ARRAY_SIZE(chunks_array));
11964 if (chunk_count < 0) {
11970 /* filter specified mirror ids */
11971 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11974 print_chunks(fname, chunks_array, chunk_count);
11976 for (idx = 0; idx < chunk_count; idx++) {
11977 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11980 "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11982 PEXT(&chunks_array[idx].chunk),
11983 (unsigned long long)stbuf.st_size);
11987 if (chunks_array[idx].mirror_count == 0) {
11989 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11991 PEXT(&chunks_array[idx].chunk));
11993 fprintf(stderr, "skipped\n");
11997 fprintf(stderr, "failed\n");
12001 if (chunks_array[idx].mirror_count == 1) {
12004 "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12006 PEXT(&chunks_array[idx].chunk),
12007 chunks_array[idx].mirror_id[0]);
12011 rc = llapi_lease_check(fd);
12012 if (rc != LL_LEASE_RDLCK) {
12013 fprintf(stderr, "%s: '%s' lost lease lock.\n",
12018 /* verify one chunk */
12019 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12020 &chunks_array[idx], verbose);
12034 llapi_layout_free(layout);
12035 llapi_lease_release(fd);
12043 * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12044 * @argc: The count of lfs mirror verify command line arguments.
12045 * @argv: Array of strings for lfs mirror verify command line arguments.
12047 * This function parses lfs mirror verify command and verifies the
12048 * specified mirrored file(s).
12050 * Return: 0 on success or a negative error code on failure.
12052 static inline int lfs_mirror_verify(int argc, char **argv)
12054 __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12060 char cmd[PATH_MAX];
12062 struct option long_opts[] = {
12063 { .val = 'h', .name = "help", .has_arg = no_argument },
12064 { .val = 'o', .name = "only", .has_arg = required_argument },
12065 { .val = 'v', .name = "verbose", .has_arg = no_argument },
12066 { .name = NULL } };
12068 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12070 while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12073 rc = parse_mirror_ids(mirror_ids,
12074 ARRAY_SIZE(mirror_ids),
12078 "%s: bad mirror ids '%s'.\n",
12085 "%s: at least 2 mirror ids needed with '--only' option.\n",
12095 fprintf(stderr, "%s: unrecognized option '%s'\n",
12096 progname, argv[optind - 1]);
12104 if (argc == optind) {
12105 fprintf(stderr, "%s: no file name given.\n", progname);
12110 if (ids_nr > 0 && argc > optind + 1) {
12112 "%s: '--only' cannot be used upon multiple files.\n",
12119 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12125 for (; optind < argc; optind++) {
12126 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12136 * lfs_mirror() - Parse and execute lfs mirror commands.
12137 * @argc: The count of lfs mirror command line arguments.
12138 * @argv: Array of strings for lfs mirror command line arguments.
12140 * This function parses lfs mirror commands and performs the
12141 * corresponding functions specified in mirror_cmdlist[].
12143 * Return: 0 on success or an error code on failure.
12145 static int lfs_mirror(int argc, char **argv)
12147 char cmd[PATH_MAX];
12150 setlinebuf(stdout);
12152 Parser_init("lfs-mirror > ", mirror_cmdlist);
12154 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12156 program_invocation_short_name = cmd;
12158 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12160 rc = Parser_commands();
12162 return rc < 0 ? -rc : rc;
12165 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12167 #if __BYTE_ORDER == __BIG_ENDIAN
12168 __swab16s(&attrs->lsa_valid);
12169 __swab64s(&attrs->lsa_size);
12170 __swab64s(&attrs->lsa_blocks);
12174 enum lfs_som_type {
12175 LFS_SOM_SIZE = 0x1,
12176 LFS_SOM_BLOCKS = 0x2,
12177 LFS_SOM_FLAGS = 0x4,
12178 LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12182 static int lfs_getsom(int argc, char **argv)
12185 struct lustre_som_attrs *attrs;
12186 char buf[sizeof(*attrs) + 64];
12187 enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12190 while ((c = getopt(argc, argv, "bfhs")) != -1) {
12193 type = LFS_SOM_BLOCKS;
12196 type = LFS_SOM_FLAGS;
12199 type = LFS_SOM_SIZE;
12202 fprintf(stderr, "%s: unrecognized option '%s'\n",
12203 progname, argv[optind - 1]);
12214 fprintf(stderr, "%s: %s\n",
12215 progname, argc == 0 ? "miss file target" :
12216 "input more than 2 files");
12221 attrs = (void *)buf;
12222 rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12225 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12226 argv[0], strerror(errno), errno);
12230 lustre_som_swab(attrs);
12233 case LFS_SOM_ATTR_ALL:
12234 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12235 path, (unsigned long long)attrs->lsa_size,
12236 (unsigned long long)attrs->lsa_blocks,
12240 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12242 case LFS_SOM_BLOCKS:
12243 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12245 case LFS_SOM_FLAGS:
12246 printf("%x\n", attrs->lsa_valid);
12249 fprintf(stderr, "%s: unknown option\n", progname);
12257 * lfs_mirror_list_commands() - List lfs mirror commands.
12258 * @argc: The count of command line arguments.
12259 * @argv: Array of strings for command line arguments.
12261 * This function lists lfs mirror commands defined in mirror_cmdlist[].
12263 * Return: 0 on success.
12265 static int lfs_mirror_list_commands(int argc, char **argv)
12267 char buffer[81] = "";
12269 Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12275 static int lfs_pcc_attach(int argc, char **argv)
12277 struct option long_opts[] = {
12278 { .val = 'h', .name = "help", .has_arg = no_argument },
12279 { .val = 'i', .name = "id", .has_arg = required_argument },
12280 { .name = NULL } };
12283 __u32 archive_id = 0;
12286 char fullpath[PATH_MAX];
12287 enum lu_pcc_type type = LU_PCC_READWRITE;
12290 while ((c = getopt_long(argc, argv, "hi:",
12291 long_opts, NULL)) != -1) {
12295 archive_id = strtoul(optarg, &end, 0);
12296 if (errno != 0 || *end != '\0' ||
12297 archive_id == 0 || archive_id > UINT32_MAX) {
12299 "error: %s: bad archive ID '%s'\n",
12305 fprintf(stderr, "%s: unrecognized option '%s'\n",
12306 progname, argv[optind - 1]);
12313 if (archive_id == 0) {
12314 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12318 if (argc <= optind) {
12319 fprintf(stderr, "%s: must specify one or more file names\n",
12324 while (optind < argc) {
12327 path = argv[optind++];
12328 if (!realpath(path, fullpath)) {
12329 fprintf(stderr, "%s: could not find path '%s': %s\n",
12330 argv[0], path, strerror(errno));
12336 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12339 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12340 argv[0], path, archive_id, strerror(-rc2));
12348 static int lfs_pcc_attach_fid(int argc, char **argv)
12350 struct option long_opts[] = {
12351 { .val = 'h', .name = "help", .has_arg = no_argument },
12352 { .val = 'i', .name = "id", .has_arg = required_argument },
12353 { .val = 'm', .name = "mnt", .has_arg = required_argument },
12354 { .name = NULL } };
12357 __u32 archive_id = 0;
12359 const char *mntpath = NULL;
12360 const char *fidstr;
12361 enum lu_pcc_type type = LU_PCC_READWRITE;
12364 while ((c = getopt_long(argc, argv, "hi:m:",
12365 long_opts, NULL)) != -1) {
12369 archive_id = strtoul(optarg, &end, 0);
12370 if (errno != 0 || *end != '\0' ||
12371 archive_id > UINT32_MAX) {
12373 "error: %s: bad archive ID '%s'\n",
12382 fprintf(stderr, "%s: unrecognized option '%s'\n",
12383 progname, argv[optind - 1]);
12390 if (archive_id == 0) {
12391 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12396 fprintf(stderr, "%s: must specify Lustre mount point\n",
12401 if (argc <= optind) {
12402 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12406 while (optind < argc) {
12409 fidstr = argv[optind++];
12411 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12415 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12416 argv[0], fidstr, mntpath, archive_id,
12419 if (rc == 0 && rc2 < 0)
12425 static int lfs_pcc_detach(int argc, char **argv)
12427 struct option long_opts[] = {
12428 { .val = 'h', .name = "help", .has_arg = no_argument },
12429 { .val = 'k', .name = "keep", .has_arg = no_argument },
12430 { .name = NULL } };
12434 char fullpath[PATH_MAX];
12435 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12438 while ((c = getopt_long(argc, argv, "hk",
12439 long_opts, NULL)) != -1) {
12442 detach_opt = PCC_DETACH_OPT_NONE;
12445 fprintf(stderr, "%s: unrecognized option '%s'\n",
12446 progname, argv[optind - 1]);
12453 while (optind < argc) {
12456 path = argv[optind++];
12457 if (!realpath(path, fullpath)) {
12458 fprintf(stderr, "%s: could not find path '%s': %s\n",
12459 argv[0], path, strerror(errno));
12465 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12469 "%s: cannot detach '%s' from PCC: %s\n",
12470 argv[0], path, strerror(errno));
12478 static int lfs_pcc_detach_fid(int argc, char **argv)
12480 struct option long_opts[] = {
12481 { .val = 'h', .name = "help", .has_arg = no_argument },
12482 { .val = 'k', .name = "keep", .has_arg = no_argument },
12483 { .name = NULL } };
12487 const char *mntpath;
12488 __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12491 while ((c = getopt_long(argc, argv, "hk",
12492 long_opts, NULL)) != -1) {
12495 detach_opt = PCC_DETACH_OPT_NONE;
12498 fprintf(stderr, "%s: unrecognized option '%s'\n",
12499 progname, argv[optind - 1]);
12506 mntpath = argv[optind++];
12508 while (optind < argc) {
12511 fid = argv[optind++];
12513 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12516 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12517 argv[0], fid, mntpath, strerror(-rc2));
12525 static int lfs_pcc_state(int argc, char **argv)
12529 char fullpath[PATH_MAX];
12530 struct lu_pcc_state state;
12535 fprintf(stderr, "%s: must specify one or more file names\n",
12540 while (optind < argc) {
12543 path = argv[optind++];
12544 if (!realpath(path, fullpath)) {
12545 fprintf(stderr, "%s: could not find path '%s': %s\n",
12546 argv[0], path, strerror(errno));
12552 rc2 = llapi_pcc_state_get(fullpath, &state);
12557 "%s: cannot get PCC state of '%s': %s\n",
12558 argv[0], path, strerror(-rc2));
12562 printf("file: %s", path);
12563 printf(", type: %s", pcc_type2string(state.pccs_type));
12564 if (state.pccs_type == LU_PCC_NONE &&
12565 state.pccs_open_count == 0) {
12570 printf(", PCC file: %s", state.pccs_path);
12571 printf(", user number: %u", state.pccs_open_count);
12572 printf(", flags: %x", state.pccs_flags);
12579 * lfs_pcc_list_commands() - List lfs pcc commands.
12580 * @argc: The count of command line arguments.
12581 * @argv: Array of strings for command line arguments.
12583 * This function lists lfs pcc commands defined in pcc_cmdlist[].
12585 * Return: 0 on success.
12587 static int lfs_pcc_list_commands(int argc, char **argv)
12589 char buffer[81] = "";
12591 Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12598 * lfs_pcc() - Parse and execute lfs pcc commands.
12599 * @argc: The count of lfs pcc command line arguments.
12600 * @argv: Array of strings for lfs pcc command line arguments.
12602 * This function parses lfs pcc commands and performs the
12603 * corresponding functions specified in pcc_cmdlist[].
12605 * Return: 0 on success or an error code on failure.
12607 static int lfs_pcc(int argc, char **argv)
12609 char cmd[PATH_MAX];
12612 setlinebuf(stdout);
12614 Parser_init("lfs-pcc > ", pcc_cmdlist);
12616 snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12618 program_invocation_short_name = cmd;
12620 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12622 rc = Parser_commands();
12624 return rc < 0 ? -rc : rc;
12627 static int lfs_list_commands(int argc, char **argv)
12629 char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12631 Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12636 int main(int argc, char **argv)
12640 /* Ensure that liblustreapi constructor has run */
12641 if (!llapi_liblustreapi_initialized())
12642 fprintf(stderr, "liblustreapi was not properly initialized\n");
12644 setlinebuf(stdout);
12647 Parser_init("lfs > ", cmdlist);
12649 progname = program_invocation_short_name; /* Used in error messages */
12651 llapi_set_command_name(argv[1]);
12652 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12653 llapi_clear_command_name();
12655 rc = Parser_commands();
12658 return rc < 0 ? -rc : rc;
12661 #ifdef _LUSTRE_IDL_H_
12662 /* Everything we need here should be included by lustreapi.h. */
12663 # error "lfs should not depend on lustre_idl.h"
12664 #endif /* _LUSTRE_IDL_H_ */