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"},
346 #define MIGRATION_BLOCKS 1
348 static int lfs_migrate(char *name, unsigned long long stripe_size,
349 int stripe_offset, int stripe_count,
350 int stripe_pattern, char *pool_name,
351 __u64 migration_flags)
354 char volatile_file[PATH_MAX +
355 LUSTRE_VOLATILE_HDR_LEN + 4];
356 char parent[PATH_MAX];
360 struct lov_user_md *lum = NULL;
365 __u64 rpos, wpos, bufoff;
370 /* find the right size for the IO and allocate the buffer */
371 lumsz = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
378 rc = llapi_file_get_stripe(name, lum);
379 /* failure can come from may case and some may be not real error
381 * in case of a real error, a later call will failed with a better
382 * error management */
386 bufsz = lum->lmm_stripe_size;
387 rc = posix_memalign(&buf, getpagesize(), bufsz);
393 if (migration_flags & MIGRATION_BLOCKS) {
394 /* generate a random id for the grouplock */
395 fd = open("/dev/urandom", O_RDONLY);
398 fprintf(stderr, "cannot open /dev/urandom (%s)\n",
403 rc = read(fd, &gid, sz);
407 fprintf(stderr, "cannot read %d bytes from"
408 " /dev/urandom (%s)\n", sz, strerror(-rc));
413 /* search for file directory pathname */
414 if (strlen(name) > sizeof(parent)-1) {
418 strncpy(parent, name, sizeof(parent));
419 ptr = strrchr(parent, '/');
421 if (getcwd(parent, sizeof(parent)) == NULL) {
431 rc = snprintf(volatile_file, sizeof(volatile_file), "%s/%s::", parent,
432 LUSTRE_VOLATILE_HDR);
433 if (rc >= sizeof(volatile_file)) {
438 /* create, open a volatile file, use caching (ie no directio) */
439 /* exclusive create is not needed because volatile files cannot
440 * conflict on name by construction */
441 fdv = llapi_file_open_pool(volatile_file, O_CREAT | O_WRONLY,
442 0644, stripe_size, stripe_offset,
443 stripe_count, stripe_pattern, pool_name);
446 fprintf(stderr, "cannot create volatile file in %s (%s)\n",
447 parent, strerror(-rc));
451 /* open file, direct io */
452 /* even if the file is only read, WR mode is nedeed to allow
453 * layout swap on fd */
454 fd = open(name, O_RDWR | O_DIRECT);
457 fprintf(stderr, "cannot open %s (%s)\n", name, strerror(-rc));
462 /* Not-owner (root?) special case.
463 * Need to set owner/group of volatile file like original.
464 * This will allow to pass related check during layout_swap.
469 fprintf(stderr, "cannot stat %s (%s)\n", name,
473 rc = fstat(fdv, &stv);
476 fprintf(stderr, "cannot stat %s (%s)\n", volatile_file,
480 if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
481 rc = fchown(fdv, st.st_uid, st.st_gid);
484 fprintf(stderr, "cannot chown %s (%s)\n", name,
490 /* get file data version */
491 rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
493 fprintf(stderr, "cannot get dataversion on %s (%s)\n",
494 name, strerror(-rc));
498 if (migration_flags & MIGRATION_BLOCKS) {
499 /* take group lock to limit concurent access
500 * this will be no more needed when exclusive access will
501 * be implemented (see LU-2919) */
502 /* group lock is taken after data version read because it
503 * blocks data version call */
504 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
506 fprintf(stderr, "cannot get group lock on %s (%s)\n",
507 name, strerror(-rc));
519 /* read new data only if we have written all
520 * previously read data */
522 rsize = read(fd, buf, bufsz);
525 fprintf(stderr, "read failed on %s"
536 wsize = write(fdv, buf + bufoff, rpos - wpos);
539 fprintf(stderr, "write failed on volatile"
540 " for %s (%s)\n", name, strerror(-rc));
550 if (migration_flags & MIGRATION_BLOCKS) {
551 /* give back group lock */
552 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
554 fprintf(stderr, "cannot put group lock on %s (%s)\n",
555 name, strerror(-rc));
561 * for a migration we need to:
562 * - check data version on file did not change
566 rc = llapi_fswap_layouts(fd, fdv, dv1, 0,
567 SWAP_LAYOUTS_CHECK_DV1 |
568 SWAP_LAYOUTS_KEEP_MTIME |
569 SWAP_LAYOUTS_KEEP_ATIME);
571 fprintf(stderr, "%s: dataversion changed during copy, "
572 "migration aborted\n", name);
576 fprintf(stderr, "%s: swap layout to new file failed: %s\n",
577 name, strerror(-rc));
580 /* give back group lock */
581 if ((migration_flags & MIGRATION_BLOCKS) && have_gl &&
582 (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1)) {
583 /* we keep in rc the original error */
584 fprintf(stderr, "cannot put group lock on %s (%s)\n",
585 name, strerror(-errno));
599 static int lfs_setstripe(int argc, char **argv)
603 unsigned long long st_size;
604 int st_offset, st_count;
608 char *stripe_size_arg = NULL;
609 char *stripe_off_arg = NULL;
610 char *stripe_count_arg = NULL;
611 char *pool_name_arg = NULL;
612 unsigned long long size_units = 1;
613 int migrate_mode = 0;
614 __u64 migration_flags = 0;
616 struct option long_opts[] = {
617 /* valid only in migrate mode */
618 {"block", no_argument, 0, 'b'},
619 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
620 /* This formerly implied "stripe-count", but was explicitly
621 * made "stripe-count" for consistency with other options,
622 * and to separate it from "mdt-count" when DNE arrives. */
623 {"count", required_argument, 0, 'c'},
625 {"stripe-count", required_argument, 0, 'c'},
626 {"stripe_count", required_argument, 0, 'c'},
627 {"delete", no_argument, 0, 'd'},
628 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
629 /* This formerly implied "stripe-index", but was explicitly
630 * made "stripe-index" for consistency with other options,
631 * and to separate it from "mdt-index" when DNE arrives. */
632 {"index", required_argument, 0, 'i'},
634 {"stripe-index", required_argument, 0, 'i'},
635 {"stripe_index", required_argument, 0, 'i'},
636 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
637 /* This formerly implied "stripe-index", but was confusing
638 * with "file offset" (which will eventually be needed for
639 * with different layouts by offset), so deprecate it. */
640 {"offset", required_argument, 0, 'o'},
642 {"pool", required_argument, 0, 'p'},
643 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
644 /* This formerly implied "--stripe-size", but was confusing
645 * with "lfs find --size|-s", which means "file size", so use
646 * the consistent "--stripe-size|-S" for all commands. */
647 {"size", required_argument, 0, 's'},
649 {"stripe-size", required_argument, 0, 'S'},
650 {"stripe_size", required_argument, 0, 'S'},
658 if (strcmp(argv[0], "migrate") == 0)
662 while ((c = getopt_long(argc, argv, "c:di:o:p:s:S:",
663 long_opts, NULL)) >= 0) {
669 if (migrate_mode == 0) {
670 fprintf(stderr, "--block is valid only for"
674 migration_flags |= MIGRATION_BLOCKS;
677 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
678 if (strcmp(argv[optind - 1], "--count") == 0)
679 fprintf(stderr, "warning: '--count' deprecated"
680 ", use '--stripe-count' instead\n");
682 stripe_count_arg = optarg;
685 /* delete the default striping pattern */
688 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
690 fprintf(stderr, "warning: '--offset|-o' deprecated, "
691 "use '--stripe-index|-i' instead\n");
694 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
695 if (strcmp(argv[optind - 1], "--index") == 0)
696 fprintf(stderr, "warning: '--index' deprecated"
697 ", use '--stripe-index' instead\n");
699 stripe_off_arg = optarg;
701 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
703 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
704 fprintf(stderr, "warning: '--size|-s' deprecated, "
705 "use '--stripe-size|-S' instead\n");
707 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0) */
709 stripe_size_arg = optarg;
712 pool_name_arg = optarg;
719 fname = argv[optind];
722 (stripe_size_arg != NULL || stripe_off_arg != NULL ||
723 stripe_count_arg != NULL || pool_name_arg != NULL)) {
724 fprintf(stderr, "error: %s: cannot specify -d with "
725 "-s, -c, -o, or -p options\n",
730 if (optind == argc) {
731 fprintf(stderr, "error: %s: missing filename|dirname\n",
736 /* get the stripe size */
737 if (stripe_size_arg != NULL) {
738 result = llapi_parse_size(stripe_size_arg, &st_size,
741 fprintf(stderr, "error: %s: bad stripe size '%s'\n",
742 argv[0], stripe_size_arg);
746 /* get the stripe offset */
747 if (stripe_off_arg != NULL) {
748 st_offset = strtol(stripe_off_arg, &end, 0);
750 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
751 argv[0], stripe_off_arg);
755 /* get the stripe count */
756 if (stripe_count_arg != NULL) {
757 st_count = strtoul(stripe_count_arg, &end, 0);
759 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
760 argv[0], stripe_count_arg);
767 result = lfs_migrate(fname, st_size, st_offset,
768 st_count, 0, pool_name_arg,
771 result = llapi_file_create_pool(fname, st_size,
776 "error: %s: %s stripe file '%s' failed\n",
777 argv[0], migrate_mode ? "migrate" : "create",
781 fname = argv[++optind];
782 } while (fname != NULL);
787 static int lfs_poollist(int argc, char **argv)
792 return llapi_poollist(argv[1]);
795 static int set_time(time_t *time, time_t *set, char *str)
802 else if (str[0] == '-')
808 t = strtol(str, NULL, 0);
809 if (*time < t * 24 * 60 * 60) {
812 fprintf(stderr, "Wrong time '%s' is specified.\n", str);
816 *set = *time - t * 24 * 60 * 60;
823 static int name2id(unsigned int *id, char *name, int type)
826 struct passwd *entry;
828 if (!(entry = getpwnam(name))) {
838 if (!(entry = getgrnam(name))) {
850 static int id2name(char **name, unsigned int id, int type)
853 struct passwd *entry;
855 if (!(entry = getpwuid(id))) {
861 *name = entry->pw_name;
865 if (!(entry = getgrgid(id))) {
871 *name = entry->gr_name;
877 static int name2layout(__u32 *layout, char *name)
882 for (ptr = name; ; ptr = NULL) {
883 lyt = strtok(ptr, ",");
886 if (strcmp(lyt, "released") == 0)
887 *layout |= LOV_PATTERN_F_RELEASED;
888 else if (strcmp(lyt, "raid0") == 0)
889 *layout |= LOV_PATTERN_RAID0;
896 #define FIND_POOL_OPT 3
897 static int lfs_find(int argc, char **argv)
902 struct find_param param = {
906 struct option long_opts[] = {
907 {"atime", required_argument, 0, 'A'},
908 {"stripe-count", required_argument, 0, 'c'},
909 {"stripe_count", required_argument, 0, 'c'},
910 {"ctime", required_argument, 0, 'C'},
911 {"maxdepth", required_argument, 0, 'D'},
912 {"gid", required_argument, 0, 'g'},
913 {"group", required_argument, 0, 'G'},
914 {"stripe-index", required_argument, 0, 'i'},
915 {"stripe_index", required_argument, 0, 'i'},
916 {"layout", required_argument, 0, 'L'},
917 {"mdt", required_argument, 0, 'm'},
918 {"mtime", required_argument, 0, 'M'},
919 {"name", required_argument, 0, 'n'},
920 /* reserve {"or", no_argument, , 0, 'o'}, to match find(1) */
921 {"obd", required_argument, 0, 'O'},
922 {"ost", required_argument, 0, 'O'},
923 /* no short option for pool, p/P already used */
924 {"pool", required_argument, 0, FIND_POOL_OPT},
925 {"print0", no_argument, 0, 'p'},
926 {"print", no_argument, 0, 'P'},
927 {"size", required_argument, 0, 's'},
928 {"stripe-size", required_argument, 0, 'S'},
929 {"stripe_size", required_argument, 0, 'S'},
930 {"type", required_argument, 0, 't'},
931 {"uid", required_argument, 0, 'u'},
932 {"user", required_argument, 0, 'U'},
946 /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
947 while ((c = getopt_long_only(argc, argv,
948 "-A:c:C:D:g:G:i:L:m:M:n:O:Ppqrs:S:t:u:U:v",
949 long_opts, NULL)) >= 0) {
954 /* '!' is part of option */
955 /* when getopt_long_only() finds a string which is not
956 * an option nor a known option argument it returns 1
957 * in that case if we already have found pathstart and pathend
958 * (i.e. we have the list of pathnames),
959 * the only supported value is "!"
961 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
962 if (!isoption && pathend != -1) {
963 fprintf(stderr, "err: %s: filename|dirname must either "
964 "precede options or follow options\n",
969 if (!isoption && pathstart == -1)
970 pathstart = optind - 1;
971 if (isoption && pathstart != -1 && pathend == -1)
972 pathend = optind - 2;
978 /* unknown; opt is "!" or path component,
979 * checking done above.
981 if (strcmp(optarg, "!") == 0)
985 xtime = ¶m.fp_atime;
986 xsign = ¶m.fp_asign;
987 param.fp_exclude_atime = !!neg_opt;
988 /* no break, this falls through to 'C' for ctime */
991 xtime = ¶m.fp_ctime;
992 xsign = ¶m.fp_csign;
993 param.fp_exclude_ctime = !!neg_opt;
995 /* no break, this falls through to 'M' for mtime */
998 xtime = ¶m.fp_mtime;
999 xsign = ¶m.fp_msign;
1000 param.fp_exclude_mtime = !!neg_opt;
1002 rc = set_time(&t, xtime, optarg);
1003 if (rc == INT_MAX) {
1011 if (optarg[0] == '+') {
1012 param.stripecount_sign = -1;
1014 } else if (optarg[0] == '-') {
1015 param.stripecount_sign = 1;
1019 param.stripecount = strtoul(optarg, &endptr, 0);
1020 if (*endptr != '\0') {
1021 fprintf(stderr,"error: bad stripe_count '%s'\n",
1026 param.check_stripecount = 1;
1027 param.exclude_stripecount = !!neg_opt;
1030 param.fp_max_depth = strtol(optarg, 0, 0);
1034 rc = name2id(¶m.fp_gid, optarg, GROUP);
1036 param.fp_gid = strtoul(optarg, &endptr, 10);
1037 if (*endptr != '\0') {
1038 fprintf(stderr, "Group/GID: %s cannot "
1039 "be found.\n", optarg);
1044 param.fp_exclude_gid = !!neg_opt;
1045 param.fp_check_gid = 1;
1048 ret = name2layout(¶m.layout, optarg);
1051 param.exclude_layout = !!neg_opt;
1052 param.check_layout = 1;
1056 rc = name2id(¶m.fp_uid, optarg, USER);
1058 param.fp_uid = strtoul(optarg, &endptr, 10);
1059 if (*endptr != '\0') {
1060 fprintf(stderr, "User/UID: %s cannot "
1061 "be found.\n", optarg);
1066 param.fp_exclude_uid = !!neg_opt;
1067 param.fp_check_uid = 1;
1070 if (strlen(optarg) > LOV_MAXPOOLNAME) {
1072 "Pool name %s is too long"
1073 " (max is %d)\n", optarg,
1078 /* we do check for empty pool because empty pool
1079 * is used to find V1 lov attributes */
1080 strncpy(param.poolname, optarg, LOV_MAXPOOLNAME);
1081 param.poolname[LOV_MAXPOOLNAME] = '\0';
1082 param.exclude_pool = !!neg_opt;
1083 param.check_pool = 1;
1086 param.pattern = (char *)optarg;
1087 param.exclude_pattern = !!neg_opt;
1092 char *buf, *token, *next, *p;
1096 buf = strdup(optarg);
1102 param.exclude_obd = !!neg_opt;
1105 while (token && *token) {
1106 token = strchr(token, ',');
1113 param.exclude_mdt = !!neg_opt;
1114 param.num_alloc_mdts += len;
1115 tmp = realloc(param.mdtuuid,
1116 param.num_alloc_mdts *
1117 sizeof(*param.mdtuuid));
1119 GOTO(err_free, ret = -ENOMEM);
1120 param.mdtuuid = tmp;
1122 param.exclude_obd = !!neg_opt;
1123 param.num_alloc_obds += len;
1124 tmp = realloc(param.obduuid,
1125 param.num_alloc_obds *
1126 sizeof(*param.obduuid));
1128 GOTO(err_free, ret = -ENOMEM);
1129 param.obduuid = tmp;
1131 for (token = buf; token && *token; token = next) {
1132 struct obd_uuid *puuid;
1135 ¶m.mdtuuid[param.num_mdts++];
1138 ¶m.obduuid[param.num_obds++];
1140 p = strchr(token, ',');
1146 if (strlen(token) > sizeof(puuid->uuid)-1)
1147 GOTO(err_free, ret = -E2BIG);
1148 strncpy(puuid->uuid, token,
1149 sizeof(puuid->uuid));
1162 if (optarg[0] == '+') {
1163 param.size_sign = -1;
1165 } else if (optarg[0] == '-') {
1166 param.size_sign = 1;
1170 ret = llapi_parse_size(optarg, ¶m.size,
1171 ¶m.size_units, 0);
1173 fprintf(stderr, "error: bad file size '%s'\n",
1177 param.check_size = 1;
1178 param.exclude_size = !!neg_opt;
1181 if (optarg[0] == '+') {
1182 param.stripesize_sign = -1;
1184 } else if (optarg[0] == '-') {
1185 param.stripesize_sign = 1;
1189 ret = llapi_parse_size(optarg, ¶m.stripesize,
1190 ¶m.stripesize_units, 0);
1192 fprintf(stderr, "error: bad stripe_size '%s'\n",
1196 param.check_stripesize = 1;
1197 param.exclude_stripesize = !!neg_opt;
1200 param.fp_exclude_type = !!neg_opt;
1201 switch (optarg[0]) {
1203 param.fp_type = S_IFBLK;
1206 param.fp_type = S_IFCHR;
1209 param.fp_type = S_IFDIR;
1212 param.fp_type = S_IFREG;
1215 param.fp_type = S_IFLNK;
1218 param.fp_type = S_IFIFO;
1221 param.fp_type = S_IFSOCK;
1224 fprintf(stderr, "error: %s: bad type '%s'\n",
1236 if (pathstart == -1) {
1237 fprintf(stderr, "error: %s: no filename|pathname\n",
1241 } else if (pathend == -1) {
1247 rc = llapi_find(argv[pathstart], ¶m);
1248 if (rc != 0 && ret == 0)
1250 } while (++pathstart < pathend);
1253 fprintf(stderr, "error: %s failed for %s.\n",
1254 argv[0], argv[optind - 1]);
1256 if (param.obduuid && param.num_alloc_obds)
1257 free(param.obduuid);
1259 if (param.mdtuuid && param.num_alloc_mdts)
1260 free(param.mdtuuid);
1265 static int lfs_getstripe_internal(int argc, char **argv,
1266 struct find_param *param)
1268 struct option long_opts[] = {
1269 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1270 /* This formerly implied "stripe-count", but was explicitly
1271 * made "stripe-count" for consistency with other options,
1272 * and to separate it from "mdt-count" when DNE arrives. */
1273 {"count", no_argument, 0, 'c'},
1275 {"stripe-count", no_argument, 0, 'c'},
1276 {"stripe_count", no_argument, 0, 'c'},
1277 {"directory", no_argument, 0, 'd'},
1278 {"default", no_argument, 0, 'D'},
1279 {"generation", no_argument, 0, 'g'},
1280 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1281 /* This formerly implied "stripe-index", but was explicitly
1282 * made "stripe-index" for consistency with other options,
1283 * and to separate it from "mdt-index" when DNE arrives. */
1284 {"index", no_argument, 0, 'i'},
1286 {"stripe-index", no_argument, 0, 'i'},
1287 {"stripe_index", no_argument, 0, 'i'},
1288 {"layout", no_argument, 0, 'L'},
1289 {"mdt-index", no_argument, 0, 'M'},
1290 {"mdt_index", no_argument, 0, 'M'},
1291 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1292 /* This formerly implied "stripe-index", but was confusing
1293 * with "file offset" (which will eventually be needed for
1294 * with different layouts by offset), so deprecate it. */
1295 {"offset", no_argument, 0, 'o'},
1297 {"obd", required_argument, 0, 'O'},
1298 {"ost", required_argument, 0, 'O'},
1299 {"pool", no_argument, 0, 'p'},
1300 {"quiet", no_argument, 0, 'q'},
1301 {"recursive", no_argument, 0, 'r'},
1302 {"raw", no_argument, 0, 'R'},
1303 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1304 /* This formerly implied "--stripe-size", but was confusing
1305 * with "lfs find --size|-s", which means "file size", so use
1306 * the consistent "--stripe-size|-S" for all commands. */
1307 {"size", no_argument, 0, 's'},
1309 {"stripe-size", no_argument, 0, 'S'},
1310 {"stripe_size", no_argument, 0, 'S'},
1311 {"verbose", no_argument, 0, 'v'},
1316 param->fp_max_depth = 1;
1318 while ((c = getopt_long(argc, argv, "cdDghiLMoO:pqrRsSv",
1319 long_opts, NULL)) != -1) {
1322 if (param->obduuid) {
1324 "error: %s: only one obduuid allowed",
1328 param->obduuid = (struct obd_uuid *)optarg;
1334 param->fp_max_depth = 0;
1337 param->get_default_lmv = 1;
1340 param->recursive = 1;
1343 param->verbose = VERBOSE_ALL | VERBOSE_DETAIL;
1346 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1347 if (strcmp(argv[optind - 1], "--count") == 0)
1348 fprintf(stderr, "warning: '--count' deprecated,"
1349 " use '--stripe-count' instead\n");
1351 if (!(param->verbose & VERBOSE_DETAIL)) {
1352 param->verbose |= VERBOSE_COUNT;
1353 param->fp_max_depth = 0;
1356 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1358 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1359 fprintf(stderr, "warning: '--size|-s' deprecated, "
1360 "use '--stripe-size|-S' instead\n");
1362 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0) */
1364 if (!(param->verbose & VERBOSE_DETAIL)) {
1365 param->verbose |= VERBOSE_SIZE;
1366 param->fp_max_depth = 0;
1369 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
1371 fprintf(stderr, "warning: '--offset|-o' deprecated, "
1372 "use '--stripe-index|-i' instead\n");
1375 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
1376 if (strcmp(argv[optind - 1], "--index") == 0)
1377 fprintf(stderr, "warning: '--index' deprecated"
1378 ", use '--stripe-index' instead\n");
1380 if (!(param->verbose & VERBOSE_DETAIL)) {
1381 param->verbose |= VERBOSE_OFFSET;
1382 param->fp_max_depth = 0;
1386 if (!(param->verbose & VERBOSE_DETAIL)) {
1387 param->verbose |= VERBOSE_POOL;
1388 param->fp_max_depth = 0;
1392 if (!(param->verbose & VERBOSE_DETAIL)) {
1393 param->verbose |= VERBOSE_GENERATION;
1394 param->fp_max_depth = 0;
1398 if (!(param->verbose & VERBOSE_DETAIL)) {
1399 param->verbose |= VERBOSE_LAYOUT;
1400 param->fp_max_depth = 0;
1404 if (!(param->verbose & VERBOSE_DETAIL))
1405 param->fp_max_depth = 0;
1406 param->verbose |= VERBOSE_MDTINDEX;
1419 if (param->recursive)
1420 param->fp_max_depth = -1;
1422 if (!param->verbose)
1423 param->verbose = VERBOSE_ALL;
1425 param->verbose = VERBOSE_OBJID;
1428 rc = llapi_getstripe(argv[optind], param);
1429 } while (++optind < argc && !rc);
1432 fprintf(stderr, "error: %s failed for %s.\n",
1433 argv[0], argv[optind - 1]);
1437 static int lfs_tgts(int argc, char **argv)
1439 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
1440 struct find_param param;
1441 int index = 0, rc=0;
1446 if (argc == 2 && !realpath(argv[1], path)) {
1448 fprintf(stderr, "error: invalid path '%s': %s\n",
1449 argv[1], strerror(-rc));
1453 while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
1454 /* Check if we have a mount point */
1455 if (mntdir[0] == '\0')
1458 memset(¶m, 0, sizeof(param));
1459 if (!strcmp(argv[0], "mdts"))
1462 rc = llapi_ostlist(mntdir, ¶m);
1464 fprintf(stderr, "error: %s: failed on %s\n",
1467 if (path[0] != '\0')
1469 memset(mntdir, 0, PATH_MAX);
1475 static int lfs_getstripe(int argc, char **argv)
1477 struct find_param param = { 0 };
1478 return lfs_getstripe_internal(argc, argv, ¶m);
1482 static int lfs_getdirstripe(int argc, char **argv)
1484 struct find_param param = { 0 };
1487 return lfs_getstripe_internal(argc, argv, ¶m);
1491 static int lfs_setdirstripe(int argc, char **argv)
1495 unsigned int stripe_offset = -1;
1496 unsigned int stripe_count = 1;
1497 enum lmv_hash_type hash_type;
1500 char *stripe_offset_opt = NULL;
1501 char *stripe_count_opt = NULL;
1502 char *stripe_hash_opt = NULL;
1503 char *mode_opt = NULL;
1504 int default_stripe = 0;
1505 mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
1506 mode_t previous_mode = 0;
1508 struct option long_opts[] = {
1509 {"count", required_argument, 0, 'c'},
1510 {"index", required_argument, 0, 'i'},
1511 {"mode", required_argument, 0, 'm'},
1512 {"hash-type", required_argument, 0, 't'},
1513 {"default_stripe", no_argument, 0, 'D'},
1519 while ((c = getopt_long(argc, argv, "c:Di:m:t:", long_opts,
1526 stripe_count_opt = optarg;
1532 stripe_offset_opt = optarg;
1538 stripe_hash_opt = optarg;
1541 fprintf(stderr, "error: %s: option '%s' "
1543 argv[0], argv[optind - 1]);
1548 if (optind == argc) {
1549 fprintf(stderr, "error: %s: missing dirname\n",
1554 if (stripe_offset_opt == NULL && stripe_count_opt == NULL) {
1555 fprintf(stderr, "error: %s: missing stripe offset and count.\n",
1560 if (stripe_offset_opt != NULL) {
1561 /* get the stripe offset */
1562 stripe_offset = strtoul(stripe_offset_opt, &end, 0);
1564 fprintf(stderr, "error: %s: bad stripe offset '%s'\n",
1565 argv[0], stripe_offset_opt);
1570 if (mode_opt != NULL) {
1571 mode = strtoul(mode_opt, &end, 8);
1573 fprintf(stderr, "error: %s: bad mode '%s'\n",
1577 previous_mode = umask(0);
1580 if (stripe_hash_opt == NULL ||
1581 strcmp(stripe_hash_opt, LMV_HASH_NAME_FNV_1A_64) == 0) {
1582 hash_type = LMV_HASH_TYPE_FNV_1A_64;
1583 } else if (strcmp(stripe_hash_opt, LMV_HASH_NAME_ALL_CHARS) == 0) {
1584 hash_type = LMV_HASH_TYPE_ALL_CHARS;
1586 fprintf(stderr, "error: %s: bad stripe hash type '%s'\n",
1587 argv[0], stripe_hash_opt);
1591 /* get the stripe count */
1592 if (stripe_count_opt != NULL) {
1593 stripe_count = strtoul(stripe_count_opt, &end, 0);
1595 fprintf(stderr, "error: %s: bad stripe count '%s'\n",
1596 argv[0], stripe_count_opt);
1601 dname = argv[optind];
1603 if (default_stripe == 1) {
1604 result = llapi_dir_set_default_lmv_stripe(dname,
1605 stripe_offset, stripe_count,
1608 result = llapi_dir_create_pool(dname, mode,
1610 stripe_count, hash_type,
1615 fprintf(stderr, "error: %s: create stripe dir '%s' "
1616 "failed\n", argv[0], dname);
1619 dname = argv[++optind];
1620 } while (dname != NULL);
1622 if (mode_opt != NULL)
1623 umask(previous_mode);
1629 static int lfs_rmentry(int argc, char **argv)
1636 fprintf(stderr, "error: %s: missing dirname\n",
1642 dname = argv[index];
1643 while (dname != NULL) {
1644 result = llapi_direntry_remove(dname);
1646 fprintf(stderr, "error: %s: remove dir entry '%s' "
1647 "failed\n", argv[0], dname);
1650 dname = argv[++index];
1655 static int lfs_mv(int argc, char **argv)
1657 struct find_param param = {
1664 struct option long_opts[] = {
1665 {"--mdt-index", required_argument, 0, 'M'},
1666 {"verbose", no_argument, 0, 'v'},
1670 while ((c = getopt_long(argc, argv, "M:v", long_opts, NULL)) != -1) {
1673 param.mdtindex = strtoul(optarg, &end, 0);
1675 fprintf(stderr, "%s: invalid MDT index'%s'\n",
1682 param.verbose = VERBOSE_DETAIL;
1686 fprintf(stderr, "error: %s: unrecognized option '%s'\n",
1687 argv[0], argv[optind - 1]);
1692 if (param.mdtindex == -1) {
1693 fprintf(stderr, "%s MDT index must be indicated\n", argv[0]);
1697 if (optind >= argc) {
1698 fprintf(stderr, "%s missing operand path\n", argv[0]);
1703 rc = llapi_mv(argv[optind], ¶m);
1705 fprintf(stderr, "cannot migrate '%s' to MDT%04x: %s\n",
1706 argv[optind], param.mdtindex, strerror(-rc));
1710 static int lfs_osts(int argc, char **argv)
1712 return lfs_tgts(argc, argv);
1715 static int lfs_mdts(int argc, char **argv)
1717 return lfs_tgts(argc, argv);
1720 #define COOK(value) \
1723 while (value > 1024) { \
1731 #define CDF "%11llu"
1732 #define HDF "%8.1f%c"
1736 static int showdf(char *mntdir, struct obd_statfs *stat,
1737 char *uuid, int ishow, int cooked,
1738 char *type, int index, int rc)
1740 long long avail, used, total;
1742 char *suffix = "KMGTPEZY";
1743 /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
1744 char tbuf[3 * sizeof(__u64)];
1745 char ubuf[3 * sizeof(__u64)];
1746 char abuf[3 * sizeof(__u64)];
1747 char rbuf[3 * sizeof(__u64)];
1755 avail = stat->os_ffree;
1756 used = stat->os_files - stat->os_ffree;
1757 total = stat->os_files;
1759 int shift = cooked ? 0 : 10;
1761 avail = (stat->os_bavail * stat->os_bsize) >> shift;
1762 used = ((stat->os_blocks - stat->os_bfree) *
1763 stat->os_bsize) >> shift;
1764 total = (stat->os_blocks * stat->os_bsize) >> shift;
1767 if ((used + avail) > 0)
1768 ratio = (double)used / (double)(used + avail);
1774 cook_val = (double)total;
1777 sprintf(tbuf, HDF, cook_val, suffix[i - 1]);
1779 sprintf(tbuf, CDF, total);
1781 cook_val = (double)used;
1784 sprintf(ubuf, HDF, cook_val, suffix[i - 1]);
1786 sprintf(ubuf, CDF, used);
1788 cook_val = (double)avail;
1791 sprintf(abuf, HDF, cook_val, suffix[i - 1]);
1793 sprintf(abuf, CDF, avail);
1795 sprintf(tbuf, CDF, total);
1796 sprintf(ubuf, CDF, used);
1797 sprintf(abuf, CDF, avail);
1800 sprintf(rbuf, RDF, (int)(ratio * 100 + 0.5));
1801 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
1802 uuid, tbuf, ubuf, abuf, rbuf, mntdir);
1804 printf("[%s:%d]\n", type, index);
1810 printf(UUF": inactive device\n", uuid);
1813 printf(UUF": %s\n", uuid, strerror(-rc));
1820 struct ll_stat_type {
1825 static int mntdf(char *mntdir, char *fsname, char *pool, int ishow,
1826 int cooked, int lazy)
1828 struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
1829 struct obd_uuid uuid_buf;
1830 char *poolname = NULL;
1831 struct ll_stat_type types[] = { { LL_STATFS_LMV, "MDT" },
1832 { LL_STATFS_LOV, "OST" },
1834 struct ll_stat_type *tp;
1835 __u64 ost_ffree = 0;
1841 poolname = strchr(pool, '.');
1842 if (poolname != NULL) {
1843 if (strncmp(fsname, pool, strlen(fsname))) {
1844 fprintf(stderr, "filesystem name incorrect\n");
1853 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1854 "UUID", "Inodes", "IUsed", "IFree",
1855 "IUse%", "Mounted on");
1857 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
1858 "UUID", cooked ? "bytes" : "1K-blocks",
1859 "Used", "Available", "Use%", "Mounted on");
1861 for (tp = types; tp->st_name != NULL; tp++) {
1862 for (index = 0; ; index++) {
1863 memset(&stat_buf, 0, sizeof(struct obd_statfs));
1864 memset(&uuid_buf, 0, sizeof(struct obd_uuid));
1865 type = lazy ? tp->st_op | LL_STATFS_NODELAY : tp->st_op;
1866 rc = llapi_obd_statfs(mntdir, type, index,
1867 &stat_buf, &uuid_buf);
1871 if (poolname && tp->st_op == LL_STATFS_LOV &&
1872 llapi_search_ost(fsname, poolname,
1873 obd_uuid2str(&uuid_buf)) != 1)
1876 /* the llapi_obd_statfs() call may have returned with
1877 * an error, but if it filled in uuid_buf we will at
1878 * lease use that to print out a message for that OBD.
1879 * If we didn't get anything in the uuid_buf, then fill
1880 * it in so that we can print an error message. */
1881 if (uuid_buf.uuid[0] == '\0')
1882 sprintf(uuid_buf.uuid, "%s%04x",
1883 tp->st_name, index);
1884 showdf(mntdir, &stat_buf, obd_uuid2str(&uuid_buf),
1885 ishow, cooked, tp->st_name, index, rc);
1888 if (tp->st_op == LL_STATFS_LMV) {
1889 sum.os_ffree += stat_buf.os_ffree;
1890 sum.os_files += stat_buf.os_files;
1891 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
1892 sum.os_blocks += stat_buf.os_blocks *
1894 sum.os_bfree += stat_buf.os_bfree *
1896 sum.os_bavail += stat_buf.os_bavail *
1898 ost_ffree += stat_buf.os_ffree;
1900 } else if (rc == -EINVAL || rc == -EFAULT) {
1906 /* If we don't have as many objects free on the OST as inodes
1907 * on the MDS, we reduce the total number of inodes to
1908 * compensate, so that the "inodes in use" number is correct.
1909 * Matches ll_statfs_internal() so the results are consistent. */
1910 if (ost_ffree < sum.os_ffree) {
1911 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
1912 sum.os_ffree = ost_ffree;
1915 showdf(mntdir, &sum, "filesystem summary:", ishow, cooked, NULL, 0, 0);
1920 static int lfs_df(int argc, char **argv)
1922 char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
1923 int ishow = 0, cooked = 0;
1925 int c, rc = 0, index = 0;
1926 char fsname[PATH_MAX] = "", *pool_name = NULL;
1927 struct option long_opts[] = {
1928 {"pool", required_argument, 0, 'p'},
1929 {"lazy", 0, 0, 'l'},
1934 while ((c = getopt_long(argc, argv, "hilp:", long_opts, NULL)) != -1) {
1952 if (optind < argc && !realpath(argv[optind], path)) {
1954 fprintf(stderr, "error: invalid path '%s': %s\n",
1955 argv[optind], strerror(-rc));
1959 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
1960 /* Check if we have a mount point */
1961 if (mntdir[0] == '\0')
1964 rc = mntdf(mntdir, fsname, pool_name, ishow, cooked, lazy);
1965 if (rc || path[0] != '\0')
1967 fsname[0] = '\0'; /* avoid matching in next loop */
1968 mntdir[0] = '\0'; /* avoid matching in next loop */
1974 static int lfs_getname(int argc, char **argv)
1976 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
1977 int rc = 0, index = 0, c;
1978 char buf[sizeof(struct obd_uuid)];
1981 while ((c = getopt(argc, argv, "h")) != -1)
1984 if (optind == argc) { /* no paths specified, get all paths. */
1985 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
1986 rc = llapi_getname(mntdir, buf, sizeof(buf));
1989 "cannot get name for `%s': %s\n",
1990 mntdir, strerror(-rc));
1994 printf("%s %s\n", buf, mntdir);
1996 path[0] = fsname[0] = mntdir[0] = 0;
1998 } else { /* paths specified, only attempt to search these. */
1999 for (; optind < argc; optind++) {
2000 rc = llapi_getname(argv[optind], buf, sizeof(buf));
2003 "cannot get name for `%s': %s\n",
2004 argv[optind], strerror(-rc));
2008 printf("%s %s\n", buf, argv[optind]);
2014 static int lfs_check(int argc, char **argv)
2017 char mntdir[PATH_MAX] = {'\0'};
2026 obd_types[0] = obd_type1;
2027 obd_types[1] = obd_type2;
2029 if (strcmp(argv[1], "osts") == 0) {
2030 strcpy(obd_types[0], "osc");
2031 } else if (strcmp(argv[1], "mds") == 0) {
2032 strcpy(obd_types[0], "mdc");
2033 } else if (strcmp(argv[1], "servers") == 0) {
2035 strcpy(obd_types[0], "osc");
2036 strcpy(obd_types[1], "mdc");
2038 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2043 rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
2044 if (rc < 0 || mntdir[0] == '\0') {
2045 fprintf(stderr, "No suitable Lustre mount found\n");
2049 rc = llapi_target_check(num_types, obd_types, mntdir);
2051 fprintf(stderr, "error: %s: %s status failed\n",
2058 static int lfs_join(int argc, char **argv)
2060 fprintf(stderr, "join two lustre files into one.\n"
2061 "obsolete, HEAD does not support it anymore.\n");
2065 #ifdef HAVE_SYS_QUOTA_H
2066 static int lfs_quotacheck(int argc, char **argv)
2068 int c, check_type = 0;
2070 struct if_quotacheck qchk;
2071 struct if_quotactl qctl;
2072 char *obd_type = (char *)qchk.obd_type;
2075 memset(&qchk, 0, sizeof(qchk));
2078 while ((c = getopt(argc, argv, "gu")) != -1) {
2087 fprintf(stderr, "error: %s: option '-%c' "
2088 "unrecognized\n", argv[0], c);
2095 else /* do quotacheck for both user & group quota by default */
2103 rc = llapi_quotacheck(mnt, check_type);
2104 if (rc == -EOPNOTSUPP) {
2105 fprintf(stderr, "error: quotacheck not supported by the quota "
2106 "master.\nPlease note that quotacheck is deprecated as "
2107 "of lustre 2.4.0 since space accounting is always "
2108 "enabled.\nFilesystems not formatted with 2.4 utils or "
2109 "beyond can be upgraded with tunefs.lustre --quota.\n");
2112 fprintf(stderr, "quotacheck failed: %s\n", strerror(-rc));
2116 rc = llapi_poll_quotacheck(mnt, &qchk);
2119 fprintf(stderr, "%s %s ", obd_type,
2120 obd_uuid2str(&qchk.obd_uuid));
2121 fprintf(stderr, "quota check failed: %s\n", strerror(-rc));
2125 memset(&qctl, 0, sizeof(qctl));
2126 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
2127 qctl.qc_type = check_type;
2128 rc = llapi_quotactl(mnt, &qctl);
2129 if (rc && rc != -EALREADY) {
2131 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
2132 obd_uuid2str(&qctl.obd_uuid));
2133 fprintf(stderr, "%s turn on quota failed: %s\n",
2134 argv[0], strerror(-rc));
2141 static int lfs_quotaon(int argc, char **argv)
2145 struct if_quotactl qctl;
2146 char *obd_type = (char *)qctl.obd_type;
2149 memset(&qctl, 0, sizeof(qctl));
2150 qctl.qc_cmd = LUSTRE_Q_QUOTAON;
2153 while ((c = getopt(argc, argv, "fgu")) != -1) {
2156 qctl.qc_type |= 0x01;
2159 qctl.qc_type |= 0x02;
2162 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
2165 fprintf(stderr, "error: %s: option '-%c' "
2166 "unrecognized\n", argv[0], c);
2173 else /* by default, enable quota for both user & group */
2174 qctl.qc_type = 0x02;
2181 rc = llapi_quotactl(mnt, &qctl);
2183 if (rc == -EOPNOTSUPP) {
2184 fprintf(stderr, "error: quotaon not supported by the "
2185 "quota master.\nPlease note that quotaon/off is"
2186 " deprecated as of lustre 2.4.0.\nQuota "
2187 "enforcement should now be enabled on the MGS "
2188 "via:\nmgs# lctl conf_param ${FSNAME}.quota."
2189 "<ost|mdt>=<u|g|ug>\n(ost for block quota, mdt "
2190 "for inode quota, u for user and g for group"
2192 } else if (rc == -EALREADY) {
2194 } else if (rc == -ENOENT) {
2195 fprintf(stderr, "error: cannot find quota database, "
2196 "make sure you have run quotacheck\n");
2199 fprintf(stderr, "%s %s ", obd_type,
2200 obd_uuid2str(&qctl.obd_uuid));
2201 fprintf(stderr, "%s failed: %s\n", argv[0],
2209 static int lfs_quotaoff(int argc, char **argv)
2213 struct if_quotactl qctl;
2214 char *obd_type = (char *)qctl.obd_type;
2217 memset(&qctl, 0, sizeof(qctl));
2218 qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
2221 while ((c = getopt(argc, argv, "gu")) != -1) {
2224 qctl.qc_type |= 0x01;
2227 qctl.qc_type |= 0x02;
2230 fprintf(stderr, "error: %s: option '-%c' "
2231 "unrecognized\n", argv[0], c);
2238 else /* by default, disable quota for both user & group */
2239 qctl.qc_type = 0x02;
2246 rc = llapi_quotactl(mnt, &qctl);
2248 if (rc == -EOPNOTSUPP) {
2249 fprintf(stderr, "error: quotaoff not supported by the "
2250 "quota master.\nPlease note that quotaon/off is"
2251 " deprecated as of lustre 2.4.0.\nQuota "
2252 "enforcement can be disabled on the MGS via:\n"
2253 "mgs# lctl conf_param ${FSNAME}.quota.<ost|mdt>"
2255 } else if (rc == -EALREADY) {
2259 fprintf(stderr, "%s %s ", obd_type,
2260 obd_uuid2str(&qctl.obd_uuid));
2261 fprintf(stderr, "quotaoff failed: %s\n",
2269 #define ARG2INT(nr, str, msg) \
2272 nr = strtol(str, &endp, 0); \
2274 fprintf(stderr, "error: bad %s: %s\n", msg, str); \
2279 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
2281 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
2282 * returns the value or ULONG_MAX on integer overflow or incorrect format
2284 * 1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
2285 * 2. specifiers may be encountered multiple times (2s3s is 5 seconds)
2286 * 3. empty integer value is interpreted as 0
2288 static unsigned long str2sec(const char* timestr)
2290 const char spec[] = "smhdw";
2291 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
2292 unsigned long val = 0;
2295 if (strpbrk(timestr, spec) == NULL) {
2296 /* no specifiers inside the time string,
2297 should treat it as an integer value */
2298 val = strtoul(timestr, &tail, 10);
2299 return *tail ? ULONG_MAX : val;
2302 /* format string is XXwXXdXXhXXmXXs */
2308 v = strtoul(timestr, &tail, 10);
2309 if (v == ULONG_MAX || *tail == '\0')
2310 /* value too large (ULONG_MAX or more)
2311 or missing specifier */
2314 ptr = strchr(spec, *tail);
2316 /* unknown specifier */
2321 /* check if product will overflow the type */
2322 if (!(v < ULONG_MAX / mult[ind]))
2325 ADD_OVERFLOW(val, mult[ind] * v);
2326 if (val == ULONG_MAX)
2338 #define ARG2ULL(nr, str, def_units) \
2340 unsigned long long limit, units = def_units; \
2343 rc = llapi_parse_size(str, &limit, &units, 1); \
2345 fprintf(stderr, "error: bad limit value %s\n", str); \
2351 static inline int has_times_option(int argc, char **argv)
2355 for (i = 1; i < argc; i++)
2356 if (!strcmp(argv[i], "-t"))
2362 int lfs_setquota_times(int argc, char **argv)
2365 struct if_quotactl qctl;
2366 char *mnt, *obd_type = (char *)qctl.obd_type;
2367 struct obd_dqblk *dqb = &qctl.qc_dqblk;
2368 struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
2369 struct option long_opts[] = {
2370 {"block-grace", required_argument, 0, 'b'},
2371 {"group", no_argument, 0, 'g'},
2372 {"inode-grace", required_argument, 0, 'i'},
2373 {"times", no_argument, 0, 't'},
2374 {"user", no_argument, 0, 'u'},
2378 memset(&qctl, 0, sizeof(qctl));
2379 qctl.qc_cmd = LUSTRE_Q_SETINFO;
2380 qctl.qc_type = UGQUOTA;
2383 while ((c = getopt_long(argc, argv, "b:gi:tu", long_opts, NULL)) != -1) {
2387 if (qctl.qc_type != UGQUOTA) {
2388 fprintf(stderr, "error: -u and -g can't be used "
2389 "more than once\n");
2392 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
2395 if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
2396 fprintf(stderr, "error: bad block-grace: %s\n",
2400 dqb->dqb_valid |= QIF_BTIME;
2403 if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
2404 fprintf(stderr, "error: bad inode-grace: %s\n",
2408 dqb->dqb_valid |= QIF_ITIME;
2410 case 't': /* Yes, of course! */
2412 default: /* getopt prints error message for us when opterr != 0 */
2417 if (qctl.qc_type == UGQUOTA) {
2418 fprintf(stderr, "error: neither -u nor -g specified\n");
2422 if (optind != argc - 1) {
2423 fprintf(stderr, "error: unexpected parameters encountered\n");
2428 rc = llapi_quotactl(mnt, &qctl);
2431 fprintf(stderr, "%s %s ", obd_type,
2432 obd_uuid2str(&qctl.obd_uuid));
2433 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
2440 #define BSLIMIT (1 << 0)
2441 #define BHLIMIT (1 << 1)
2442 #define ISLIMIT (1 << 2)
2443 #define IHLIMIT (1 << 3)
2445 int lfs_setquota(int argc, char **argv)
2448 struct if_quotactl qctl;
2449 char *mnt, *obd_type = (char *)qctl.obd_type;
2450 struct obd_dqblk *dqb = &qctl.qc_dqblk;
2451 struct option long_opts[] = {
2452 {"block-softlimit", required_argument, 0, 'b'},
2453 {"block-hardlimit", required_argument, 0, 'B'},
2454 {"group", required_argument, 0, 'g'},
2455 {"inode-softlimit", required_argument, 0, 'i'},
2456 {"inode-hardlimit", required_argument, 0, 'I'},
2457 {"user", required_argument, 0, 'u'},
2460 unsigned limit_mask = 0;
2463 if (has_times_option(argc, argv))
2464 return lfs_setquota_times(argc, argv);
2466 memset(&qctl, 0, sizeof(qctl));
2467 qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
2468 qctl.qc_type = UGQUOTA; /* UGQUOTA makes no sense for setquota,
2469 * so it can be used as a marker that qc_type
2470 * isn't reinitialized from command line */
2473 while ((c = getopt_long(argc, argv, "b:B:g:i:I:u:", long_opts, NULL)) != -1) {
2477 if (qctl.qc_type != UGQUOTA) {
2478 fprintf(stderr, "error: -u and -g can't be used"
2479 " more than once\n");
2482 qctl.qc_type = (c == 'u') ? USRQUOTA : GRPQUOTA;
2483 rc = name2id(&qctl.qc_id, optarg,
2484 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2486 qctl.qc_id = strtoul(optarg, &endptr, 10);
2487 if (*endptr != '\0') {
2488 fprintf(stderr, "error: can't find id "
2489 "for name %s\n", optarg);
2495 ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
2496 dqb->dqb_bsoftlimit >>= 10;
2497 limit_mask |= BSLIMIT;
2498 if (dqb->dqb_bsoftlimit &&
2499 dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
2500 fprintf(stderr, "warning: block softlimit is "
2501 "smaller than the miminal qunit size, "
2502 "please see the help of setquota or "
2503 "Lustre manual for details.\n");
2506 ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
2507 dqb->dqb_bhardlimit >>= 10;
2508 limit_mask |= BHLIMIT;
2509 if (dqb->dqb_bhardlimit &&
2510 dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
2511 fprintf(stderr, "warning: block hardlimit is "
2512 "smaller than the miminal qunit size, "
2513 "please see the help of setquota or "
2514 "Lustre manual for details.\n");
2517 ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
2518 limit_mask |= ISLIMIT;
2519 if (dqb->dqb_isoftlimit &&
2520 dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
2521 fprintf(stderr, "warning: inode softlimit is "
2522 "smaller than the miminal qunit size, "
2523 "please see the help of setquota or "
2524 "Lustre manual for details.\n");
2527 ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
2528 limit_mask |= IHLIMIT;
2529 if (dqb->dqb_ihardlimit &&
2530 dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
2531 fprintf(stderr, "warning: inode hardlimit is "
2532 "smaller than the miminal qunit size, "
2533 "please see the help of setquota or "
2534 "Lustre manual for details.\n");
2536 default: /* getopt prints error message for us when opterr != 0 */
2541 if (qctl.qc_type == UGQUOTA) {
2542 fprintf(stderr, "error: neither -u nor -g was specified\n");
2546 if (limit_mask == 0) {
2547 fprintf(stderr, "error: at least one limit must be specified\n");
2551 if (optind != argc - 1) {
2552 fprintf(stderr, "error: unexpected parameters encountered\n");
2558 if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
2559 (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
2560 /* sigh, we can't just set blimits/ilimits */
2561 struct if_quotactl tmp_qctl = {.qc_cmd = LUSTRE_Q_GETQUOTA,
2562 .qc_type = qctl.qc_type,
2563 .qc_id = qctl.qc_id};
2565 rc = llapi_quotactl(mnt, &tmp_qctl);
2567 fprintf(stderr, "error: setquota failed while retrieving"
2568 " current quota settings (%s)\n",
2573 if (!(limit_mask & BHLIMIT))
2574 dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
2575 if (!(limit_mask & BSLIMIT))
2576 dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
2577 if (!(limit_mask & IHLIMIT))
2578 dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
2579 if (!(limit_mask & ISLIMIT))
2580 dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
2582 /* Keep grace times if we have got no softlimit arguments */
2583 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
2584 dqb->dqb_valid |= QIF_BTIME;
2585 dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
2588 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
2589 dqb->dqb_valid |= QIF_ITIME;
2590 dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
2594 dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
2595 dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
2597 rc = llapi_quotactl(mnt, &qctl);
2600 fprintf(stderr, "%s %s ", obd_type,
2601 obd_uuid2str(&qctl.obd_uuid));
2602 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
2609 static inline char *type2name(int check_type)
2611 if (check_type == USRQUOTA)
2613 else if (check_type == GRPQUOTA)
2619 /* Converts seconds value into format string
2620 * result is returned in buf
2622 * 1. result is in descenting order: 1w2d3h4m5s
2623 * 2. zero fields are not filled (except for p. 3): 5d1s
2624 * 3. zero seconds value is presented as "0s"
2626 static char * __sec2str(time_t seconds, char *buf)
2628 const char spec[] = "smhdw";
2629 const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
2634 for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
2635 c = seconds / mult[i];
2637 if (c > 0 || (i == 0 && buf == tail))
2638 tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
2646 static void sec2str(time_t seconds, char *buf, int rc)
2653 tail = __sec2str(seconds, tail);
2655 if (rc && tail - buf < 39) {
2661 static void diff2str(time_t seconds, char *buf, time_t now)
2667 if (seconds <= now) {
2668 strcpy(buf, "none");
2671 __sec2str(seconds - now, buf);
2674 static void print_quota_title(char *name, struct if_quotactl *qctl,
2675 bool human_readable)
2677 printf("Disk quotas for %s %s (%cid %u):\n",
2678 type2name(qctl->qc_type), name,
2679 *type2name(qctl->qc_type), qctl->qc_id);
2680 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
2681 "Filesystem", human_readable ? "used" : "kbytes",
2682 "quota", "limit", "grace",
2683 "files", "quota", "limit", "grace");
2686 static void kbytes2str(__u64 num, char *buf, bool h)
2689 sprintf(buf, LPU64, num);
2692 sprintf(buf, "%5.4gT", (double)num / (1 << 30));
2694 sprintf(buf, "%5.4gG", (double)num / (1 << 20));
2696 sprintf(buf, "%5.4gM", (double)num / (1 << 10));
2698 sprintf(buf, LPU64"%s", num, "k");
2702 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
2709 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
2710 int bover = 0, iover = 0;
2711 struct obd_dqblk *dqb = &qctl->qc_dqblk;
2716 if (dqb->dqb_bhardlimit &&
2717 lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
2719 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
2720 if (dqb->dqb_btime > now) {
2727 if (dqb->dqb_ihardlimit &&
2728 dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
2730 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
2731 if (dqb->dqb_itime > now) {
2739 if (strlen(mnt) > 15)
2740 printf("%s\n%15s", mnt, "");
2742 printf("%15s", mnt);
2745 diff2str(dqb->dqb_btime, timebuf, now);
2747 kbytes2str(lustre_stoqb(dqb->dqb_curspace), strbuf, h);
2748 if (rc == -EREMOTEIO)
2749 sprintf(numbuf[0], "%s*", strbuf);
2751 sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
2752 "%s" : "[%s]", strbuf);
2754 kbytes2str(dqb->dqb_bsoftlimit, strbuf, h);
2755 if (type == QC_GENERAL)
2756 sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
2757 "%s" : "[%s]", strbuf);
2759 sprintf(numbuf[1], "%s", "-");
2761 kbytes2str(dqb->dqb_bhardlimit, strbuf, h);
2762 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
2763 "%s" : "[%s]", strbuf);
2765 printf(" %7s%c %6s %7s %7s",
2766 numbuf[0], bover ? '*' : ' ', numbuf[1],
2767 numbuf[2], bover > 1 ? timebuf : "-");
2770 diff2str(dqb->dqb_itime, timebuf, now);
2772 sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
2773 LPU64 : "["LPU64"]", dqb->dqb_curinodes);
2775 if (type == QC_GENERAL)
2776 sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
2777 LPU64 : "["LPU64"]", dqb->dqb_isoftlimit);
2779 sprintf(numbuf[1], "%s", "-");
2781 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
2782 LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
2784 if (type != QC_OSTIDX)
2785 printf(" %7s%c %6s %7s %7s",
2786 numbuf[0], iover ? '*' : ' ', numbuf[1],
2787 numbuf[2], iover > 1 ? timebuf : "-");
2789 printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
2792 } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
2793 qctl->qc_cmd == Q_GETOINFO) {
2797 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
2798 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
2799 printf("Block grace time: %s; Inode grace time: %s\n",
2800 bgtimebuf, igtimebuf);
2804 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
2805 bool h, __u64 *total)
2807 int rc = 0, rc1 = 0, count = 0;
2808 __u32 valid = qctl->qc_valid;
2810 rc = llapi_get_obd_count(mnt, &count, is_mdt);
2812 fprintf(stderr, "can not get %s count: %s\n",
2813 is_mdt ? "mdt": "ost", strerror(-rc));
2817 for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
2818 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
2819 rc = llapi_quotactl(mnt, qctl);
2821 /* It is remote client case. */
2822 if (-rc == EOPNOTSUPP) {
2829 fprintf(stderr, "quotactl %s%d failed.\n",
2830 is_mdt ? "mdt": "ost", qctl->qc_idx);
2834 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
2835 qctl->qc_valid, 0, h);
2836 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
2837 qctl->qc_dqblk.dqb_bhardlimit;
2840 qctl->qc_valid = valid;
2844 static int lfs_quota(int argc, char **argv)
2847 char *mnt, *name = NULL;
2848 struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
2849 .qc_type = UGQUOTA };
2850 char *obd_type = (char *)qctl.obd_type;
2851 char *obd_uuid = (char *)qctl.obd_uuid.uuid;
2852 int rc, rc1 = 0, rc2 = 0, rc3 = 0,
2853 verbose = 0, pass = 0, quiet = 0, inacc;
2855 __u32 valid = QC_GENERAL, idx = 0;
2856 __u64 total_ialloc = 0, total_balloc = 0;
2857 bool human_readable = false;
2860 while ((c = getopt(argc, argv, "gi:I:o:qtuvh")) != -1) {
2863 if (qctl.qc_type != UGQUOTA) {
2864 fprintf(stderr, "error: use either -u or -g\n");
2867 qctl.qc_type = USRQUOTA;
2870 if (qctl.qc_type != UGQUOTA) {
2871 fprintf(stderr, "error: use either -u or -g\n");
2874 qctl.qc_type = GRPQUOTA;
2877 qctl.qc_cmd = LUSTRE_Q_GETINFO;
2880 valid = qctl.qc_valid = QC_UUID;
2881 strlcpy(obd_uuid, optarg, sizeof(qctl.obd_uuid));
2884 valid = qctl.qc_valid = QC_MDTIDX;
2885 idx = qctl.qc_idx = atoi(optarg);
2888 valid = qctl.qc_valid = QC_OSTIDX;
2889 idx = qctl.qc_idx = atoi(optarg);
2898 human_readable = true;
2901 fprintf(stderr, "error: %s: option '-%c' "
2902 "unrecognized\n", argv[0], c);
2907 /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
2908 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == UGQUOTA &&
2909 optind == argc - 1) {
2911 memset(&qctl, 0, sizeof(qctl)); /* spoiled by print_*_quota */
2912 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
2913 qctl.qc_valid = valid;
2916 qctl.qc_type = USRQUOTA;
2917 qctl.qc_id = geteuid();
2919 qctl.qc_type = GRPQUOTA;
2920 qctl.qc_id = getegid();
2922 rc = id2name(&name, qctl.qc_id,
2923 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2926 /* lfs quota -u username /path/to/lustre/mount */
2927 } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
2928 /* options should be followed by u/g-name and mntpoint */
2929 if (optind + 2 != argc || qctl.qc_type == UGQUOTA) {
2930 fprintf(stderr, "error: missing quota argument(s)\n");
2934 name = argv[optind++];
2935 rc = name2id(&qctl.qc_id, name,
2936 (qctl.qc_type == USRQUOTA) ? USER : GROUP);
2938 qctl.qc_id = strtoul(name, &endptr, 10);
2939 if (*endptr != '\0') {
2940 fprintf(stderr, "error: can't find id for name "
2945 } else if (optind + 1 != argc || qctl.qc_type == UGQUOTA) {
2946 fprintf(stderr, "error: missing quota info argument(s)\n");
2952 rc1 = llapi_quotactl(mnt, &qctl);
2956 fprintf(stderr, "%s quotas are not enabled.\n",
2957 qctl.qc_type == USRQUOTA ? "user" : "group");
2960 fprintf(stderr, "Permission denied.\n");
2962 /* We already got a "No such file..." message. */
2965 fprintf(stderr, "Unexpected quotactl error: %s\n",
2970 if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && !quiet)
2971 print_quota_title(name, &qctl, human_readable);
2973 if (rc1 && *obd_type)
2974 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
2976 if (qctl.qc_valid != QC_GENERAL)
2979 inacc = (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) &&
2980 ((qctl.qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
2981 (QIF_LIMITS|QIF_USAGE));
2983 print_quota(mnt, &qctl, QC_GENERAL, rc1, human_readable);
2985 if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO &&
2989 rc2 = print_obd_quota(mnt, &qctl, 1, human_readable,
2991 rc3 = print_obd_quota(mnt, &qctl, 0, human_readable,
2993 kbytes2str(total_balloc, strbuf, human_readable);
2994 printf("Total allocated inode limit: "LPU64", total "
2995 "allocated block limit: %s\n", total_ialloc, strbuf);
2998 if (rc1 || rc2 || rc3 || inacc)
2999 printf("Some errors happened when getting quota info. "
3000 "Some devices may be not working or deactivated. "
3001 "The data in \"[]\" is inaccurate.\n");
3009 #endif /* HAVE_SYS_QUOTA_H! */
3011 static int flushctx_ioctl(char *mp)
3015 fd = open(mp, O_RDONLY);
3017 fprintf(stderr, "flushctx: error open %s: %s\n",
3018 mp, strerror(errno));
3022 rc = ioctl(fd, LL_IOC_FLUSHCTX);
3024 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
3025 mp, strerror(errno));
3031 static int lfs_flushctx(int argc, char **argv)
3033 int kdestroy = 0, c;
3034 char mntdir[PATH_MAX] = {'\0'};
3039 while ((c = getopt(argc, argv, "k")) != -1) {
3045 fprintf(stderr, "error: %s: option '-%c' "
3046 "unrecognized\n", argv[0], c);
3052 if ((rc = system("kdestroy > /dev/null")) != 0) {
3053 rc = WEXITSTATUS(rc);
3054 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
3058 if (optind >= argc) {
3059 /* flush for all mounted lustre fs. */
3060 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
3061 /* Check if we have a mount point */
3062 if (mntdir[0] == '\0')
3065 if (flushctx_ioctl(mntdir))
3068 mntdir[0] = '\0'; /* avoid matching in next loop */
3071 /* flush fs as specified */
3072 while (optind < argc) {
3073 if (flushctx_ioctl(argv[optind++]))
3080 static int lfs_lsetfacl(int argc, char **argv)
3083 return(llapi_lsetfacl(argc, argv));
3086 static int lfs_lgetfacl(int argc, char **argv)
3089 return(llapi_lgetfacl(argc, argv));
3092 static int lfs_rsetfacl(int argc, char **argv)
3095 return(llapi_rsetfacl(argc, argv));
3098 static int lfs_rgetfacl(int argc, char **argv)
3101 return(llapi_rgetfacl(argc, argv));
3104 static int lfs_cp(int argc, char **argv)
3106 return(llapi_cp(argc, argv));
3109 static int lfs_ls(int argc, char **argv)
3111 return(llapi_ls(argc, argv));
3114 static int lfs_changelog(int argc, char **argv)
3116 void *changelog_priv;
3117 struct changelog_ext_rec *rec;
3118 long long startrec = 0, endrec = 0;
3120 struct option long_opts[] = {
3121 {"follow", no_argument, 0, 'f'},
3124 char short_opts[] = "f";
3128 while ((rc = getopt_long(argc, argv, short_opts,
3129 long_opts, NULL)) != -1) {
3137 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3138 argv[0], argv[optind - 1]);
3145 mdd = argv[optind++];
3147 startrec = strtoll(argv[optind++], NULL, 10);
3149 endrec = strtoll(argv[optind++], NULL, 10);
3151 rc = llapi_changelog_start(&changelog_priv,
3152 CHANGELOG_FLAG_BLOCK |
3153 (follow ? CHANGELOG_FLAG_FOLLOW : 0),
3156 fprintf(stderr, "Can't start changelog: %s\n",
3157 strerror(errno = -rc));
3161 while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
3165 if (endrec && rec->cr_index > endrec) {
3166 llapi_changelog_free(&rec);
3169 if (rec->cr_index < startrec) {
3170 llapi_changelog_free(&rec);
3174 secs = rec->cr_time >> 30;
3175 gmtime_r(&secs, &ts);
3176 printf(LPU64" %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
3177 "0x%x t="DFID, rec->cr_index, rec->cr_type,
3178 changelog_type2str(rec->cr_type),
3179 ts.tm_hour, ts.tm_min, ts.tm_sec,
3180 (int)(rec->cr_time & ((1<<30) - 1)),
3181 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
3182 rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
3183 if (rec->cr_namelen)
3184 /* namespace rec includes parent and filename */
3185 printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
3186 rec->cr_namelen, rec->cr_name);
3188 if (!fid_is_zero(&rec->cr_sfid))
3189 printf(" s="DFID" sp="DFID" %.*s",
3190 PFID(&rec->cr_sfid), PFID(&rec->cr_spfid),
3191 changelog_rec_snamelen(rec),
3192 changelog_rec_sname(rec));
3195 llapi_changelog_free(&rec);
3198 llapi_changelog_fini(&changelog_priv);
3201 fprintf(stderr, "Changelog: %s\n", strerror(errno = -rc));
3203 return (rc == 1 ? 0 : rc);
3206 static int lfs_changelog_clear(int argc, char **argv)
3214 endrec = strtoll(argv[3], NULL, 10);
3216 rc = llapi_changelog_clear(argv[1], argv[2], endrec);
3218 fprintf(stderr, "%s error: %s\n", argv[0],
3219 strerror(errno = -rc));
3223 static int lfs_fid2path(int argc, char **argv)
3225 struct option long_opts[] = {
3226 {"cur", no_argument, 0, 'c'},
3227 {"link", required_argument, 0, 'l'},
3228 {"rec", required_argument, 0, 'r'},
3231 char short_opts[] = "cl:r:";
3232 char *device, *fid, *path;
3233 long long recno = -1;
3241 while ((rc = getopt_long(argc, argv, short_opts,
3242 long_opts, NULL)) != -1) {
3248 linkno = strtol(optarg, NULL, 10);
3251 recno = strtoll(optarg, NULL, 10);
3256 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3257 argv[0], argv[optind - 1]);
3265 device = argv[optind++];
3266 path = calloc(1, PATH_MAX);
3268 fprintf(stderr, "error: Not enough memory\n");
3273 while (optind < argc) {
3274 fid = argv[optind++];
3276 lnktmp = (linkno >= 0) ? linkno : 0;
3278 int oldtmp = lnktmp;
3279 long long rectmp = recno;
3281 rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
3284 fprintf(stderr, "%s: error on FID %s: %s\n",
3285 argv[0], fid, strerror(errno = -rc2));
3292 fprintf(stdout, "%lld ", rectmp);
3293 if (device[0] == '/') {
3294 fprintf(stdout, "%s", device);
3295 if (device[strlen(device) - 1] != '/')
3296 fprintf(stdout, "/");
3297 } else if (path[0] == '\0') {
3298 fprintf(stdout, "/");
3300 fprintf(stdout, "%s\n", path);
3303 /* specified linkno */
3305 if (oldtmp == lnktmp)
3315 static int lfs_path2fid(int argc, char **argv)
3318 const char *sep = "";
3328 while (*path != NULL) {
3329 int err = llapi_path2fid(*path, &fid);
3332 fprintf(stderr, "%s: can't get fid for %s: %s\n",
3333 argv[0], *path, strerror(-err));
3340 printf("%s%s"DFID"\n", *sep != '\0' ? *path : "", sep,
3349 static int lfs_data_version(int argc, char **argv)
3356 int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
3362 while ((c = getopt(argc, argv, "nrw")) != -1) {
3365 data_version_flags = 0;
3368 data_version_flags |= LL_DV_RD_FLUSH;
3371 data_version_flags |= LL_DV_WR_FLUSH;
3380 path = argv[optind];
3381 fd = open(path, O_RDONLY);
3383 err(errno, "cannot open file %s", path);
3385 rc = llapi_get_data_version(fd, &data_version, data_version_flags);
3387 err(errno, "cannot get version for %s", path);
3389 printf(LPU64 "\n", data_version);
3395 static int lfs_hsm_state(int argc, char **argv)
3400 struct hsm_user_state hus;
3408 rc = llapi_hsm_state_get(path, &hus);
3410 fprintf(stderr, "can't get hsm state for %s: %s\n",
3411 path, strerror(errno = -rc));
3415 /* Display path name and status flags */
3416 printf("%s: (0x%08x)", path, hus.hus_states);
3418 if (hus.hus_states & HS_RELEASED)
3419 printf(" released");
3420 if (hus.hus_states & HS_EXISTS)
3422 if (hus.hus_states & HS_DIRTY)
3424 if (hus.hus_states & HS_ARCHIVED)
3425 printf(" archived");
3426 /* Display user-settable flags */
3427 if (hus.hus_states & HS_NORELEASE)
3428 printf(" never_release");
3429 if (hus.hus_states & HS_NOARCHIVE)
3430 printf(" never_archive");
3431 if (hus.hus_states & HS_LOST)
3432 printf(" lost_from_hsm");
3434 if (hus.hus_archive_id != 0)
3435 printf(", archive_id:%d", hus.hus_archive_id);
3438 } while (++i < argc);
3443 #define LFS_HSM_SET 0
3444 #define LFS_HSM_CLEAR 1
3447 * Generic function to set or clear HSM flags.
3448 * Used by hsm_set and hsm_clear.
3450 * @mode if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
3452 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
3454 struct option long_opts[] = {
3455 {"lost", 0, 0, 'l'},
3456 {"norelease", 0, 0, 'r'},
3457 {"noarchive", 0, 0, 'a'},
3458 {"archived", 0, 0, 'A'},
3459 {"dirty", 0, 0, 'd'},
3460 {"exists", 0, 0, 'e'},
3463 char short_opts[] = "lraAde";
3472 while ((c = getopt_long(argc, argv, short_opts,
3473 long_opts, NULL)) != -1) {
3479 mask |= HS_NOARCHIVE;
3482 mask |= HS_ARCHIVED;
3485 mask |= HS_NORELEASE;
3496 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3497 argv[0], argv[optind - 1]);
3502 /* User should have specified a flag */
3506 while (optind < argc) {
3508 path = argv[optind];
3510 /* If mode == 0, this means we apply the mask. */
3511 if (mode == LFS_HSM_SET)
3512 rc = llapi_hsm_state_set(path, mask, 0, 0);
3514 rc = llapi_hsm_state_set(path, 0, mask, 0);
3517 fprintf(stderr, "Can't change hsm flags for %s: %s\n",
3518 path, strerror(errno = -rc));
3527 static int lfs_hsm_action(int argc, char **argv)
3532 struct hsm_current_action hca;
3533 struct hsm_extent he;
3534 enum hsm_user_action hua;
3535 enum hsm_progress_states hps;
3543 rc = llapi_hsm_current_action(path, &hca);
3545 fprintf(stderr, "can't get hsm action for %s: %s\n",
3546 path, strerror(errno = -rc));
3549 he = hca.hca_location;
3550 hua = hca.hca_action;
3551 hps = hca.hca_state;
3553 printf("%s: %s", path, hsm_user_action2name(hua));
3555 /* Skip file without action */
3556 if (hca.hca_action == HUA_NONE) {
3561 printf(" %s ", hsm_progress_state2name(hps));
3563 if ((hps == HPS_RUNNING) &&
3564 (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
3565 printf("("LPX64 " bytes moved)\n", he.length);
3566 else if ((he.offset + he.length) == LUSTRE_EOF)
3567 printf("(from "LPX64 " to EOF)\n", he.offset);
3569 printf("(from "LPX64 " to "LPX64")\n",
3570 he.offset, he.offset + he.length);
3572 } while (++i < argc);
3577 static int lfs_hsm_set(int argc, char **argv)
3579 return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
3582 static int lfs_hsm_clear(int argc, char **argv)
3584 return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
3588 * Check file state and return its fid, to be used by lfs_hsm_request().
3590 * \param[in] file Path to file to check
3591 * \param[in,out] fid Pointer to allocated lu_fid struct.
3592 * \param[in,out] last_dev Pointer to last device id used.
3594 * \return 0 on success.
3596 static int lfs_hsm_prepare_file(char *file, struct lu_fid *fid,
3602 rc = lstat(file, &st);
3604 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
3607 /* A request should be ... */
3608 if (*last_dev != st.st_dev && *last_dev != 0) {
3609 fprintf(stderr, "All files should be "
3610 "on the same filesystem: %s\n", file);
3613 *last_dev = st.st_dev;
3615 rc = llapi_path2fid(file, fid);
3617 fprintf(stderr, "Cannot read FID of %s: %s\n",
3618 file, strerror(-rc));
3624 static int lfs_hsm_request(int argc, char **argv, int action)
3626 struct option long_opts[] = {
3627 {"filelist", 1, 0, 'l'},
3628 {"data", 1, 0, 'D'},
3629 {"archive", 1, 0, 'a'},
3633 char short_opts[] = "l:D:a:";
3634 struct hsm_user_request *hur, *oldhur;
3639 char *filelist = NULL;
3640 char fullpath[PATH_MAX];
3641 char *opaque = NULL;
3645 int nbfile_alloc = 0;
3646 char some_file[PATH_MAX+1] = "";
3653 while ((c = getopt_long(argc, argv, short_opts,
3654 long_opts, NULL)) != -1) {
3663 if (action != HUA_ARCHIVE) {
3665 "error: -a is supported only "
3666 "when archiving\n");
3669 archive_id = atoi(optarg);
3674 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
3675 argv[0], argv[optind - 1]);
3680 /* All remaining args are files, so we have at least nbfile */
3681 nbfile = argc - optind;
3683 if ((nbfile == 0) && (filelist == NULL))
3687 opaque_len = strlen(opaque);
3689 /* Alloc the request structure with enough place to store all files
3690 * from command line. */
3691 hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
3693 fprintf(stderr, "Cannot create the request: %s\n",
3697 nbfile_alloc = nbfile;
3699 hur->hur_request.hr_action = action;
3700 hur->hur_request.hr_archive_id = archive_id;
3701 hur->hur_request.hr_flags = 0;
3703 /* All remaining args are files, add them */
3705 if (strlen(argv[optind]) > sizeof(some_file)-1) {
3709 strncpy(some_file, argv[optind], sizeof(some_file));
3712 for (i = 0; i < nbfile; i++) {
3713 hur->hur_user_item[i].hui_extent.length = -1;
3714 rc = lfs_hsm_prepare_file(argv[optind + i],
3715 &hur->hur_user_item[i].hui_fid,
3717 hur->hur_request.hr_itemcount++;
3722 /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
3724 /* If a filelist was specified, read the filelist from it. */
3725 if (filelist != NULL) {
3726 fp = fopen(filelist, "r");
3728 fprintf(stderr, "Cannot read the file list %s: %s\n",
3729 filelist, strerror(errno));
3734 while ((rc = getline(&line, &len, fp)) != -1) {
3735 struct hsm_user_item *hui;
3737 /* If allocated buffer was too small, gets something
3739 if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
3740 nbfile_alloc = nbfile_alloc * 2 + 1;
3742 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
3745 fprintf(stderr, "Cannot allocate "
3746 "the request: %s\n",
3753 memcpy(hur, oldhur, hur_len(oldhur));
3758 if (line[strlen(line) - 1] == '\n')
3759 line[strlen(line) - 1] = '\0';
3762 &hur->hur_user_item[hur->hur_request.hr_itemcount];
3763 hui->hui_extent.length = -1;
3764 rc = lfs_hsm_prepare_file(line, &hui->hui_fid,
3766 hur->hur_request.hr_itemcount++;
3772 if ((some_file[0] == '\0') &&
3773 (strlen(line) < sizeof(some_file)))
3774 strcpy(some_file, line);
3782 /* If a --data was used, add it to the request */
3783 hur->hur_request.hr_data_len = opaque_len;
3785 memcpy(hur_data(hur), opaque, opaque_len);
3787 /* Send the HSM request */
3788 if (realpath(some_file, fullpath) == NULL) {
3789 fprintf(stderr, "Could not find path '%s': %s\n",
3790 some_file, strerror(errno));
3792 rc = llapi_hsm_request(fullpath, hur);
3794 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
3795 some_file, strerror(-rc));
3804 static int lfs_hsm_archive(int argc, char **argv)
3806 return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
3809 static int lfs_hsm_restore(int argc, char **argv)
3811 return lfs_hsm_request(argc, argv, HUA_RESTORE);
3814 static int lfs_hsm_release(int argc, char **argv)
3816 return lfs_hsm_request(argc, argv, HUA_RELEASE);
3819 static int lfs_hsm_remove(int argc, char **argv)
3821 return lfs_hsm_request(argc, argv, HUA_REMOVE);
3824 static int lfs_hsm_cancel(int argc, char **argv)
3826 return lfs_hsm_request(argc, argv, HUA_CANCEL);
3829 static int lfs_swap_layouts(int argc, char **argv)
3834 return llapi_swap_layouts(argv[1], argv[2], 0, 0,
3835 SWAP_LAYOUTS_KEEP_MTIME |
3836 SWAP_LAYOUTS_KEEP_ATIME);
3839 int main(int argc, char **argv)
3845 Parser_init("lfs > ", cmdlist);
3848 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
3850 rc = Parser_commands();
3853 return rc < 0 ? -rc : rc;
3856 #ifdef _LUSTRE_IDL_H_
3857 /* Everything we need here should be included by lustreapi.h. */
3858 # error "lfs should not depend on lustre_idl.h"
3859 #endif /* _LUSTRE_IDL_H_ */