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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Peter J. Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
40 * Author: Robert Read <rread@clusterfs.com>
57 #include <sys/quota.h>
58 #include <sys/types.h>
64 #ifdef HAVE_SYS_QUOTA_H
65 # include <sys/quota.h>
68 #include <libcfs/libcfs.h>
69 #include <libcfs/libcfsutil.h>
70 #include <lustre/lustreapi.h>
71 #include <lustre_ver.h>
74 static int lfs_setstripe(int argc, char **argv);
75 static int lfs_find(int argc, char **argv);
76 static int lfs_getstripe(int argc, char **argv);
77 static int lfs_getdirstripe(int argc, char **argv);
78 static int lfs_setdirstripe(int argc, char **argv);
79 static int lfs_rmentry(int argc, char **argv);
80 static int lfs_osts(int argc, char **argv);
81 static int lfs_mdts(int argc, char **argv);
82 static int lfs_df(int argc, char **argv);
83 static int lfs_getname(int argc, char **argv);
84 static int lfs_check(int argc, char **argv);
85 #ifdef HAVE_SYS_QUOTA_H
86 static int lfs_quotacheck(int argc, char **argv);
87 static int lfs_quotaon(int argc, char **argv);
88 static int lfs_quotaoff(int argc, char **argv);
89 static int lfs_setquota(int argc, char **argv);
90 static int lfs_quota(int argc, char **argv);
92 static int lfs_flushctx(int argc, char **argv);
93 static int lfs_join(int argc, char **argv);
94 static int lfs_lsetfacl(int argc, char **argv);
95 static int lfs_lgetfacl(int argc, char **argv);
96 static int lfs_rsetfacl(int argc, char **argv);
97 static int lfs_rgetfacl(int argc, char **argv);
98 static int lfs_cp(int argc, char **argv);
99 static int lfs_ls(int argc, char **argv);
100 static int lfs_poollist(int argc, char **argv);
101 static int lfs_changelog(int argc, char **argv);
102 static int lfs_changelog_clear(int argc, char **argv);
103 static int lfs_fid2path(int argc, char **argv);
104 static int lfs_path2fid(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);
118 #define SETSTRIPE_USAGE(_cmd, _tgt) \
119 "usage: "_cmd" [--stripe-count|-c <stripe_count>]\n"\
120 " [--stripe-index|-i <start_ost_idx>]\n"\
121 " [--stripe-size|-S <stripe_size>]\n"\
122 " [--pool|-p <pool_name>]\n"\
123 " [--block|-b] "_tgt"\n"\
124 "\tstripe_size: Number of bytes on each OST (0 filesystem default)\n"\
125 "\t Can be specified with k, m or g (in KB, MB and GB\n"\
126 "\t respectively)\n"\
127 "\tstart_ost_idx: OST index of first stripe (-1 default)\n"\
128 "\tstripe_count: Number of OSTs to stripe over (0 default, -1 all)\n"\
129 "\tpool_name: Name of OST pool to use (default none)\n"\
130 "\tblock: Block file access during data migration"
132 /* all avaialable commands */
133 command_t cmdlist[] = {
134 {"setstripe", lfs_setstripe, 0,
135 "Create a new file with a specific striping pattern or\n"
136 "set the default striping pattern on an existing directory or\n"
137 "delete the default striping pattern from an existing directory\n"
138 "usage: setstripe -d <directory> (to delete default striping)\n"\
140 SETSTRIPE_USAGE("setstripe", "<directory|filename>")},
141 {"getstripe", lfs_getstripe, 0,
142 "To list the striping info for a given file or files in a\n"
143 "directory or recursively for all files in a directory tree.\n"
144 "usage: getstripe [--ost|-O <uuid>] [--quiet | -q] [--verbose | -v]\n"
145 " [--stripe-count|-c] [--stripe-index|-i]\n"
146 " [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
147 " [--mdt-index|-M] [--recursive|-r] [--raw|-R]\n"
149 " <directory|filename> ..."},
150 {"setdirstripe", lfs_setdirstripe, 0,
151 "To create a striped directory on a specified MDT. This can only\n"
152 "be done on MDT0 with the right of administrator.\n"
153 "usage: setdirstripe <--count|-c stripe_count>\n"
154 " [--index|-i mdt_index] [--hash-type|-t hash_type]\n"
155 " [--default_stripe|-D ] [--mode|-m mode] <dir>\n"
156 "\tstripe_count: stripe count of the striped directory\n"
157 "\tmdt_index: MDT index of first stripe\n"
158 "\thash_type: hash type of the striped directory. Hash types:\n"
159 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
160 " all_char sum of characters % MDT_COUNT (not recommended)\n"
161 "\tdefault_stripe: set default dirstripe of the directory\n"
162 "\tmode: the mode of the directory\n"},
163 {"getdirstripe", lfs_getdirstripe, 0,
164 "To list the striping info for a given directory\n"
165 "or recursively for all directories in a directory tree.\n"
166 "usage: getdirstripe [--obd|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
167 " [--count|-c ] [--index|-i ] [--raw|-R]\n"
168 " [--recursive | -r] [ --default_stripe | -D ] <dir> "},
169 {"mkdir", lfs_setdirstripe, 0,
170 "To create a striped directory on a specified MDT. This can only\n"
171 "be done on MDT0 with the right of administrator.\n"
172 "usage: mkdir <--count|-c stripe_count>\n"
173 " [--index|-i mdt_index] [--hash-type|-t hash_type]\n"
174 " [--default_stripe|-D ] [--mode|-m mode] <dir>\n"
175 "\tstripe_count: stripe count of the striped directory\n"
176 "\tmdt_index: MDT index of first stripe\n"
177 "\thash_type: hash type of the striped directory. Hash types:\n"
178 " fnv_1a_64 FNV-1a hash algorithm (default)\n"
179 " all_char sum of characters % MDT_COUNT (not recommended)\n"
180 "\tdefault_stripe: set default dirstripe of the directory\n"
181 "\tmode: the mode of the directory\n"},
182 {"rm_entry", lfs_rmentry, 0,
183 "To remove the name entry of the remote directory. Note: This\n"
184 "command will only delete the name entry, i.e. the remote directory\n"
185 "will become inaccessable after this command. This can only be done\n"
186 "by the administrator\n"
187 "usage: rm_entry <dir>\n"},
188 {"pool_list", lfs_poollist, 0,
189 "List pools or pool OSTs\n"
190 "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
191 {"find", lfs_find, 0,
192 "find files matching given attributes recursively in directory tree.\n"
193 "usage: find <directory|filename> ...\n"
194 " [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
195 " [[!] --mtime|-M [+-]N] [[!] --mdt|-m <uuid|index,...>]\n"
196 " [--maxdepth|-D N] [[!] --name|-n <pattern>]\n"
197 " [[!] --ost|-O <uuid|index,...>] [--print|-p] [--print0|-P]\n"
198 " [[!] --size|-s [+-]N[bkMGTPE]]\n"
199 " [[!] --stripe-count|-c [+-]<stripes>]\n"
200 " [[!] --stripe-index|-i <index,...>]\n"
201 " [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
202 " [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
203 " [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
204 " [[!] --layout|-L released,raid0]\n"
205 "\t !: used before an option indicates 'NOT' requested attribute\n"
206 "\t -: used before a value indicates 'AT MOST' requested value\n"
207 "\t +: used before a value indicates 'AT LEAST' requested value\n"},
208 {"check", lfs_check, 0,
209 "Display the status of MDS or OSTs (as specified in the command)\n"
210 "or all the servers (MDS and OSTs).\n"
211 "usage: check <osts|mds|servers>"},
212 {"join", lfs_join, 0,
213 "join two lustre files into one.\n"
214 "obsolete, HEAD does not support it anymore.\n"},
215 {"osts", lfs_osts, 0, "list OSTs connected to client "
216 "[for specified path only]\n" "usage: osts [path]"},
217 {"mdts", lfs_mdts, 0, "list MDTs connected to client "
218 "[for specified path only]\n" "usage: mdts [path]"},
220 "report filesystem disk space usage or inodes usage"
221 "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
222 "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
223 {"getname", lfs_getname, 0, "list instances and specified mount points "
224 "[for specified path only]\n"
225 "Usage: getname [-h]|[path ...] "},
226 #ifdef HAVE_SYS_QUOTA_H
227 {"quotacheck", lfs_quotacheck, 0,
228 "Scan the specified filesystem for disk usage, and create,\n"
229 "or update quota files. Deprecated as of 2.4.0.\n"
230 "usage: quotacheck [ -ug ] <filesystem>"},
231 {"quotaon", lfs_quotaon, 0, "Turn filesystem"
232 " quotas on. Deprecated as of 2.4.0.\n"
233 "usage: quotaon [ -ugf ] <filesystem>"},
234 {"quotaoff", lfs_quotaoff, 0, "Turn filesystem"
235 " quotas off. Deprecated as of 2.4.0.\n"
236 "usage: quotaoff [ -ug ] <filesystem>"},
237 {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
238 "usage: setquota <-u|-g> <uname>|<uid>|<gname>|<gid>\n"
239 " -b <block-softlimit> -B <block-hardlimit>\n"
240 " -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
241 " setquota <-u|--user|-g|--group> <uname>|<uid>|<gname>|<gid>\n"
242 " [--block-softlimit <block-softlimit>]\n"
243 " [--block-hardlimit <block-hardlimit>]\n"
244 " [--inode-softlimit <inode-softlimit>]\n"
245 " [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
246 " setquota [-t] <-u|--user|-g|--group>\n"
247 " [--block-grace <block-grace>]\n"
248 " [--inode-grace <inode-grace>] <filesystem>\n"
249 " -b can be used instead of --block-softlimit/--block-grace\n"
250 " -B can be used instead of --block-hardlimit\n"
251 " -i can be used instead of --inode-softlimit/--inode-grace\n"
252 " -I can be used instead of --inode-hardlimit\n\n"
253 "Note: The total quota space will be split into many qunits and\n"
254 " balanced over all server targets, the minimal qunit size is\n"
255 " 1M bytes for block space and 1K inodes for inode space.\n\n"
256 " Quota space rebalancing process will stop when this mininum\n"
257 " value is reached. As a result, quota exceeded can be returned\n"
258 " while many targets still have 1MB or 1K inodes of spare\n"
260 {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
261 "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
263 " [<-u|-g> <uname>|<uid>|<gname>|<gid>] <filesystem>\n"
264 " quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g> <filesystem>"},
266 {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
267 "usage: flushctx [-k] [mountpoint...]"},
268 {"lsetfacl", lfs_lsetfacl, 0,
269 "Remote user setfacl for user/group on the same remote client.\n"
270 "usage: lsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
271 {"lgetfacl", lfs_lgetfacl, 0,
272 "Remote user getfacl for user/group on the same remote client.\n"
273 "usage: lgetfacl [-dRLPvh] file ..."},
274 {"rsetfacl", lfs_rsetfacl, 0,
275 "Remote user setfacl for user/group on other clients.\n"
276 "usage: rsetfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ..."},
277 {"rgetfacl", lfs_rgetfacl, 0,
278 "Remote user getfacl for user/group on other clients.\n"
279 "usage: rgetfacl [-dRLPvh] file ..."},
281 "Remote user copy files and directories.\n"
282 "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
284 "Remote user list directory contents.\n"
285 "usage: ls [OPTION]... [FILE]..."},
286 {"changelog", lfs_changelog, 0,
287 "Show the metadata changes on an MDT."
288 "\nusage: changelog <mdtname> [startrec [endrec]]"},
289 {"changelog_clear", lfs_changelog_clear, 0,
290 "Indicate that old changelog records up to <endrec> are no longer of "
291 "interest to consumer <id>, allowing the system to free up space.\n"
292 "An <endrec> of 0 means all records.\n"
293 "usage: changelog_clear <mdtname> <id> <endrec>"},
294 {"fid2path", lfs_fid2path, 0,
295 "Resolve the full path(s) for given FID(s). For a specific hardlink "
296 "specify link number <linkno>.\n"
297 /* "For a historical link name, specify changelog record <recno>.\n" */
298 "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
299 /* [ --rec <recno> ] */ },
300 {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
301 "usage: path2fid <path> ..."},
302 {"data_version", lfs_data_version, 0, "Display file data version for "
303 "a given path.\n" "usage: data_version -[n|r|w] <path>"},
304 {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
305 "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
306 {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
307 "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
308 "[--archived] [--lost] <file> ..."},
309 {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
311 "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
312 "[--archived] [--lost] <file> ..."},
313 {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
314 "given files.\n" "usage: hsm_action <file> ..."},
315 {"hsm_archive", lfs_hsm_archive, 0,
316 "Archive file to external storage.\n"
317 "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
319 {"hsm_restore", lfs_hsm_restore, 0,
320 "Restore file from external storage.\n"
321 "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
322 {"hsm_release", lfs_hsm_release, 0,
323 "Release files from Lustre.\n"
324 "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
325 {"hsm_remove", lfs_hsm_remove, 0,
326 "Remove file copy from external storage.\n"
327 "usage: hsm_remove [--filelist FILELIST] [--data DATA] <file> ..."},
328 {"hsm_cancel", lfs_hsm_cancel, 0,
329 "Cancel requests related to specified files.\n"
330 "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
331 {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
332 "usage: swap_layouts <path1> <path2>"},
333 {"migrate", lfs_setstripe, 0, "migrate file from one OST layout to "
334 "another (may be not safe with concurent writes).\n"
335 SETSTRIPE_USAGE("migrate ", "<filename>")},
337 "To move directories between MDTs.\n"
338 "usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
340 {"help", Parser_help, 0, "help"},
341 {"exit", Parser_quit, 0, "quit"},
342 {"quit", Parser_quit, 0, "quit"},
343 {"--version", Parser_version, 0,
344 "output build version of the utility and exit"},
348 #define MIGRATION_BLOCKS 1
350 static int lfs_migrate(char *name, unsigned long long stripe_size,
351 int stripe_offset, int stripe_count,
352 int stripe_pattern, char *pool_name,
353 __u64 migration_flags)
356 char volatile_file[PATH_MAX +
357 LUSTRE_VOLATILE_HDR_LEN + 4];
358 char parent[PATH_MAX];
362 struct lov_user_md *lum = NULL;
367 __u64 rpos, wpos, bufoff;
372 /* find the right size for the IO and allocate the buffer */
373 lumsz = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
380 rc = llapi_file_get_stripe(name, lum);
381 /* failure can come from may case and some may be not real error
383 * in case of a real error, a later call will failed with a better
384 * error management */
388 bufsz = lum->lmm_stripe_size;
389 rc = posix_memalign(&buf, getpagesize(), bufsz);
395 if (migration_flags & MIGRATION_BLOCKS) {
396 /* generate a random id for the grouplock */
397 fd = open("/dev/urandom", O_RDONLY);
400 fprintf(stderr, "cannot open /dev/urandom (%s)\n",
405 rc = read(fd, &gid, sz);
409 fprintf(stderr, "cannot read %d bytes from"
410 " /dev/urandom (%s)\n", sz, strerror(-rc));
415 /* search for file directory pathname */
416 if (strlen(name) > sizeof(parent)-1) {
420 strncpy(parent, name, sizeof(parent));
421 ptr = strrchr(parent, '/');
423 if (getcwd(parent, sizeof(parent)) == NULL) {
433 rc = snprintf(volatile_file, sizeof(volatile_file), "%s/%s::", parent,
434 LUSTRE_VOLATILE_HDR);
435 if (rc >= sizeof(volatile_file)) {
440 /* create, open a volatile file, use caching (ie no directio) */
441 /* exclusive create is not needed because volatile files cannot
442 * conflict on name by construction */
443 fdv = llapi_file_open_pool(volatile_file, O_CREAT | O_WRONLY,
444 0644, stripe_size, stripe_offset,
445 stripe_count, stripe_pattern, pool_name);
448 fprintf(stderr, "cannot create volatile file in %s (%s)\n",
449 parent, strerror(-rc));
453 /* open file, direct io */
454 /* even if the file is only read, WR mode is nedeed to allow
455 * layout swap on fd */
456 fd = open(name, O_RDWR | O_DIRECT);
459 fprintf(stderr, "cannot open %s (%s)\n", name, strerror(-rc));
464 /* Not-owner (root?) special case.
465 * Need to set owner/group of volatile file like original.
466 * This will allow to pass related check during layout_swap.
471 fprintf(stderr, "cannot stat %s (%s)\n", name,
475 rc = fstat(fdv, &stv);
478 fprintf(stderr, "cannot stat %s (%s)\n", volatile_file,
482 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
483 rc = fchown(fdv, st.st_uid, st.st_gid);
486 fprintf(stderr, "cannot chown %s (%s)\n", name,
492 /* get file data version */
493 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
495 fprintf(stderr, "cannot get dataversion on %s (%s)\n",
496 name, strerror(-rc));
500 if (migration_flags & MIGRATION_BLOCKS) {
501 /* take group lock to limit concurent access
502 * this will be no more needed when exclusive access will
503 * be implemented (see LU-2919) */
504 /* group lock is taken after data version read because it
505 * blocks data version call */
506 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
508 fprintf(stderr, "cannot get group lock on %s (%s)\n",
509 name, strerror(-rc));
521 /* read new data only if we have written all
522 * previously read data */
524 rsize = read(fd, buf, bufsz);
527 fprintf(stderr, "read failed on %s"
538 wsize = write(fdv, buf + bufoff, rpos - wpos);
541 fprintf(stderr, "write failed on volatile"
542 " for %s (%s)\n", name, strerror(-rc));
552 if (migration_flags & MIGRATION_BLOCKS) {
553 /* give back group lock */
554 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
556 fprintf(stderr, "cannot put group lock on %s (%s)\n",
557 name, strerror(-rc));
563 * for a migration we need to:
564 * - check data version on file did not change
568 rc = llapi_fswap_layouts(fd, fdv, dv1, 0,
569 SWAP_LAYOUTS_CHECK_DV1 |
570 SWAP_LAYOUTS_KEEP_MTIME |
571 SWAP_LAYOUTS_KEEP_ATIME);
573 fprintf(stderr, "%s: dataversion changed during copy, "
574 "migration aborted\n", name);
578 fprintf(stderr, "%s: swap layout to new file failed: %s\n",
579 name, strerror(-rc));
582 /* give back group lock */
583 if ((migration_flags & MIGRATION_BLOCKS) && have_gl &&
584 (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1)) {
585 /* we keep in rc the original error */
586 fprintf(stderr, "cannot put group lock on %s (%s)\n",
587 name, strerror(-errno));
601 static int lfs_setstripe(int argc, char **argv)
605 unsigned long long st_size;
606 int st_offset, st_count;
610 char *stripe_size_arg = NULL;
611 char *stripe_off_arg = NULL;
612 char *stripe_count_arg = NULL;
613 char *pool_name_arg = NULL;
614 unsigned long long size_units = 1;
615 int migrate_mode = 0;
616 __u64 migration_flags = 0;
618 struct option long_opts[] = {
619 /* valid only in migrate mode */
620 {"block", no_argument, 0, 'b'},
621 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
622 /* This formerly implied "stripe-count", but was explicitly
623 * made "stripe-count" for consistency with other options,
624 * and to separate it from "mdt-count" when DNE arrives. */
625 {"count", required_argument, 0, 'c'},
627 {"stripe-count", required_argument, 0, 'c'},
628 {"stripe_count", required_argument, 0, 'c'},
629 {"delete", no_argument, 0, 'd'},
630 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
631 /* This formerly implied "stripe-index", but was explicitly
632 * made "stripe-index" for consistency with other options,
633 * and to separate it from "mdt-index" when DNE arrives. */
634 {"index", required_argument, 0, 'i'},
636 {"stripe-index", required_argument, 0, 'i'},
637 {"stripe_index", required_argument, 0, 'i'},
638 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
639 /* This formerly implied "stripe-index", but was confusing
640 * with "file offset" (which will eventually be needed for
641 * with different layouts by offset), so deprecate it. */
642 {"offset", required_argument, 0, 'o'},
644 {"pool", required_argument, 0, 'p'},
645 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
646 /* This formerly implied "--stripe-size", but was confusing
647 * with "lfs find --size|-s", which means "file size", so use
648 * the consistent "--stripe-size|-S" for all commands. */
649 {"size", required_argument, 0, 's'},
651 {"stripe-size", required_argument, 0, 'S'},
652 {"stripe_size", required_argument, 0, 'S'},
660 if (strcmp(argv[0], "migrate") == 0)
664 while ((c = getopt_long(argc, argv, "c:di:o:p:s:S:",
665 long_opts, NULL)) >= 0) {
671 if (migrate_mode == 0) {
672 fprintf(stderr, "--block is valid only for"
676 migration_flags |= MIGRATION_BLOCKS;
679 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
680 if (strcmp(argv[optind - 1], "--count") == 0)
681 fprintf(stderr, "warning: '--count' deprecated"
682 ", use '--stripe-count' instead\n");
684 stripe_count_arg = optarg;
687 /* delete the default striping pattern */
690 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
692 fprintf(stderr, "warning: '--offset|-o' deprecated, "
693 "use '--stripe-index|-i' instead\n");
696 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
697 if (strcmp(argv[optind - 1], "--index") == 0)
698 fprintf(stderr, "warning: '--index' deprecated"
699 ", use '--stripe-index' instead\n");
701 stripe_off_arg = optarg;
703 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
705 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
706 fprintf(stderr, "warning: '--size|-s' deprecated, "
707 "use '--stripe-size|-S' instead\n");
709 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0) */
711 stripe_size_arg = optarg;
714 pool_name_arg = optarg;
721 fname = argv[optind];
724 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
725 stripe_count_arg != NULL || pool_name_arg != NULL)) {
726 fprintf(stderr, "error: %s: cannot specify -d with "
727 "-s, -c, -o, or -p options\n",
732 if (optind == argc) {
733 fprintf(stderr, "error: %s: missing filename|dirname\n",
738 /* get the stripe size */
739 if (stripe_size_arg != NULL) {
740 result = llapi_parse_size(stripe_size_arg, &st_size,
743 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
744 argv[0], stripe_size_arg);
748 /* get the stripe offset */
749 if (stripe_off_arg != NULL) {
750 st_offset = strtol(stripe_off_arg, &end, 0);
752 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
753 argv[0], stripe_off_arg);
757 /* get the stripe count */
758 if (stripe_count_arg != NULL) {
759 st_count = strtoul(stripe_count_arg, &end, 0);
761 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
762 argv[0], stripe_count_arg);
769 result = lfs_migrate(fname, st_size, st_offset,
770 st_count, 0, pool_name_arg,
773 result = llapi_file_create_pool(fname, st_size,
778 "error: %s: %s stripe file '%s' failed\n",
779 argv[0], migrate_mode ? "migrate" : "create",
783 fname = argv[++optind];
784 } while (fname != NULL);
789 static int lfs_poollist(int argc, char **argv)
794 return llapi_poollist(argv[1]);
797 static int set_time(time_t *time, time_t *set, char *str)
804 else if (str[0] == '-')
810 t = strtol(str, NULL, 0);
811 if (*time < t * 24 * 60 * 60) {
814 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
818 *set = *time - t * 24 * 60 * 60;
825 static int name2id(unsigned int *id, char *name, int type)
828 struct passwd *entry;
830 if (!(entry = getpwnam(name))) {
840 if (!(entry = getgrnam(name))) {
852 static int id2name(char **name, unsigned int id, int type)
855 struct passwd *entry;
857 if (!(entry = getpwuid(id))) {
863 *name = entry->pw_name;
867 if (!(entry = getgrgid(id))) {
873 *name = entry->gr_name;
879 static int name2layout(__u32 *layout, char *name)
884 for (ptr = name; ; ptr = NULL) {
885 lyt = strtok(ptr, ",");
888 if (strcmp(lyt, "released") == 0)
889 *layout |= LOV_PATTERN_F_RELEASED;
890 else if (strcmp(lyt, "raid0") == 0)
891 *layout |= LOV_PATTERN_RAID0;
898 #define FIND_POOL_OPT 3
899 static int lfs_find(int argc, char **argv)
904 struct find_param param = {
908 struct option long_opts[] = {
909 {"atime", required_argument, 0, 'A'},
910 {"stripe-count", required_argument, 0, 'c'},
911 {"stripe_count", required_argument, 0, 'c'},
912 {"ctime", required_argument, 0, 'C'},
913 {"maxdepth", required_argument, 0, 'D'},
914 {"gid", required_argument, 0, 'g'},
915 {"group", required_argument, 0, 'G'},
916 {"stripe-index", required_argument, 0, 'i'},
917 {"stripe_index", required_argument, 0, 'i'},
918 {"layout", required_argument, 0, 'L'},
919 {"mdt", required_argument, 0, 'm'},
920 {"mtime", required_argument, 0, 'M'},
921 {"name", required_argument, 0, 'n'},
922 /* reserve {"or", no_argument, , 0, 'o'}, to match find(1) */
923 {"obd", required_argument, 0, 'O'},
924 {"ost", required_argument, 0, 'O'},
925 /* no short option for pool, p/P already used */
926 {"pool", required_argument, 0, FIND_POOL_OPT},
927 {"print0", no_argument, 0, 'p'},
928 {"print", no_argument, 0, 'P'},
929 {"size", required_argument, 0, 's'},
930 {"stripe-size", required_argument, 0, 'S'},
931 {"stripe_size", required_argument, 0, 'S'},
932 {"type", required_argument, 0, 't'},
933 {"uid", required_argument, 0, 'u'},
934 {"user", required_argument, 0, 'U'},
948 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
949 while ((c = getopt_long_only(argc, argv,
950 "-A:c:C:D:g:G:i:L:m:M:n:O:Ppqrs:S:t:u:U:v",
951 long_opts, NULL)) >= 0) {
956 /* '!' is part of option */
957 /* when getopt_long_only() finds a string which is not
958 * an option nor a known option argument it returns 1
959 * in that case if we already have found pathstart and pathend
960 * (i.e. we have the list of pathnames),
961 * the only supported value is "!"
963 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
964 if (!isoption && pathend != -1) {
965 fprintf(stderr, "err: %s: filename|dirname must either "
966 "precede options or follow options\n",
971 if (!isoption && pathstart == -1)
972 pathstart = optind - 1;
973 if (isoption && pathstart != -1 && pathend == -1)
974 pathend = optind - 2;
980 /* unknown; opt is "!" or path component,
981 * checking done above.
983 if (strcmp(optarg, "!") == 0)
987 xtime = ¶m.fp_atime;
988 xsign = ¶m.fp_asign;
989 param.fp_exclude_atime = !!neg_opt;
990 /* no break, this falls through to 'C' for ctime */
993 xtime = ¶m.fp_ctime;
994 xsign = ¶m.fp_csign;
995 param.fp_exclude_ctime = !!neg_opt;
997 /* no break, this falls through to 'M' for mtime */
1000 xtime = ¶m.fp_mtime;
1001 xsign = ¶m.fp_msign;
1002 param.fp_exclude_mtime = !!neg_opt;
1004 rc = set_time(&t, xtime, optarg);
1005 if (rc == INT_MAX) {
1013 if (optarg[0] == '+') {
1014 param.stripecount_sign = -1;
1016 } else if (optarg[0] == '-') {
1017 param.stripecount_sign = 1;
1021 param.stripecount = strtoul(optarg, &endptr, 0);
1022 if (*endptr != '\0') {
1023 fprintf(stderr,"error: bad stripe_count '%s'\n",
1028 param.check_stripecount = 1;
1029 param.exclude_stripecount = !!neg_opt;
1032 param.fp_max_depth = strtol(optarg, 0, 0);
1036 rc = name2id(¶m.fp_gid, optarg, GROUP);
1038 param.fp_gid = strtoul(optarg, &endptr, 10);
1039 if (*endptr != '\0') {
1040 fprintf(stderr, "Group/GID: %s cannot "
1041 "be found.\n", optarg);
1046 param.fp_exclude_gid = !!neg_opt;
1047 param.fp_check_gid = 1;
1050 ret = name2layout(¶m.layout, optarg);
1053 param.exclude_layout = !!neg_opt;
1054 param.check_layout = 1;
1058 rc = name2id(¶m.fp_uid, optarg, USER);
1060 param.fp_uid = strtoul(optarg, &endptr, 10);
1061 if (*endptr != '\0') {
1062 fprintf(stderr, "User/UID: %s cannot "
1063 "be found.\n", optarg);
1068 param.fp_exclude_uid = !!neg_opt;
1069 param.fp_check_uid = 1;
1072 if (strlen(optarg) > LOV_MAXPOOLNAME) {
1074 "Pool name %s is too long"
1075 " (max is %d)\n", optarg,
1080 /* we do check for empty pool because empty pool
1081 * is used to find V1 lov attributes */
1082 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
1083 param.poolname[LOV_MAXPOOLNAME] = '\0';
1084 param.exclude_pool = !!neg_opt;
1085 param.check_pool = 1;
1088 param.pattern = (char *)optarg;
1089 param.exclude_pattern = !!neg_opt;
1094 char *buf, *token, *next, *p;
1098 buf = strdup(optarg);
1104 param.exclude_obd = !!neg_opt;
1107 while (token && *token) {
1108 token = strchr(token, ',');
1115 param.exclude_mdt = !!neg_opt;
1116 param.num_alloc_mdts += len;
1117 tmp = realloc(param.mdtuuid,
1118 param.num_alloc_mdts *
1119 sizeof(*param.mdtuuid));
1125 param.mdtuuid = tmp;
1127 param.exclude_obd = !!neg_opt;
1128 param.num_alloc_obds += len;
1129 tmp = realloc(param.obduuid,
1130 param.num_alloc_obds *
1131 sizeof(*param.obduuid));
1137 param.obduuid = tmp;
1139 for (token = buf; token && *token; token = next) {
1140 struct obd_uuid *puuid;
1143 ¶m.mdtuuid[param.num_mdts++];
1146 ¶m.obduuid[param.num_obds++];
1148 p = strchr(token, ',');
1155 if (strlen(token) > sizeof(puuid->uuid) - 1) {
1160 strncpy(puuid->uuid, token,
1161 sizeof(puuid->uuid));
1174 if (optarg[0] == '+') {
1175 param.size_sign = -1;
1177 } else if (optarg[0] == '-') {
1178 param.size_sign = 1;
1182 ret = llapi_parse_size(optarg, ¶m.size,
1183 ¶m.size_units, 0);
1185 fprintf(stderr, "error: bad file size '%s'\n",
1189 param.check_size = 1;
1190 param.exclude_size = !!neg_opt;
1193 if (optarg[0] == '+') {
1194 param.stripesize_sign = -1;
1196 } else if (optarg[0] == '-') {
1197 param.stripesize_sign = 1;
1201 ret = llapi_parse_size(optarg, ¶m.stripesize,
1202 ¶m.stripesize_units, 0);
1204 fprintf(stderr, "error: bad stripe_size '%s'\n",
1208 param.check_stripesize = 1;
1209 param.exclude_stripesize = !!neg_opt;
1212 param.fp_exclude_type = !!neg_opt;
1213 switch (optarg[0]) {
1215 param.fp_type = S_IFBLK;
1218 param.fp_type = S_IFCHR;
1221 param.fp_type = S_IFDIR;
1224 param.fp_type = S_IFREG;
1227 param.fp_type = S_IFLNK;
1230 param.fp_type = S_IFIFO;
1233 param.fp_type = S_IFSOCK;
1236 fprintf(stderr, "error: %s: bad type '%s'\n",
1248 if (pathstart == -1) {
1249 fprintf(stderr, "error: %s: no filename|pathname\n",
1253 } else if (pathend == -1) {
1259 rc = llapi_find(argv[pathstart], ¶m);
1260 if (rc != 0 && ret == 0)
1262 } while (++pathstart < pathend);
1265 fprintf(stderr, "error: %s failed for %s.\n",
1266 argv[0], argv[optind - 1]);
1268 if (param.obduuid && param.num_alloc_obds)
1269 free(param.obduuid);
1271 if (param.mdtuuid && param.num_alloc_mdts)
1272 free(param.mdtuuid);
1277 static int lfs_getstripe_internal(int argc, char **argv,
1278 struct find_param *param)
1280 struct option long_opts[] = {
1281 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1282 /* This formerly implied "stripe-count", but was explicitly
1283 * made "stripe-count" for consistency with other options,
1284 * and to separate it from "mdt-count" when DNE arrives. */
1285 {"count", no_argument, 0, 'c'},
1287 {"stripe-count", no_argument, 0, 'c'},
1288 {"stripe_count", no_argument, 0, 'c'},
1289 {"directory", no_argument, 0, 'd'},
1290 {"default", no_argument, 0, 'D'},
1291 {"generation", no_argument, 0, 'g'},
1292 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1293 /* This formerly implied "stripe-index", but was explicitly
1294 * made "stripe-index" for consistency with other options,
1295 * and to separate it from "mdt-index" when DNE arrives. */
1296 {"index", no_argument, 0, 'i'},
1298 {"stripe-index", no_argument, 0, 'i'},
1299 {"stripe_index", no_argument, 0, 'i'},
1300 {"layout", no_argument, 0, 'L'},
1301 {"mdt-index", no_argument, 0, 'M'},
1302 {"mdt_index", no_argument, 0, 'M'},
1303 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1304 /* This formerly implied "stripe-index", but was confusing
1305 * with "file offset" (which will eventually be needed for
1306 * with different layouts by offset), so deprecate it. */
1307 {"offset", no_argument, 0, 'o'},
1309 {"obd", required_argument, 0, 'O'},
1310 {"ost", required_argument, 0, 'O'},
1311 {"pool", no_argument, 0, 'p'},
1312 {"quiet", no_argument, 0, 'q'},
1313 {"recursive", no_argument, 0, 'r'},
1314 {"raw", no_argument, 0, 'R'},
1315 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1316 /* This formerly implied "--stripe-size", but was confusing
1317 * with "lfs find --size|-s", which means "file size", so use
1318 * the consistent "--stripe-size|-S" for all commands. */
1319 {"size", no_argument, 0, 's'},
1321 {"stripe-size", no_argument, 0, 'S'},
1322 {"stripe_size", no_argument, 0, 'S'},
1323 {"verbose", no_argument, 0, 'v'},
1328 param->fp_max_depth = 1;
1330 while ((c = getopt_long(argc, argv, "cdDghiLMoO:pqrRsSv",
1331 long_opts, NULL)) != -1) {
1334 if (param->obduuid) {
1336 "error: %s: only one obduuid allowed",
1340 param->obduuid = (struct obd_uuid *)optarg;
1346 param->fp_max_depth = 0;
1349 param->get_default_lmv = 1;
1352 param->recursive = 1;
1355 param->verbose = VERBOSE_ALL | VERBOSE_DETAIL;
1358 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1359 if (strcmp(argv[optind - 1], "--count") == 0)
1360 fprintf(stderr, "warning: '--count' deprecated,"
1361 " use '--stripe-count' instead\n");
1363 if (!(param->verbose & VERBOSE_DETAIL)) {
1364 param->verbose |= VERBOSE_COUNT;
1365 param->fp_max_depth = 0;
1368 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1370 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1371 fprintf(stderr, "warning: '--size|-s' deprecated, "
1372 "use '--stripe-size|-S' instead\n");
1374 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0) */
1376 if (!(param->verbose & VERBOSE_DETAIL)) {
1377 param->verbose |= VERBOSE_SIZE;
1378 param->fp_max_depth = 0;
1381 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1383 fprintf(stderr, "warning: '--offset|-o' deprecated, "
1384 "use '--stripe-index|-i' instead\n");
1387 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1388 if (strcmp(argv[optind - 1], "--index") == 0)
1389 fprintf(stderr, "warning: '--index' deprecated"
1390 ", use '--stripe-index' instead\n");
1392 if (!(param->verbose & VERBOSE_DETAIL)) {
1393 param->verbose |= VERBOSE_OFFSET;
1394 param->fp_max_depth = 0;
1398 if (!(param->verbose & VERBOSE_DETAIL)) {
1399 param->verbose |= VERBOSE_POOL;
1400 param->fp_max_depth = 0;
1404 if (!(param->verbose & VERBOSE_DETAIL)) {
1405 param->verbose |= VERBOSE_GENERATION;
1406 param->fp_max_depth = 0;
1410 if (!(param->verbose & VERBOSE_DETAIL)) {
1411 param->verbose |= VERBOSE_LAYOUT;
1412 param->fp_max_depth = 0;
1416 if (!(param->verbose & VERBOSE_DETAIL))
1417 param->fp_max_depth = 0;
1418 param->verbose |= VERBOSE_MDTINDEX;
1431 if (param->recursive)
1432 param->fp_max_depth = -1;
1434 if (!param->verbose)
1435 param->verbose = VERBOSE_ALL;
1437 param->verbose = VERBOSE_OBJID;
1440 rc = llapi_getstripe(argv[optind], param);
1441 } while (++optind < argc && !rc);
1444 fprintf(stderr, "error: %s failed for %s.\n",
1445 argv[0], argv[optind - 1]);
1449 static int lfs_tgts(int argc, char **argv)
1451 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
1452 struct find_param param;
1453 int index = 0, rc=0;
1458 if (argc == 2 && !realpath(argv[1], path)) {
1460 fprintf(stderr, "error: invalid path '%s': %s\n",
1461 argv[1], strerror(-rc));
1465 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
1466 /* Check if we have a mount point */
1467 if (mntdir[0] == '\0')
1470 memset(¶m, 0, sizeof(param));
1471 if (!strcmp(argv[0], "mdts"))
1474 rc = llapi_ostlist(mntdir, ¶m);
1476 fprintf(stderr, "error: %s: failed on %s\n",
1479 if (path[0] != '\0')
1481 memset(mntdir, 0, PATH_MAX);
1487 static int lfs_getstripe(int argc, char **argv)
1489 struct find_param param = { 0 };
1490 return lfs_getstripe_internal(argc, argv, ¶m);
1494 static int lfs_getdirstripe(int argc, char **argv)
1496 struct find_param param = { 0 };
1499 return lfs_getstripe_internal(argc, argv, ¶m);
1503 static int lfs_setdirstripe(int argc, char **argv)
1507 unsigned int stripe_offset = -1;
1508 unsigned int stripe_count = 1;
1509 enum lmv_hash_type hash_type;
1512 char *stripe_offset_opt = NULL;
1513 char *stripe_count_opt = NULL;
1514 char *stripe_hash_opt = NULL;
1515 char *mode_opt = NULL;
1516 int default_stripe = 0;
1517 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
1518 mode_t previous_mode = 0;
1520 struct option long_opts[] = {
1521 {"count", required_argument, 0, 'c'},
1522 {"index", required_argument, 0, 'i'},
1523 {"mode", required_argument, 0, 'm'},
1524 {"hash-type", required_argument, 0, 't'},
1525 {"default_stripe", no_argument, 0, 'D'},
1531 while ((c = getopt_long(argc, argv, "c:Di:m:t:", long_opts,
1538 stripe_count_opt = optarg;
1544 stripe_offset_opt = optarg;
1550 stripe_hash_opt = optarg;
1553 fprintf(stderr, "error: %s: option '%s' "
1555 argv[0], argv[optind - 1]);
1560 if (optind == argc) {
1561 fprintf(stderr, "error: %s: missing dirname\n",
1566 if (stripe_offset_opt == NULL && stripe_count_opt == NULL) {
1567 fprintf(stderr, "error: %s: missing stripe offset and count.\n",
1572 if (stripe_offset_opt != NULL) {
1573 /* get the stripe offset */
1574 stripe_offset = strtoul(stripe_offset_opt, &end, 0);
1576 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
1577 argv[0], stripe_offset_opt);
1582 if (mode_opt != NULL) {
1583 mode = strtoul(mode_opt, &end, 8);
1585 fprintf(stderr, "error: %s: bad mode '%s'\n",
1589 previous_mode = umask(0);
1592 if (stripe_hash_opt == NULL ||
1593 strcmp(stripe_hash_opt, LMV_HASH_NAME_FNV_1A_64) == 0) {
1594 hash_type = LMV_HASH_TYPE_FNV_1A_64;
1595 } else if (strcmp(stripe_hash_opt, LMV_HASH_NAME_ALL_CHARS) == 0) {
1596 hash_type = LMV_HASH_TYPE_ALL_CHARS;
1598 fprintf(stderr, "error: %s: bad stripe hash type '%s'\n",
1599 argv[0], stripe_hash_opt);
1603 /* get the stripe count */
1604 if (stripe_count_opt != NULL) {
1605 stripe_count = strtoul(stripe_count_opt, &end, 0);
1607 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
1608 argv[0], stripe_count_opt);
1613 dname = argv[optind];
1615 if (default_stripe == 1) {
1616 result = llapi_dir_set_default_lmv_stripe(dname,
1617 stripe_offset, stripe_count,
1620 result = llapi_dir_create_pool(dname, mode,
1622 stripe_count, hash_type,
1627 fprintf(stderr, "error: %s: create stripe dir '%s' "
1628 "failed\n", argv[0], dname);
1631 dname = argv[++optind];
1632 } while (dname != NULL);
1634 if (mode_opt != NULL)
1635 umask(previous_mode);
1641 static int lfs_rmentry(int argc, char **argv)
1648 fprintf(stderr, "error: %s: missing dirname\n",
1654 dname = argv[index];
1655 while (dname != NULL) {
1656 result = llapi_direntry_remove(dname);
1658 fprintf(stderr, "error: %s: remove dir entry '%s' "
1659 "failed\n", argv[0], dname);
1662 dname = argv[++index];
1667 static int lfs_mv(int argc, char **argv)
1669 struct find_param param = {
1676 struct option long_opts[] = {
1677 {"--mdt-index", required_argument, 0, 'M'},
1678 {"verbose", no_argument, 0, 'v'},
1682 while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
1685 param.mdtindex = strtoul(optarg, &end, 0);
1687 fprintf(stderr, "%s: invalid MDT index'%s'\n",
1694 param.verbose = VERBOSE_DETAIL;
1698 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
1699 argv[0], argv[optind - 1]);
1704 if (param.mdtindex == -1) {
1705 fprintf(stderr, "%s MDT index must be indicated\n", argv[0]);
1709 if (optind >= argc) {
1710 fprintf(stderr, "%s missing operand path\n", argv[0]);
1715 rc = llapi_mv(argv[optind], ¶m);
1717 fprintf(stderr, "cannot migrate '%s' to MDT%04x: %s\n",
1718 argv[optind], param.mdtindex, strerror(-rc));
1722 static int lfs_osts(int argc, char **argv)
1724 return lfs_tgts(argc, argv);
1727 static int lfs_mdts(int argc, char **argv)
1729 return lfs_tgts(argc, argv);
1732 #define COOK(value) \
1735 while (value > 1024) { \
1743 #define CDF "%11llu"
1744 #define HDF "%8.1f%c"
1748 static int showdf(char *mntdir, struct obd_statfs *stat,
1749 char *uuid, int ishow, int cooked,
1750 char *type, int index, int rc)
1752 long long avail, used, total;
1754 char *suffix = "KMGTPEZY";
1755 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
1756 char tbuf[3 * sizeof(__u64)];
1757 char ubuf[3 * sizeof(__u64)];
1758 char abuf[3 * sizeof(__u64)];
1759 char rbuf[3 * sizeof(__u64)];
1767 avail = stat->os_ffree;
1768 used = stat->os_files - stat->os_ffree;
1769 total = stat->os_files;
1771 int shift = cooked ? 0 : 10;
1773 avail = (stat->os_bavail * stat->os_bsize) >> shift;
1774 used = ((stat->os_blocks - stat->os_bfree) *
1775 stat->os_bsize) >> shift;
1776 total = (stat->os_blocks * stat->os_bsize) >> shift;
1779 if ((used + avail) > 0)
1780 ratio = (double)used / (double)(used + avail);
1786 cook_val = (double)total;
1789 sprintf(tbuf, HDF, cook_val, suffix[i - 1]);
1791 sprintf(tbuf, CDF, total);
1793 cook_val = (double)used;
1796 sprintf(ubuf, HDF, cook_val, suffix[i - 1]);
1798 sprintf(ubuf, CDF, used);
1800 cook_val = (double)avail;
1803 sprintf(abuf, HDF, cook_val, suffix[i - 1]);
1805 sprintf(abuf, CDF, avail);
1807 sprintf(tbuf, CDF, total);
1808 sprintf(ubuf, CDF, used);
1809 sprintf(abuf, CDF, avail);
1812 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
1813 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
1814 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
1816 printf("[%s:%d]\n", type, index);
1822 printf(UUF": inactive device\n", uuid);
1825 printf(UUF": %s\n", uuid, strerror(-rc));
1832 struct ll_stat_type {
1837 static int mntdf(char *mntdir, char *fsname, char *pool, int ishow,
1838 int cooked, int lazy)
1840 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1841 struct obd_uuid uuid_buf;
1842 char *poolname = NULL;
1843 struct ll_stat_type types[] = { { LL_STATFS_LMV, "MDT" },
1844 { LL_STATFS_LOV, "OST" },
1846 struct ll_stat_type *tp;
1847 __u64 ost_ffree = 0;
1853 poolname = strchr(pool, '.');
1854 if (poolname != NULL) {
1855 if (strncmp(fsname, pool, strlen(fsname))) {
1856 fprintf(stderr, "filesystem name incorrect\n");
1865 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1866 "UUID", "Inodes", "IUsed", "IFree",
1867 "IUse%", "Mounted on");
1869 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1870 "UUID", cooked ? "bytes" : "1K-blocks",
1871 "Used", "Available", "Use%", "Mounted on");
1873 for (tp = types; tp->st_name != NULL; tp++) {
1874 for (index = 0; ; index++) {
1875 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1876 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1877 type = lazy ? tp->st_op | LL_STATFS_NODELAY : tp->st_op;
1878 rc = llapi_obd_statfs(mntdir, type, index,
1879 &stat_buf, &uuid_buf);
1883 if (poolname && tp->st_op == LL_STATFS_LOV &&
1884 llapi_search_ost(fsname, poolname,
1885 obd_uuid2str(&uuid_buf)) != 1)
1888 /* the llapi_obd_statfs() call may have returned with
1889 * an error, but if it filled in uuid_buf we will at
1890 * lease use that to print out a message for that OBD.
1891 * If we didn't get anything in the uuid_buf, then fill
1892 * it in so that we can print an error message. */
1893 if (uuid_buf.uuid[0] == '\0')
1894 sprintf(uuid_buf.uuid, "%s%04x",
1895 tp->st_name, index);
1896 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1897 ishow, cooked, tp->st_name, index, rc);
1900 if (tp->st_op == LL_STATFS_LMV) {
1901 sum.os_ffree += stat_buf.os_ffree;
1902 sum.os_files += stat_buf.os_files;
1903 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
1904 sum.os_blocks += stat_buf.os_blocks *
1906 sum.os_bfree += stat_buf.os_bfree *
1908 sum.os_bavail += stat_buf.os_bavail *
1910 ost_ffree += stat_buf.os_ffree;
1912 } else if (rc == -EINVAL || rc == -EFAULT) {
1918 /* If we don't have as many objects free on the OST as inodes
1919 * on the MDS, we reduce the total number of inodes to
1920 * compensate, so that the "inodes in use" number is correct.
1921 * Matches ll_statfs_internal() so the results are consistent. */
1922 if (ost_ffree < sum.os_ffree) {
1923 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
1924 sum.os_ffree = ost_ffree;
1927 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0, 0);
1932 static int lfs_df(int argc, char **argv)
1934 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
1935 int ishow = 0, cooked = 0;
1937 int c, rc = 0, index = 0;
1938 char fsname[PATH_MAX] = "", *pool_name = NULL;
1939 struct option long_opts[] = {
1940 {"pool", required_argument, 0, 'p'},
1941 {"lazy", 0, 0, 'l'},
1946 while ((c = getopt_long(argc, argv, "hilp:", long_opts, NULL)) != -1) {
1964 if (optind < argc && !realpath(argv[optind], path)) {
1966 fprintf(stderr, "error: invalid path '%s': %s\n",
1967 argv[optind], strerror(-rc));
1971 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
1972 /* Check if we have a mount point */
1973 if (mntdir[0] == '\0')
1976 rc = mntdf(mntdir, fsname, pool_name, ishow, cooked, lazy);
1977 if (rc || path[0] != '\0')
1979 fsname[0] = '\0'; /* avoid matching in next loop */
1980 mntdir[0] = '\0'; /* avoid matching in next loop */
1986 static int lfs_getname(int argc, char **argv)
1988 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
1989 int rc = 0, index = 0, c;
1990 char buf[sizeof(struct obd_uuid)];
1993 while ((c = getopt(argc, argv, "h")) != -1)
1996 if (optind == argc) { /* no paths specified, get all paths. */
1997 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
1998 rc = llapi_getname(mntdir, buf, sizeof(buf));
2001 "cannot get name for `%s': %s\n",
2002 mntdir, strerror(-rc));
2006 printf("%s %s\n", buf, mntdir);
2008 path[0] = fsname[0] = mntdir[0] = 0;
2010 } else { /* paths specified, only attempt to search these. */
2011 for (; optind < argc; optind++) {
2012 rc = llapi_getname(argv[optind], buf, sizeof(buf));
2015 "cannot get name for `%s': %s\n",
2016 argv[optind], strerror(-rc));
2020 printf("%s %s\n", buf, argv[optind]);
2026 static int lfs_check(int argc, char **argv)
2029 char mntdir[PATH_MAX] = {'\0'};
2038 obd_types[0] = obd_type1;
2039 obd_types[1] = obd_type2;
2041 if (strcmp(argv[1], "osts") == 0) {
2042 strcpy(obd_types[0], "osc");
2043 } else if (strcmp(argv[1], "mds") == 0) {
2044 strcpy(obd_types[0], "mdc");
2045 } else if (strcmp(argv[1], "servers") == 0) {
2047 strcpy(obd_types[0], "osc");
2048 strcpy(obd_types[1], "mdc");
2050 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2055 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
2056 if (rc < 0 || mntdir[0] == '\0') {
2057 fprintf(stderr, "No suitable Lustre mount found\n");
2061 rc = llapi_target_check(num_types, obd_types, mntdir);
2063 fprintf(stderr, "error: %s: %s status failed\n",
2070 static int lfs_join(int argc, char **argv)
2072 fprintf(stderr, "join two lustre files into one.\n"
2073 "obsolete, HEAD does not support it anymore.\n");
2077 #ifdef HAVE_SYS_QUOTA_H
2078 static int lfs_quotacheck(int argc, char **argv)
2080 int c, check_type = 0;
2082 struct if_quotacheck qchk;
2083 struct if_quotactl qctl;
2084 char *obd_type = (char *)qchk.obd_type;
2087 memset(&qchk, 0, sizeof(qchk));
2090 while ((c = getopt(argc, argv, "gu")) != -1) {
2099 fprintf(stderr, "error: %s: option '-%c' "
2100 "unrecognized\n", argv[0], c);
2107 else /* do quotacheck for both user & group quota by default */
2115 rc = llapi_quotacheck(mnt, check_type);
2116 if (rc == -EOPNOTSUPP) {
2117 fprintf(stderr, "error: quotacheck not supported by the quota "
2118 "master.\nPlease note that quotacheck is deprecated as "
2119 "of lustre 2.4.0 since space accounting is always "
2120 "enabled.\nFilesystems not formatted with 2.4 utils or "
2121 "beyond can be upgraded with tunefs.lustre --quota.\n");
2124 fprintf(stderr, "quotacheck failed: %s\n", strerror(-rc));
2128 rc = llapi_poll_quotacheck(mnt, &qchk);
2131 fprintf(stderr, "%s %s ", obd_type,
2132 obd_uuid2str(&qchk.obd_uuid));
2133 fprintf(stderr, "quota check failed: %s\n", strerror(-rc));
2137 memset(&qctl, 0, sizeof(qctl));
2138 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
2139 qctl.qc_type = check_type;
2140 rc = llapi_quotactl(mnt, &qctl);
2141 if (rc && rc != -EALREADY) {
2143 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
2144 obd_uuid2str(&qctl.obd_uuid));
2145 fprintf(stderr, "%s turn on quota failed: %s\n",
2146 argv[0], strerror(-rc));
2153 static int lfs_quotaon(int argc, char **argv)
2157 struct if_quotactl qctl;
2158 char *obd_type = (char *)qctl.obd_type;
2161 memset(&qctl, 0, sizeof(qctl));
2162 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
2165 while ((c = getopt(argc, argv, "fgu")) != -1) {
2168 qctl.qc_type |= 0x01;
2171 qctl.qc_type |= 0x02;
2174 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
2177 fprintf(stderr, "error: %s: option '-%c' "
2178 "unrecognized\n", argv[0], c);
2185 else /* by default, enable quota for both user & group */
2186 qctl.qc_type = 0x02;
2193 rc = llapi_quotactl(mnt, &qctl);
2195 if (rc == -EOPNOTSUPP) {
2196 fprintf(stderr, "error: quotaon not supported by the "
2197 "quota master.\nPlease note that quotaon/off is"
2198 " deprecated as of lustre 2.4.0.\nQuota "
2199 "enforcement should now be enabled on the MGS "
2200 "via:\nmgs# lctl conf_param ${FSNAME}.quota."
2201 "<ost|mdt>=<u|g|ug>\n(ost for block quota, mdt "
2202 "for inode quota, u for user and g for group"
2204 } else if (rc == -EALREADY) {
2206 } else if (rc == -ENOENT) {
2207 fprintf(stderr, "error: cannot find quota database, "
2208 "make sure you have run quotacheck\n");
2211 fprintf(stderr, "%s %s ", obd_type,
2212 obd_uuid2str(&qctl.obd_uuid));
2213 fprintf(stderr, "%s failed: %s\n", argv[0],
2221 static int lfs_quotaoff(int argc, char **argv)
2225 struct if_quotactl qctl;
2226 char *obd_type = (char *)qctl.obd_type;
2229 memset(&qctl, 0, sizeof(qctl));
2230 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
2233 while ((c = getopt(argc, argv, "gu")) != -1) {
2236 qctl.qc_type |= 0x01;
2239 qctl.qc_type |= 0x02;
2242 fprintf(stderr, "error: %s: option '-%c' "
2243 "unrecognized\n", argv[0], c);
2250 else /* by default, disable quota for both user & group */
2251 qctl.qc_type = 0x02;
2258 rc = llapi_quotactl(mnt, &qctl);
2260 if (rc == -EOPNOTSUPP) {
2261 fprintf(stderr, "error: quotaoff not supported by the "
2262 "quota master.\nPlease note that quotaon/off is"
2263 " deprecated as of lustre 2.4.0.\nQuota "
2264 "enforcement can be disabled on the MGS via:\n"
2265 "mgs# lctl conf_param ${FSNAME}.quota.<ost|mdt>"
2267 } else if (rc == -EALREADY) {
2271 fprintf(stderr, "%s %s ", obd_type,
2272 obd_uuid2str(&qctl.obd_uuid));
2273 fprintf(stderr, "quotaoff failed: %s\n",
2281 #define ARG2INT(nr, str, msg) \
2284 nr = strtol(str, &endp, 0); \
2286 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
2291 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
2293 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
2294 * returns the value or ULONG_MAX on integer overflow or incorrect format
2296 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
2297 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
2298 * 3. empty integer value is interpreted as 0
2300 static unsigned long str2sec(const char* timestr)
2302 const char spec[] = "smhdw";
2303 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
2304 unsigned long val = 0;
2307 if (strpbrk(timestr, spec) == NULL) {
2308 /* no specifiers inside the time string,
2309 should treat it as an integer value */
2310 val = strtoul(timestr, &tail, 10);
2311 return *tail ? ULONG_MAX : val;
2314 /* format string is XXwXXdXXhXXmXXs */
2320 v = strtoul(timestr, &tail, 10);
2321 if (v == ULONG_MAX || *tail == '\0')
2322 /* value too large (ULONG_MAX or more)
2323 or missing specifier */
2326 ptr = strchr(spec, *tail);
2328 /* unknown specifier */
2333 /* check if product will overflow the type */
2334 if (!(v < ULONG_MAX / mult[ind]))
2337 ADD_OVERFLOW(val, mult[ind] * v);
2338 if (val == ULONG_MAX)
2350 #define ARG2ULL(nr, str, def_units) \
2352 unsigned long long limit, units = def_units; \
2355 rc = llapi_parse_size(str, &limit, &units, 1); \
2357 fprintf(stderr, "error: bad limit value %s\n", str); \
2363 static inline int has_times_option(int argc, char **argv)
2367 for (i = 1; i < argc; i++)
2368 if (!strcmp(argv[i], "-t"))
2374 int lfs_setquota_times(int argc, char **argv)
2377 struct if_quotactl qctl;
2378 char *mnt, *obd_type = (char *)qctl.obd_type;
2379 struct obd_dqblk *dqb = &qctl.qc_dqblk;
2380 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
2381 struct option long_opts[] = {
2382 {"block-grace", required_argument, 0, 'b'},
2383 {"group", no_argument, 0, 'g'},
2384 {"inode-grace", required_argument, 0, 'i'},
2385 {"times", no_argument, 0, 't'},
2386 {"user", no_argument, 0, 'u'},
2390 memset(&qctl, 0, sizeof(qctl));
2391 qctl.qc_cmd = LUSTRE_Q_SETINFO;
2392 qctl.qc_type = UGQUOTA;
2395 while ((c = getopt_long(argc, argv, "b:gi:tu", long_opts, NULL)) != -1) {
2399 if (qctl.qc_type != UGQUOTA) {
2400 fprintf(stderr, "error: -u and -g can't be used "
2401 "more than once\n");
2404 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
2407 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
2408 fprintf(stderr, "error: bad block-grace: %s\n",
2412 dqb->dqb_valid |= QIF_BTIME;
2415 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
2416 fprintf(stderr, "error: bad inode-grace: %s\n",
2420 dqb->dqb_valid |= QIF_ITIME;
2422 case 't': /* Yes, of course! */
2424 default: /* getopt prints error message for us when opterr != 0 */
2429 if (qctl.qc_type == UGQUOTA) {
2430 fprintf(stderr, "error: neither -u nor -g specified\n");
2434 if (optind != argc - 1) {
2435 fprintf(stderr, "error: unexpected parameters encountered\n");
2440 rc = llapi_quotactl(mnt, &qctl);
2443 fprintf(stderr, "%s %s ", obd_type,
2444 obd_uuid2str(&qctl.obd_uuid));
2445 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
2452 #define BSLIMIT (1 << 0)
2453 #define BHLIMIT (1 << 1)
2454 #define ISLIMIT (1 << 2)
2455 #define IHLIMIT (1 << 3)
2457 int lfs_setquota(int argc, char **argv)
2460 struct if_quotactl qctl;
2461 char *mnt, *obd_type = (char *)qctl.obd_type;
2462 struct obd_dqblk *dqb = &qctl.qc_dqblk;
2463 struct option long_opts[] = {
2464 {"block-softlimit", required_argument, 0, 'b'},
2465 {"block-hardlimit", required_argument, 0, 'B'},
2466 {"group", required_argument, 0, 'g'},
2467 {"inode-softlimit", required_argument, 0, 'i'},
2468 {"inode-hardlimit", required_argument, 0, 'I'},
2469 {"user", required_argument, 0, 'u'},
2472 unsigned limit_mask = 0;
2475 if (has_times_option(argc, argv))
2476 return lfs_setquota_times(argc, argv);
2478 memset(&qctl, 0, sizeof(qctl));
2479 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
2480 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
2481 * so it can be used as a marker that qc_type
2482 * isn't reinitialized from command line */
2485 while ((c = getopt_long(argc, argv, "b:B:g:i:I:u:", long_opts, NULL)) != -1) {
2489 if (qctl.qc_type != UGQUOTA) {
2490 fprintf(stderr, "error: -u and -g can't be used"
2491 " more than once\n");
2494 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
2495 rc = name2id(&qctl.qc_id, optarg,
2496 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2498 qctl.qc_id = strtoul(optarg, &endptr, 10);
2499 if (*endptr != '\0') {
2500 fprintf(stderr, "error: can't find id "
2501 "for name %s\n", optarg);
2507 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
2508 dqb->dqb_bsoftlimit >>= 10;
2509 limit_mask |= BSLIMIT;
2510 if (dqb->dqb_bsoftlimit &&
2511 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
2512 fprintf(stderr, "warning: block softlimit is "
2513 "smaller than the miminal qunit size, "
2514 "please see the help of setquota or "
2515 "Lustre manual for details.\n");
2518 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
2519 dqb->dqb_bhardlimit >>= 10;
2520 limit_mask |= BHLIMIT;
2521 if (dqb->dqb_bhardlimit &&
2522 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
2523 fprintf(stderr, "warning: block hardlimit is "
2524 "smaller than the miminal qunit size, "
2525 "please see the help of setquota or "
2526 "Lustre manual for details.\n");
2529 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
2530 limit_mask |= ISLIMIT;
2531 if (dqb->dqb_isoftlimit &&
2532 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
2533 fprintf(stderr, "warning: inode softlimit is "
2534 "smaller than the miminal qunit size, "
2535 "please see the help of setquota or "
2536 "Lustre manual for details.\n");
2539 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
2540 limit_mask |= IHLIMIT;
2541 if (dqb->dqb_ihardlimit &&
2542 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
2543 fprintf(stderr, "warning: inode hardlimit is "
2544 "smaller than the miminal qunit size, "
2545 "please see the help of setquota or "
2546 "Lustre manual for details.\n");
2548 default: /* getopt prints error message for us when opterr != 0 */
2553 if (qctl.qc_type == UGQUOTA) {
2554 fprintf(stderr, "error: neither -u nor -g was specified\n");
2558 if (limit_mask == 0) {
2559 fprintf(stderr, "error: at least one limit must be specified\n");
2563 if (optind != argc - 1) {
2564 fprintf(stderr, "error: unexpected parameters encountered\n");
2570 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
2571 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
2572 /* sigh, we can't just set blimits/ilimits */
2573 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
2574 .qc_type = qctl.qc_type,
2575 .qc_id = qctl.qc_id};
2577 rc = llapi_quotactl(mnt, &tmp_qctl);
2579 fprintf(stderr, "error: setquota failed while retrieving"
2580 " current quota settings (%s)\n",
2585 if (!(limit_mask & BHLIMIT))
2586 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
2587 if (!(limit_mask & BSLIMIT))
2588 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
2589 if (!(limit_mask & IHLIMIT))
2590 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
2591 if (!(limit_mask & ISLIMIT))
2592 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
2594 /* Keep grace times if we have got no softlimit arguments */
2595 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
2596 dqb->dqb_valid |= QIF_BTIME;
2597 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
2600 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
2601 dqb->dqb_valid |= QIF_ITIME;
2602 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
2606 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
2607 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
2609 rc = llapi_quotactl(mnt, &qctl);
2612 fprintf(stderr, "%s %s ", obd_type,
2613 obd_uuid2str(&qctl.obd_uuid));
2614 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
2621 static inline char *type2name(int check_type)
2623 if (check_type == USRQUOTA)
2625 else if (check_type == GRPQUOTA)
2631 /* Converts seconds value into format string
2632 * result is returned in buf
2634 * 1. result is in descenting order: 1w2d3h4m5s
2635 * 2. zero fields are not filled (except for p. 3): 5d1s
2636 * 3. zero seconds value is presented as "0s"
2638 static char * __sec2str(time_t seconds, char *buf)
2640 const char spec[] = "smhdw";
2641 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
2646 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
2647 c = seconds / mult[i];
2649 if (c > 0 || (i == 0 && buf == tail))
2650 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
2658 static void sec2str(time_t seconds, char *buf, int rc)
2665 tail = __sec2str(seconds, tail);
2667 if (rc && tail - buf < 39) {
2673 static void diff2str(time_t seconds, char *buf, time_t now)
2679 if (seconds <= now) {
2680 strcpy(buf, "none");
2683 __sec2str(seconds - now, buf);
2686 static void print_quota_title(char *name, struct if_quotactl *qctl,
2687 bool human_readable)
2689 printf("Disk quotas for %s %s (%cid %u):\n",
2690 type2name(qctl->qc_type), name,
2691 *type2name(qctl->qc_type), qctl->qc_id);
2692 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
2693 "Filesystem", human_readable ? "used" : "kbytes",
2694 "quota", "limit", "grace",
2695 "files", "quota", "limit", "grace");
2698 static void kbytes2str(__u64 num, char *buf, bool h)
2701 sprintf(buf, LPU64, num);
2704 sprintf(buf, "%5.4gT", (double)num / (1 << 30));
2706 sprintf(buf, "%5.4gG", (double)num / (1 << 20));
2708 sprintf(buf, "%5.4gM", (double)num / (1 << 10));
2710 sprintf(buf, LPU64"%s", num, "k");
2714 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
2721 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
2722 int bover = 0, iover = 0;
2723 struct obd_dqblk *dqb = &qctl->qc_dqblk;
2728 if (dqb->dqb_bhardlimit &&
2729 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
2731 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
2732 if (dqb->dqb_btime > now) {
2739 if (dqb->dqb_ihardlimit &&
2740 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
2742 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
2743 if (dqb->dqb_itime > now) {
2751 if (strlen(mnt) > 15)
2752 printf("%s\n%15s", mnt, "");
2754 printf("%15s", mnt);
2757 diff2str(dqb->dqb_btime, timebuf, now);
2759 kbytes2str(lustre_stoqb(dqb->dqb_curspace), strbuf, h);
2760 if (rc == -EREMOTEIO)
2761 sprintf(numbuf[0], "%s*", strbuf);
2763 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
2764 "%s" : "[%s]", strbuf);
2766 kbytes2str(dqb->dqb_bsoftlimit, strbuf, h);
2767 if (type == QC_GENERAL)
2768 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
2769 "%s" : "[%s]", strbuf);
2771 sprintf(numbuf[1], "%s", "-");
2773 kbytes2str(dqb->dqb_bhardlimit, strbuf, h);
2774 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
2775 "%s" : "[%s]", strbuf);
2777 printf(" %7s%c %6s %7s %7s",
2778 numbuf[0], bover ? '*' : ' ', numbuf[1],
2779 numbuf[2], bover > 1 ? timebuf : "-");
2782 diff2str(dqb->dqb_itime, timebuf, now);
2784 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2785 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2787 if (type == QC_GENERAL)
2788 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
2789 LPU64 : "["LPU64"]", dqb->dqb_isoftlimit);
2791 sprintf(numbuf[1], "%s", "-");
2793 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2794 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2796 if (type != QC_OSTIDX)
2797 printf(" %7s%c %6s %7s %7s",
2798 numbuf[0], iover ? '*' : ' ', numbuf[1],
2799 numbuf[2], iover > 1 ? timebuf : "-");
2801 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
2804 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2805 qctl->qc_cmd == Q_GETOINFO) {
2809 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
2810 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
2811 printf("Block grace time: %s; Inode grace time: %s\n",
2812 bgtimebuf, igtimebuf);
2816 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
2817 bool h, __u64 *total)
2819 int rc = 0, rc1 = 0, count = 0;
2820 __u32 valid = qctl->qc_valid;
2822 rc = llapi_get_obd_count(mnt, &count, is_mdt);
2824 fprintf(stderr, "can not get %s count: %s\n",
2825 is_mdt ? "mdt": "ost", strerror(-rc));
2829 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2830 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2831 rc = llapi_quotactl(mnt, qctl);
2833 /* It is remote client case. */
2834 if (-rc == EOPNOTSUPP) {
2841 fprintf(stderr, "quotactl %s%d failed.\n",
2842 is_mdt ? "mdt": "ost", qctl->qc_idx);
2846 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
2847 qctl->qc_valid, 0, h);
2848 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
2849 qctl->qc_dqblk.dqb_bhardlimit;
2852 qctl->qc_valid = valid;
2856 static int lfs_quota(int argc, char **argv)
2859 char *mnt, *name = NULL;
2860 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2861 .qc_type = UGQUOTA };
2862 char *obd_type = (char *)qctl.obd_type;
2863 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2864 int rc, rc1 = 0, rc2 = 0, rc3 = 0,
2865 verbose = 0, pass = 0, quiet = 0, inacc;
2867 __u32 valid = QC_GENERAL, idx = 0;
2868 __u64 total_ialloc = 0, total_balloc = 0;
2869 bool human_readable = false;
2872 while ((c = getopt(argc, argv, "gi:I:o:qtuvh")) != -1) {
2875 if (qctl.qc_type != UGQUOTA) {
2876 fprintf(stderr, "error: use either -u or -g\n");
2879 qctl.qc_type = USRQUOTA;
2882 if (qctl.qc_type != UGQUOTA) {
2883 fprintf(stderr, "error: use either -u or -g\n");
2886 qctl.qc_type = GRPQUOTA;
2889 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2892 valid = qctl.qc_valid = QC_UUID;
2893 strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2896 valid = qctl.qc_valid = QC_MDTIDX;
2897 idx = qctl.qc_idx = atoi(optarg);
2900 valid = qctl.qc_valid = QC_OSTIDX;
2901 idx = qctl.qc_idx = atoi(optarg);
2910 human_readable = true;
2913 fprintf(stderr, "error: %s: option '-%c' "
2914 "unrecognized\n", argv[0], c);
2919 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2920 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2921 optind == argc - 1) {
2923 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2924 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2925 qctl.qc_valid = valid;
2928 qctl.qc_type = USRQUOTA;
2929 qctl.qc_id = geteuid();
2931 qctl.qc_type = GRPQUOTA;
2932 qctl.qc_id = getegid();
2934 rc = id2name(&name, qctl.qc_id,
2935 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2938 /* lfs quota -u username /path/to/lustre/mount */
2939 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2940 /* options should be followed by u/g-name and mntpoint */
2941 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2942 fprintf(stderr, "error: missing quota argument(s)\n");
2946 name = argv[optind++];
2947 rc = name2id(&qctl.qc_id, name,
2948 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2950 qctl.qc_id = strtoul(name, &endptr, 10);
2951 if (*endptr != '\0') {
2952 fprintf(stderr, "error: can't find id for name "
2957 } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2958 fprintf(stderr, "error: missing quota info argument(s)\n");
2964 rc1 = llapi_quotactl(mnt, &qctl);
2968 fprintf(stderr, "%s quotas are not enabled.\n",
2969 qctl.qc_type == USRQUOTA ? "user" : "group");
2972 fprintf(stderr, "Permission denied.\n");
2974 /* We already got a "No such file..." message. */
2977 fprintf(stderr, "Unexpected quotactl error: %s\n",
2982 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
2983 print_quota_title(name, &qctl, human_readable);
2985 if (rc1 && *obd_type)
2986 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2988 if (qctl.qc_valid != QC_GENERAL)
2991 inacc = (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) &&
2992 ((qctl.qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
2993 (QIF_LIMITS|QIF_USAGE));
2995 print_quota(mnt, &qctl, QC_GENERAL, rc1, human_readable);
2997 if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO &&
3001 rc2 = print_obd_quota(mnt, &qctl, 1, human_readable,
3003 rc3 = print_obd_quota(mnt, &qctl, 0, human_readable,
3005 kbytes2str(total_balloc, strbuf, human_readable);
3006 printf("Total allocated inode limit: "LPU64", total "
3007 "allocated block limit: %s\n", total_ialloc, strbuf);
3010 if (rc1 || rc2 || rc3 || inacc)
3011 printf("Some errors happened when getting quota info. "
3012 "Some devices may be not working or deactivated. "
3013 "The data in \"[]\" is inaccurate.\n");
3021 #endif /* HAVE_SYS_QUOTA_H! */
3023 static int flushctx_ioctl(char *mp)
3027 fd = open(mp, O_RDONLY);
3029 fprintf(stderr, "flushctx: error open %s: %s\n",
3030 mp, strerror(errno));
3034 rc = ioctl(fd, LL_IOC_FLUSHCTX);
3036 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
3037 mp, strerror(errno));
3043 static int lfs_flushctx(int argc, char **argv)
3045 int kdestroy = 0, c;
3046 char mntdir[PATH_MAX] = {'\0'};
3051 while ((c = getopt(argc, argv, "k")) != -1) {
3057 fprintf(stderr, "error: %s: option '-%c' "
3058 "unrecognized\n", argv[0], c);
3064 if ((rc = system("kdestroy > /dev/null")) != 0) {
3065 rc = WEXITSTATUS(rc);
3066 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
3070 if (optind >= argc) {
3071 /* flush for all mounted lustre fs. */
3072 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
3073 /* Check if we have a mount point */
3074 if (mntdir[0] == '\0')
3077 if (flushctx_ioctl(mntdir))
3080 mntdir[0] = '\0'; /* avoid matching in next loop */
3083 /* flush fs as specified */
3084 while (optind < argc) {
3085 if (flushctx_ioctl(argv[optind++]))
3092 static int lfs_lsetfacl(int argc, char **argv)
3095 return(llapi_lsetfacl(argc, argv));
3098 static int lfs_lgetfacl(int argc, char **argv)
3101 return(llapi_lgetfacl(argc, argv));
3104 static int lfs_rsetfacl(int argc, char **argv)
3107 return(llapi_rsetfacl(argc, argv));
3110 static int lfs_rgetfacl(int argc, char **argv)
3113 return(llapi_rgetfacl(argc, argv));
3116 static int lfs_cp(int argc, char **argv)
3118 return(llapi_cp(argc, argv));
3121 static int lfs_ls(int argc, char **argv)
3123 return(llapi_ls(argc, argv));
3126 static int lfs_changelog(int argc, char **argv)
3128 void *changelog_priv;
3129 struct changelog_ext_rec *rec;
3130 long long startrec = 0, endrec = 0;
3132 struct option long_opts[] = {
3133 {"follow", no_argument, 0, 'f'},
3136 char short_opts[] = "f";
3140 while ((rc = getopt_long(argc, argv, short_opts,
3141 long_opts, NULL)) != -1) {
3149 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3150 argv[0], argv[optind - 1]);
3157 mdd = argv[optind++];
3159 startrec = strtoll(argv[optind++], NULL, 10);
3161 endrec = strtoll(argv[optind++], NULL, 10);
3163 rc = llapi_changelog_start(&changelog_priv,
3164 CHANGELOG_FLAG_BLOCK |
3165 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
3168 fprintf(stderr, "Can't start changelog: %s\n",
3169 strerror(errno = -rc));
3173 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
3177 if (endrec && rec->cr_index > endrec) {
3178 llapi_changelog_free(&rec);
3181 if (rec->cr_index < startrec) {
3182 llapi_changelog_free(&rec);
3186 secs = rec->cr_time >> 30;
3187 gmtime_r(&secs, &ts);
3188 printf(LPU64" %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
3189 "0x%x t="DFID, rec->cr_index, rec->cr_type,
3190 changelog_type2str(rec->cr_type),
3191 ts.tm_hour, ts.tm_min, ts.tm_sec,
3192 (int)(rec->cr_time & ((1<<30) - 1)),
3193 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
3194 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
3195 if (rec->cr_namelen)
3196 /* namespace rec includes parent and filename */
3197 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
3198 rec->cr_namelen, rec->cr_name);
3200 if (!fid_is_zero(&rec->cr_sfid))
3201 printf(" s="DFID" sp="DFID" %.*s",
3202 PFID(&rec->cr_sfid), PFID(&rec->cr_spfid),
3203 changelog_rec_snamelen(rec),
3204 changelog_rec_sname(rec));
3207 llapi_changelog_free(&rec);
3210 llapi_changelog_fini(&changelog_priv);
3213 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
3215 return (rc == 1 ? 0 : rc);
3218 static int lfs_changelog_clear(int argc, char **argv)
3226 endrec = strtoll(argv[3], NULL, 10);
3228 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
3230 fprintf(stderr, "%s error: %s\n", argv[0],
3231 strerror(errno = -rc));
3235 static int lfs_fid2path(int argc, char **argv)
3237 struct option long_opts[] = {
3238 {"cur", no_argument, 0, 'c'},
3239 {"link", required_argument, 0, 'l'},
3240 {"rec", required_argument, 0, 'r'},
3243 char short_opts[] = "cl:r:";
3244 char *device, *fid, *path;
3245 long long recno = -1;
3253 while ((rc = getopt_long(argc, argv, short_opts,
3254 long_opts, NULL)) != -1) {
3260 linkno = strtol(optarg, NULL, 10);
3263 recno = strtoll(optarg, NULL, 10);
3268 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3269 argv[0], argv[optind - 1]);
3277 device = argv[optind++];
3278 path = calloc(1, PATH_MAX);
3280 fprintf(stderr, "error: Not enough memory\n");
3285 while (optind < argc) {
3286 fid = argv[optind++];
3288 lnktmp = (linkno >= 0) ? linkno : 0;
3290 int oldtmp = lnktmp;
3291 long long rectmp = recno;
3293 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
3296 fprintf(stderr, "%s: error on FID %s: %s\n",
3297 argv[0], fid, strerror(errno = -rc2));
3304 fprintf(stdout, "%lld ", rectmp);
3305 if (device[0] == '/') {
3306 fprintf(stdout, "%s", device);
3307 if (device[strlen(device) - 1] != '/')
3308 fprintf(stdout, "/");
3309 } else if (path[0] == '\0') {
3310 fprintf(stdout, "/");
3312 fprintf(stdout, "%s\n", path);
3315 /* specified linkno */
3317 if (oldtmp == lnktmp)
3327 static int lfs_path2fid(int argc, char **argv)
3330 const char *sep = "";
3340 while (*path != NULL) {
3341 int err = llapi_path2fid(*path, &fid);
3344 fprintf(stderr, "%s: can't get fid for %s: %s\n",
3345 argv[0], *path, strerror(-err));
3352 printf("%s%s"DFID"\n", *sep != '\0' ? *path : "", sep,
3361 static int lfs_data_version(int argc, char **argv)
3368 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
3374 while ((c = getopt(argc, argv, "nrw")) != -1) {
3377 data_version_flags = 0;
3380 data_version_flags |= LL_DV_RD_FLUSH;
3383 data_version_flags |= LL_DV_WR_FLUSH;
3392 path = argv[optind];
3393 fd = open(path, O_RDONLY);
3395 err(errno, "cannot open file %s", path);
3397 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
3399 err(errno, "cannot get version for %s", path);
3401 printf(LPU64 "\n", data_version);
3407 static int lfs_hsm_state(int argc, char **argv)
3412 struct hsm_user_state hus;
3420 rc = llapi_hsm_state_get(path, &hus);
3422 fprintf(stderr, "can't get hsm state for %s: %s\n",
3423 path, strerror(errno = -rc));
3427 /* Display path name and status flags */
3428 printf("%s: (0x%08x)", path, hus.hus_states);
3430 if (hus.hus_states & HS_RELEASED)
3431 printf(" released");
3432 if (hus.hus_states & HS_EXISTS)
3434 if (hus.hus_states & HS_DIRTY)
3436 if (hus.hus_states & HS_ARCHIVED)
3437 printf(" archived");
3438 /* Display user-settable flags */
3439 if (hus.hus_states & HS_NORELEASE)
3440 printf(" never_release");
3441 if (hus.hus_states & HS_NOARCHIVE)
3442 printf(" never_archive");
3443 if (hus.hus_states & HS_LOST)
3444 printf(" lost_from_hsm");
3446 if (hus.hus_archive_id != 0)
3447 printf(", archive_id:%d", hus.hus_archive_id);
3450 } while (++i < argc);
3455 #define LFS_HSM_SET 0
3456 #define LFS_HSM_CLEAR 1
3459 * Generic function to set or clear HSM flags.
3460 * Used by hsm_set and hsm_clear.
3462 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
3464 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
3466 struct option long_opts[] = {
3467 {"lost", 0, 0, 'l'},
3468 {"norelease", 0, 0, 'r'},
3469 {"noarchive", 0, 0, 'a'},
3470 {"archived", 0, 0, 'A'},
3471 {"dirty", 0, 0, 'd'},
3472 {"exists", 0, 0, 'e'},
3475 char short_opts[] = "lraAde";
3484 while ((c = getopt_long(argc, argv, short_opts,
3485 long_opts, NULL)) != -1) {
3491 mask |= HS_NOARCHIVE;
3494 mask |= HS_ARCHIVED;
3497 mask |= HS_NORELEASE;
3508 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3509 argv[0], argv[optind - 1]);
3514 /* User should have specified a flag */
3518 while (optind < argc) {
3520 path = argv[optind];
3522 /* If mode == 0, this means we apply the mask. */
3523 if (mode == LFS_HSM_SET)
3524 rc = llapi_hsm_state_set(path, mask, 0, 0);
3526 rc = llapi_hsm_state_set(path, 0, mask, 0);
3529 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
3530 path, strerror(errno = -rc));
3539 static int lfs_hsm_action(int argc, char **argv)
3544 struct hsm_current_action hca;
3545 struct hsm_extent he;
3546 enum hsm_user_action hua;
3547 enum hsm_progress_states hps;
3555 rc = llapi_hsm_current_action(path, &hca);
3557 fprintf(stderr, "can't get hsm action for %s: %s\n",
3558 path, strerror(errno = -rc));
3561 he = hca.hca_location;
3562 hua = hca.hca_action;
3563 hps = hca.hca_state;
3565 printf("%s: %s", path, hsm_user_action2name(hua));
3567 /* Skip file without action */
3568 if (hca.hca_action == HUA_NONE) {
3573 printf(" %s ", hsm_progress_state2name(hps));
3575 if ((hps == HPS_RUNNING) &&
3576 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
3577 printf("("LPX64 " bytes moved)\n", he.length);
3578 else if ((he.offset + he.length) == LUSTRE_EOF)
3579 printf("(from "LPX64 " to EOF)\n", he.offset);
3581 printf("(from "LPX64 " to "LPX64")\n",
3582 he.offset, he.offset + he.length);
3584 } while (++i < argc);
3589 static int lfs_hsm_set(int argc, char **argv)
3591 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
3594 static int lfs_hsm_clear(int argc, char **argv)
3596 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
3600 * Check file state and return its fid, to be used by lfs_hsm_request().
3602 * \param[in] file Path to file to check
3603 * \param[in,out] fid Pointer to allocated lu_fid struct.
3604 * \param[in,out] last_dev Pointer to last device id used.
3606 * \return 0 on success.
3608 static int lfs_hsm_prepare_file(char *file, struct lu_fid *fid,
3614 rc = lstat(file, &st);
3616 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
3619 /* A request should be ... */
3620 if (*last_dev != st.st_dev && *last_dev != 0) {
3621 fprintf(stderr, "All files should be "
3622 "on the same filesystem: %s\n", file);
3625 *last_dev = st.st_dev;
3627 rc = llapi_path2fid(file, fid);
3629 fprintf(stderr, "Cannot read FID of %s: %s\n",
3630 file, strerror(-rc));
3636 static int lfs_hsm_request(int argc, char **argv, int action)
3638 struct option long_opts[] = {
3639 {"filelist", 1, 0, 'l'},
3640 {"data", 1, 0, 'D'},
3641 {"archive", 1, 0, 'a'},
3645 char short_opts[] = "l:D:a:";
3646 struct hsm_user_request *hur, *oldhur;
3651 char *filelist = NULL;
3652 char fullpath[PATH_MAX];
3653 char *opaque = NULL;
3657 int nbfile_alloc = 0;
3658 char some_file[PATH_MAX+1] = "";
3665 while ((c = getopt_long(argc, argv, short_opts,
3666 long_opts, NULL)) != -1) {
3675 if (action != HUA_ARCHIVE) {
3677 "error: -a is supported only "
3678 "when archiving\n");
3681 archive_id = atoi(optarg);
3686 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3687 argv[0], argv[optind - 1]);
3692 /* All remaining args are files, so we have at least nbfile */
3693 nbfile = argc - optind;
3695 if ((nbfile == 0) && (filelist == NULL))
3699 opaque_len = strlen(opaque);
3701 /* Alloc the request structure with enough place to store all files
3702 * from command line. */
3703 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
3705 fprintf(stderr, "Cannot create the request: %s\n",
3709 nbfile_alloc = nbfile;
3711 hur->hur_request.hr_action = action;
3712 hur->hur_request.hr_archive_id = archive_id;
3713 hur->hur_request.hr_flags = 0;
3715 /* All remaining args are files, add them */
3717 if (strlen(argv[optind]) > sizeof(some_file)-1) {
3721 strncpy(some_file, argv[optind], sizeof(some_file));
3724 for (i = 0; i < nbfile; i++) {
3725 hur->hur_user_item[i].hui_extent.length = -1;
3726 rc = lfs_hsm_prepare_file(argv[optind + i],
3727 &hur->hur_user_item[i].hui_fid,
3729 hur->hur_request.hr_itemcount++;
3734 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
3736 /* If a filelist was specified, read the filelist from it. */
3737 if (filelist != NULL) {
3738 fp = fopen(filelist, "r");
3740 fprintf(stderr, "Cannot read the file list %s: %s\n",
3741 filelist, strerror(errno));
3746 while ((rc = getline(&line, &len, fp)) != -1) {
3747 struct hsm_user_item *hui;
3749 /* If allocated buffer was too small, gets something
3751 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
3753 nbfile_alloc = nbfile_alloc * 2 + 1;
3755 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
3758 fprintf(stderr, "hsm: cannot allocate "
3759 "the request: %s\n",
3766 size = hur_len(oldhur);
3768 fprintf(stderr, "hsm: cannot allocate "
3769 "%u files + %u bytes data\n",
3770 oldhur->hur_request.hr_itemcount,
3771 oldhur->hur_request.hr_data_len);
3778 memcpy(hur, oldhur, size);
3783 if (line[strlen(line) - 1] == '\n')
3784 line[strlen(line) - 1] = '\0';
3787 &hur->hur_user_item[hur->hur_request.hr_itemcount];
3788 hui->hui_extent.length = -1;
3789 rc = lfs_hsm_prepare_file(line, &hui->hui_fid,
3791 hur->hur_request.hr_itemcount++;
3797 if ((some_file[0] == '\0') &&
3798 (strlen(line) < sizeof(some_file)))
3799 strcpy(some_file, line);
3807 /* If a --data was used, add it to the request */
3808 hur->hur_request.hr_data_len = opaque_len;
3810 memcpy(hur_data(hur), opaque, opaque_len);
3812 /* Send the HSM request */
3813 if (realpath(some_file, fullpath) == NULL) {
3814 fprintf(stderr, "Could not find path '%s': %s\n",
3815 some_file, strerror(errno));
3817 rc = llapi_hsm_request(fullpath, hur);
3819 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
3820 some_file, strerror(-rc));
3829 static int lfs_hsm_archive(int argc, char **argv)
3831 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
3834 static int lfs_hsm_restore(int argc, char **argv)
3836 return lfs_hsm_request(argc, argv, HUA_RESTORE);
3839 static int lfs_hsm_release(int argc, char **argv)
3841 return lfs_hsm_request(argc, argv, HUA_RELEASE);
3844 static int lfs_hsm_remove(int argc, char **argv)
3846 return lfs_hsm_request(argc, argv, HUA_REMOVE);
3849 static int lfs_hsm_cancel(int argc, char **argv)
3851 return lfs_hsm_request(argc, argv, HUA_CANCEL);
3854 static int lfs_swap_layouts(int argc, char **argv)
3859 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
3860 SWAP_LAYOUTS_KEEP_MTIME |
3861 SWAP_LAYOUTS_KEEP_ATIME);
3864 int main(int argc, char **argv)
3870 Parser_init("lfs > ", cmdlist);
3873 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
3875 rc = Parser_commands();
3878 return rc < 0 ? -rc : rc;
3881 #ifdef _LUSTRE_IDL_H_
3882 /* Everything we need here should be included by lustreapi.h. */
3883 # error "lfs should not depend on lustre_idl.h"
3884 #endif /* _LUSTRE_IDL_H_ */