* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
* Use is subject to license terms.
*
- * Copyright (c) 2011, 2014, Intel Corporation.
+ * Copyright (c) 2011, 2015, Intel Corporation.
*/
/*
* This file is part of Lustre, http://www.lustre.org/
#include <err.h>
#include <pwd.h>
#include <grp.h>
+#include <sys/ioctl.h>
#include <sys/quota.h>
+#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
# include <sys/quota.h>
#endif
-#include <libcfs/libcfs.h>
+#include <libcfs/util/string.h>
#include <libcfs/util/ioctl.h>
#include <libcfs/util/parser.h>
#include <lustre/lustreapi.h>
#include <lustre_ver.h>
+#include <lustre_param.h>
+
+#ifndef ARRAY_SIZE
+# define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
+#endif /* !ARRAY_SIZE */
/* all functions */
static int lfs_setstripe(int argc, char **argv);
static int lfs_getname(int argc, char **argv);
static int lfs_check(int argc, char **argv);
#ifdef HAVE_SYS_QUOTA_H
-static int lfs_quotacheck(int argc, char **argv);
-static int lfs_quotaon(int argc, char **argv);
-static int lfs_quotaoff(int argc, char **argv);
static int lfs_setquota(int argc, char **argv);
static int lfs_quota(int argc, char **argv);
#endif
static int lfs_hsm_cancel(int argc, char **argv);
static int lfs_swap_layouts(int argc, char **argv);
static int lfs_mv(int argc, char **argv);
+static int lfs_ladvise(int argc, char **argv);
/* Setstripe and migrate share mostly the same parameters */
#define SSM_CMD_COMMON(cmd) \
#define MIGRATE_USAGE \
SSM_CMD_COMMON("migrate ") \
" [--block|-b]\n" \
+ " [--non-block|-n]\n" \
" <filename>\n" \
SSM_HELP_COMMON \
"\n" \
- "\tblock: Block file access during data migration\n" \
+ "\tblock: Block file access during data migration (default)\n" \
+ "\tnon-block: Abort migrations if concurrent access is detected\n" \
+
+static const char *progname;
+static bool file_lease_supported = true;
/* all available commands */
command_t cmdlist[] = {
"[for specified path only]\n"
"Usage: getname [-h]|[path ...] "},
#ifdef HAVE_SYS_QUOTA_H
- {"quotacheck", lfs_quotacheck, 0,
- "Scan the specified filesystem for disk usage, and create,\n"
- "or update quota files. Deprecated as of 2.4.0.\n"
- "usage: quotacheck [ -ug ] <filesystem>"},
- {"quotaon", lfs_quotaon, 0, "Turn filesystem"
- " quotas on. Deprecated as of 2.4.0.\n"
- "usage: quotaon [ -ugf ] <filesystem>"},
- {"quotaoff", lfs_quotaoff, 0, "Turn filesystem"
- " quotas off. Deprecated as of 2.4.0.\n"
- "usage: quotaoff [ -ug ] <filesystem>"},
{"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
"usage: setquota <-u|-g> <uname>|<uid>|<gname>|<gid>\n"
" -b <block-softlimit> -B <block-hardlimit>\n"
"usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
{"hsm_remove", lfs_hsm_remove, 0,
"Remove file copy from external storage.\n"
- "usage: hsm_remove [--filelist FILELIST] [--data DATA] <file> ..."},
+ "usage: hsm_remove [--filelist FILELIST] [--data DATA]\n"
+ " [--mntpath MOUNTPATH] [--archive NUM] <file|FID> ...\n"
+ "\n"
+ "Note: To remove files from the archive that have been deleted on\n"
+ "Lustre, set mntpath and optionally archive. In that case, all the\n"
+ "positional arguments and entries in the file list must be FIDs."
+ },
{"hsm_cancel", lfs_hsm_cancel, 0,
"Cancel requests related to specified files.\n"
"usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
{"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
"usage: swap_layouts <path1> <path2>"},
- {"migrate", lfs_setstripe, 0, "migrate file from one OST layout to "
- "another (may be not safe with concurrent writes).\n"
- MIGRATE_USAGE},
+ {"migrate", lfs_setstripe, 0,
+ "migrate a directory between MDTs.\n"
+ "usage: migrate --mdt-index <mdt_idx> [--verbose|-v] "
+ "<directory>\n"
+ "\tmdt_idx: index of the destination MDT\n"
+ "\n"
+ "migrate file objects from one OST "
+ "layout\nto another (may be not safe with concurent writes).\n"
+ "usage: migrate "
+ "[--stripe-count|-c] <stripe_count>\n"
+ " [--stripe-index|-i] <start_ost_index>\n"
+ " [--stripe-size|-S] <stripe_size>\n"
+ " [--pool|-p] <pool_name>\n"
+ " [--ost-list|-o] <ost_indices>\n"
+ " [--block|-b]\n"
+ " [--non-block|-n]\n"
+ " <file|directory>\n"
+ "\tstripe_count: number of OSTs to stripe a file over\n"
+ "\tstripe_ost_index: index of the first OST to stripe a file over\n"
+ "\tstripe_size: number of bytes to store before moving to the next OST\n"
+ "\tpool_name: name of the predefined pool of OSTs\n"
+ "\tost_indices: OSTs to stripe over, in order\n"
+ "\tblock: wait for the operation to return before continuing\n"
+ "\tnon-block: do not wait for the operation to return.\n"},
{"mv", lfs_mv, 0,
- "To move directories between MDTs.\n"
+ "To move directories between MDTs. This command is deprecated, "
+ "use \"migrate\" instead.\n"
"usage: mv <directory|filename> [--mdt-index|-M] <mdt_index> "
"[--verbose|-v]\n"},
+ {"ladvise", lfs_ladvise, 0,
+ "Provide servers with advice about access patterns for a file.\n"
+ "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
+ " [--background|-b]\n"
+ " {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
+ " <file> ..."},
{"help", Parser_help, 0, "help"},
{"exit", Parser_quit, 0, "quit"},
{"quit", Parser_quit, 0, "quit"},
{ 0, 0, 0, NULL }
};
-#define MIGRATION_BLOCKS 1
+
+#define MIGRATION_NONBLOCK 1
+
+/**
+ * Internal helper for migrate_copy_data(). Check lease and report error if
+ * need be.
+ *
+ * \param[in] fd File descriptor on which to check the lease.
+ * \param[out] lease_broken Set to true if the lease was broken.
+ * \param[in] group_locked Whether a group lock was taken or not.
+ * \param[in] path Name of the file being processed, for error
+ * reporting
+ *
+ * \retval 0 Migration can keep on going.
+ * \retval -errno Error occurred, abort migration.
+ */
+static int check_lease(int fd, bool *lease_broken, bool group_locked,
+ const char *path)
+{
+ int rc;
+
+ if (!file_lease_supported)
+ return 0;
+
+ rc = llapi_lease_check(fd);
+ if (rc > 0)
+ return 0; /* llapi_check_lease returns > 0 on success. */
+
+ if (!group_locked) {
+ fprintf(stderr, "%s: cannot migrate '%s': file busy\n",
+ progname, path);
+ rc = rc ? rc : -EAGAIN;
+ } else {
+ fprintf(stderr, "%s: external attempt to access file '%s' "
+ "blocked until migration ends.\n", progname, path);
+ rc = 0;
+ }
+ *lease_broken = true;
+ return rc;
+}
+
+static int migrate_copy_data(int fd_src, int fd_dst, size_t buf_size,
+ bool group_locked, const char *fname)
+{
+ void *buf = NULL;
+ ssize_t rsize = -1;
+ ssize_t wsize = 0;
+ size_t rpos = 0;
+ size_t wpos = 0;
+ off_t bufoff = 0;
+ int rc;
+ bool lease_broken = false;
+
+ /* Use a page-aligned buffer for direct I/O */
+ rc = posix_memalign(&buf, getpagesize(), buf_size);
+ if (rc != 0)
+ return -rc;
+
+ while (1) {
+ /* read new data only if we have written all
+ * previously read data */
+ if (wpos == rpos) {
+ if (!lease_broken) {
+ rc = check_lease(fd_src, &lease_broken,
+ group_locked, fname);
+ if (rc < 0)
+ goto out;
+ }
+ rsize = read(fd_src, buf, buf_size);
+ if (rsize < 0) {
+ rc = -errno;
+ fprintf(stderr, "%s: %s: read failed: %s\n",
+ progname, fname, strerror(-rc));
+ goto out;
+ }
+ rpos += rsize;
+ bufoff = 0;
+ }
+ /* eof ? */
+ if (rsize == 0)
+ break;
+
+ wsize = write(fd_dst, buf + bufoff, rpos - wpos);
+ if (wsize < 0) {
+ rc = -errno;
+ fprintf(stderr,
+ "%s: %s: write failed on volatile: %s\n",
+ progname, fname, strerror(-rc));
+ goto out;
+ }
+ wpos += wsize;
+ bufoff += wsize;
+ }
+
+ rc = fsync(fd_dst);
+ if (rc < 0) {
+ rc = -errno;
+ fprintf(stderr, "%s: %s: fsync failed: %s\n",
+ progname, fname, strerror(-rc));
+ }
+
+out:
+ free(buf);
+ return rc;
+}
+
+static int migrate_copy_timestamps(int fdv, const struct stat *st)
+{
+ struct timeval tv[2] = {
+ {.tv_sec = st->st_atime},
+ {.tv_sec = st->st_mtime}
+ };
+
+ return futimes(fdv, tv);
+}
+
+static int migrate_block(int fd, int fdv, const struct stat *st,
+ size_t buf_size, const char *name)
+{
+ __u64 dv1;
+ int gid;
+ int rc;
+ int rc2;
+
+ rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
+ if (rc < 0) {
+ fprintf(stderr, "%s: %s: cannot get dataversion: %s\n",
+ progname, name, strerror(-rc));
+ return rc;
+ }
+
+ do
+ gid = random();
+ while (gid == 0);
+
+ /* The grouplock blocks all concurrent accesses to the file.
+ * It has to be taken after llapi_get_data_version as it would
+ * block it too. */
+ rc = llapi_group_lock(fd, gid);
+ if (rc < 0) {
+ fprintf(stderr, "%s: %s: cannot get group lock: %s\n",
+ progname, name, strerror(-rc));
+ return rc;
+ }
+
+ rc = migrate_copy_data(fd, fdv, buf_size, true, name);
+ if (rc < 0) {
+ fprintf(stderr, "%s: %s: data copy failed\n", progname, name);
+ goto out_unlock;
+ }
+
+ /* Make sure we keep original atime/mtime values */
+ rc = migrate_copy_timestamps(fdv, st);
+ if (rc < 0) {
+ fprintf(stderr, "%s: %s: timestamp copy failed\n",
+ progname, name);
+ goto out_unlock;
+ }
+
+ /* swap layouts
+ * for a migration we need to check data version on file did
+ * not change.
+ *
+ * Pass in gid=0 since we already own grouplock. */
+ rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
+ SWAP_LAYOUTS_CHECK_DV1);
+ if (rc == -EAGAIN) {
+ fprintf(stderr, "%s: %s: dataversion changed during copy, "
+ "migration aborted\n", progname, name);
+ goto out_unlock;
+ } else if (rc < 0) {
+ fprintf(stderr, "%s: %s: cannot swap layouts: %s\n", progname,
+ name, strerror(-rc));
+ goto out_unlock;
+ }
+
+out_unlock:
+ rc2 = llapi_group_unlock(fd, gid);
+ if (rc2 < 0 && rc == 0) {
+ fprintf(stderr, "%s: %s: putting group lock failed: %s\n",
+ progname, name, strerror(-rc2));
+ rc = rc2;
+ }
+
+ return rc;
+}
+
+static int migrate_nonblock(int fd, int fdv, const struct stat *st,
+ size_t buf_size, const char *name)
+{
+ __u64 dv1;
+ __u64 dv2;
+ int rc;
+
+ rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
+ if (rc < 0) {
+ fprintf(stderr, "%s: %s: cannot get data version: %s\n",
+ progname, name, strerror(-rc));
+ return rc;
+ }
+
+ rc = migrate_copy_data(fd, fdv, buf_size, false, name);
+ if (rc < 0) {
+ fprintf(stderr, "%s: %s: data copy failed\n", progname, name);
+ return rc;
+ }
+
+ rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
+ if (rc != 0) {
+ fprintf(stderr, "%s: %s: cannot get data version: %s\n",
+ progname, name, strerror(-rc));
+ return rc;
+ }
+
+ if (dv1 != dv2) {
+ rc = -EAGAIN;
+ fprintf(stderr, "%s: %s: data version changed during "
+ "migration\n",
+ progname, name);
+ return rc;
+ }
+
+ /* Make sure we keep original atime/mtime values */
+ rc = migrate_copy_timestamps(fdv, st);
+ if (rc < 0) {
+ fprintf(stderr, "%s: %s: timestamp copy failed\n",
+ progname, name);
+ return rc;
+ }
+
+ /* Atomically put lease, swap layouts and close.
+ * for a migration we need to check data version on file did
+ * not change. */
+ rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
+ if (rc < 0) {
+ fprintf(stderr, "%s: %s: cannot swap layouts: %s\n",
+ progname, name, strerror(-rc));
+ return rc;
+ }
+
+ return 0;
+}
static int lfs_migrate(char *name, __u64 migration_flags,
struct llapi_stripe_param *param)
{
- int fd, fdv;
- char volatile_file[PATH_MAX +
- LUSTRE_VOLATILE_HDR_LEN + 4];
+ int fd = -1;
+ int fdv = -1;
char parent[PATH_MAX];
+ int mdt_index;
+ int random_value;
+ char volatile_file[sizeof(parent) +
+ LUSTRE_VOLATILE_HDR_LEN +
+ 2 * sizeof(mdt_index) +
+ 2 * sizeof(random_value) + 4];
char *ptr;
int rc;
- __u64 dv1;
struct lov_user_md *lum = NULL;
- int lumsz;
- int bufsz;
- void *buf = NULL;
- int rsize, wsize;
- __u64 rpos, wpos, bufoff;
- int gid;
- int have_gl = 0;
- struct stat st, stv;
+ int lum_size;
+ int buf_size;
+ bool have_lease_rdlck = false;
+ struct stat st;
+ struct stat stv;
/* find the right size for the IO and allocate the buffer */
- lumsz = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
- lum = malloc(lumsz);
+ lum_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
+ lum = malloc(lum_size);
if (lum == NULL) {
rc = -ENOMEM;
goto free;
* in case of a real error, a later call will fail with better
* error management */
if (rc < 0)
- bufsz = 1024*1024;
+ buf_size = 1024 * 1024;
else
- bufsz = lum->lmm_stripe_size;
- rc = posix_memalign(&buf, getpagesize(), bufsz);
- if (rc != 0) {
- rc = -rc;
+ buf_size = lum->lmm_stripe_size;
+
+ /* open file, direct io */
+ /* even if the file is only read, WR mode is nedeed to allow
+ * layout swap on fd */
+ fd = open(name, O_RDWR | O_DIRECT);
+ if (fd == -1) {
+ rc = -errno;
+ fprintf(stderr, "%s: %s: cannot open: %s\n", progname, name,
+ strerror(-rc));
goto free;
}
+ if (file_lease_supported) {
+ rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
+ if (rc == -EOPNOTSUPP) {
+ /* Older servers do not support file lease.
+ * Disable related checks. This opens race conditions
+ * as explained in LU-4840 */
+ file_lease_supported = false;
+ } else if (rc < 0) {
+ fprintf(stderr, "%s: %s: cannot get open lease: %s\n",
+ progname, name, strerror(-rc));
+ goto error;
+ } else {
+ have_lease_rdlck = true;
+ }
+ }
+
/* search for file directory pathname */
if (strlen(name) > sizeof(parent)-1) {
rc = -E2BIG;
- goto free;
+ goto error;
}
strncpy(parent, name, sizeof(parent));
ptr = strrchr(parent, '/');
if (ptr == NULL) {
if (getcwd(parent, sizeof(parent)) == NULL) {
rc = -errno;
- goto free;
+ goto error;
}
} else {
if (ptr == parent)
else
*ptr = '\0';
}
- rc = snprintf(volatile_file, sizeof(volatile_file), "%s/%s::", parent,
- LUSTRE_VOLATILE_HDR);
- if (rc >= sizeof(volatile_file)) {
- rc = -E2BIG;
- goto free;
+
+ rc = llapi_file_fget_mdtidx(fd, &mdt_index);
+ if (rc < 0) {
+ fprintf(stderr, "%s: %s: cannot get MDT index: %s\n",
+ progname, name, strerror(-rc));
+ goto error;
}
- /* create, open a volatile file, use caching (ie no directio) */
- /* exclusive create is not needed because volatile files cannot
- * conflict on name by construction */
- fdv = llapi_file_open_param(volatile_file, O_CREAT | O_WRONLY, 0644,
- param);
+ do {
+ random_value = random();
+ rc = snprintf(volatile_file, sizeof(volatile_file),
+ "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
+ mdt_index, random_value);
+ if (rc >= sizeof(volatile_file)) {
+ rc = -E2BIG;
+ goto error;
+ }
+
+ /* create, open a volatile file, use caching (ie no directio) */
+ fdv = llapi_file_open_param(volatile_file,
+ O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW,
+ S_IRUSR | S_IWUSR, param);
+ } while (fdv == -EEXIST);
+
if (fdv < 0) {
rc = fdv;
- fprintf(stderr, "cannot create volatile file in %s (%s)\n",
- parent, strerror(-rc));
- goto free;
- }
-
- /* open file, direct io */
- /* even if the file is only read, WR mode is nedeed to allow
- * layout swap on fd */
- fd = open(name, O_RDWR | O_DIRECT);
- if (fd == -1) {
- rc = -errno;
- fprintf(stderr, "cannot open %s (%s)\n", name, strerror(-rc));
- close(fdv);
- goto free;
+ fprintf(stderr, "%s: %s: cannot create volatile file in"
+ " directory: %s\n",
+ progname, parent, strerror(-rc));
+ goto error;
}
/* Not-owner (root?) special case.
rc = fstat(fd, &st);
if (rc != 0) {
rc = -errno;
- fprintf(stderr, "cannot stat %s (%s)\n", name,
+ fprintf(stderr, "%s: %s: cannot stat: %s\n", progname, name,
strerror(errno));
goto error;
}
rc = fstat(fdv, &stv);
if (rc != 0) {
rc = -errno;
- fprintf(stderr, "cannot stat %s (%s)\n", volatile_file,
- strerror(errno));
+ fprintf(stderr, "%s: %s: cannot stat: %s\n", progname,
+ volatile_file, strerror(errno));
goto error;
}
if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
rc = fchown(fdv, st.st_uid, st.st_gid);
if (rc != 0) {
rc = -errno;
- fprintf(stderr, "cannot chown %s (%s)\n", name,
- strerror(errno));
+ fprintf(stderr, "%s: %s: cannot chown: %s\n", progname,
+ name, strerror(errno));
goto error;
}
}
- /* get file data version */
- rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
- if (rc != 0) {
- fprintf(stderr, "cannot get dataversion on %s (%s)\n",
- name, strerror(-rc));
- goto error;
- }
-
- do
- gid = random();
- while (gid == 0);
- if (migration_flags & MIGRATION_BLOCKS) {
- /* take group lock to limit concurrent access
- * this will be no more needed when exclusive access will
- * be implemented (see LU-2919) */
- /* group lock is taken after data version read because it
- * blocks data version call */
- rc = llapi_group_lock(fd, gid);
- if (rc < 0) {
- fprintf(stderr, "cannot get group lock on %s (%s)\n",
- name, strerror(-rc));
- goto error;
+ if (migration_flags & MIGRATION_NONBLOCK && file_lease_supported) {
+ rc = migrate_nonblock(fd, fdv, &st, buf_size, name);
+ if (rc == 0) {
+ have_lease_rdlck = false;
+ fdv = -1; /* The volatile file is closed as we put the
+ * lease in non-blocking mode. */
}
- have_gl = 1;
- }
-
- /* copy data */
- rpos = 0;
- wpos = 0;
- bufoff = 0;
- rsize = -1;
- do {
- /* read new data only if we have written all
- * previously read data */
- if (wpos == rpos) {
- rsize = read(fd, buf, bufsz);
- if (rsize < 0) {
- rc = -errno;
- fprintf(stderr, "read failed on %s"
- " (%s)\n", name,
- strerror(-rc));
- goto error;
- }
- rpos += rsize;
- bufoff = 0;
- }
- /* eof ? */
- if (rsize == 0)
- break;
- wsize = write(fdv, buf + bufoff, rpos - wpos);
- if (wsize < 0) {
- rc = -errno;
- fprintf(stderr, "write failed on volatile"
- " for %s (%s)\n", name, strerror(-rc));
- goto error;
- }
- wpos += wsize;
- bufoff += wsize;
- } while (1);
-
- /* flush data */
- fsync(fdv);
-
- if (migration_flags & MIGRATION_BLOCKS) {
- /* give back group lock */
- rc = llapi_group_unlock(fd, gid);
- if (rc < 0)
- fprintf(stderr, "cannot put group lock on %s (%s)\n",
- name, strerror(-rc));
- have_gl = 0;
- }
-
- /* swap layouts
- * for a migration we need to:
- * - check data version on file did not change
- * - keep file mtime
- * - keep file atime
- */
- rc = llapi_fswap_layouts(fd, fdv, dv1, 0,
- SWAP_LAYOUTS_CHECK_DV1 |
- SWAP_LAYOUTS_KEEP_MTIME |
- SWAP_LAYOUTS_KEEP_ATIME);
- if (rc == -EAGAIN) {
- fprintf(stderr, "%s: dataversion changed during copy, "
- "migration aborted\n", name);
- goto error;
+ } else {
+ /* Blocking mode (forced if servers do not support file lease).
+ * It is also the default mode, since we cannot distinguish
+ * between a broken lease and a server that does not support
+ * atomic swap/close (LU-6785) */
+ rc = migrate_block(fd, fdv, &st, buf_size, name);
}
- if (rc != 0)
- fprintf(stderr, "%s: swap layout to new file failed: %s\n",
- name, strerror(-rc));
error:
- /* give back group lock */
- if ((migration_flags & MIGRATION_BLOCKS) && have_gl) {
- int rc2;
+ if (have_lease_rdlck)
+ llapi_lease_put(fd);
- /* we keep the original error in rc */
- rc2 = llapi_group_unlock(fd, gid);
- if (rc2 < 0)
- fprintf(stderr, "cannot put group lock on %s (%s)\n",
- name, strerror(-rc2));
- }
+ if (fd >= 0)
+ close(fd);
+
+ if (fdv >= 0)
+ close(fdv);
- close(fdv);
- close(fd);
free:
if (lum)
free(lum);
- if (buf)
- free(buf);
+
return rc;
}
/* functions */
static int lfs_setstripe(int argc, char **argv)
{
- struct llapi_stripe_param *param;
+ struct llapi_stripe_param *param = NULL;
+ struct find_param migrate_mdt_param = {
+ .fp_max_depth = -1,
+ .fp_mdt_index = -1,
+ };
char *fname;
int result;
+ int result2 = 0;
unsigned long long st_size;
int st_offset, st_count;
char *end;
char *stripe_off_arg = NULL;
char *stripe_count_arg = NULL;
char *pool_name_arg = NULL;
+ char *mdt_idx_arg = NULL;
unsigned long long size_units = 1;
bool migrate_mode = false;
+ bool migration_block = false;
__u64 migration_flags = 0;
__u32 osts[LOV_MAX_STRIPE_COUNT] = { 0 };
int nr_osts = 0;
struct option long_opts[] = {
- /* valid only in migrate mode */
+ /* --block is only valid in migrate mode */
{"block", no_argument, 0, 'b'},
#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
/* This formerly implied "stripe-count", but was explicitly
#endif
{"stripe-index", required_argument, 0, 'i'},
{"stripe_index", required_argument, 0, 'i'},
+ {"mdt-index", required_argument, 0, 'm'},
+ {"mdt_index", required_argument, 0, 'm'},
+ /* --non-block is only valid in migrate mode */
+ {"non-block", no_argument, 0, 'n'},
{"ost-list", required_argument, 0, 'o'},
{"ost_list", required_argument, 0, 'o'},
{"pool", required_argument, 0, 'p'},
#endif
{"stripe-size", required_argument, 0, 'S'},
{"stripe_size", required_argument, 0, 'S'},
+ /* --verbose is only valid in migrate mode */
+ {"verbose", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
if (strcmp(argv[0], "migrate") == 0)
migrate_mode = true;
- while ((c = getopt_long(argc, argv, "bc:di:o:p:s:S:",
+ while ((c = getopt_long(argc, argv, "bc:di:m:no:p:s:S:v",
long_opts, NULL)) >= 0) {
switch (c) {
case 0:
case 'b':
if (!migrate_mode) {
fprintf(stderr, "--block is valid only for"
- " migrate mode");
+ " migrate mode\n");
return CMD_HELP;
}
- migration_flags |= MIGRATION_BLOCKS;
+ migration_block = true;
break;
case 'c':
#if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
delete = 1;
break;
case 'o':
- nr_osts = parse_targets(osts, ARRAY_SIZE(osts), nr_osts,
- optarg);
+ nr_osts = parse_targets(osts,
+ sizeof(osts) / sizeof(__u32),
+ nr_osts, optarg);
if (nr_osts < 0) {
fprintf(stderr,
"error: %s: bad OST indices '%s'\n",
#endif
stripe_off_arg = optarg;
break;
+ case 'm':
+ if (!migrate_mode) {
+ fprintf(stderr, "--mdt-index is valid only for"
+ " migrate mode\n");
+ return CMD_HELP;
+ }
+ mdt_idx_arg = optarg;
+ break;
+ case 'n':
+ if (!migrate_mode) {
+ fprintf(stderr, "--non-block is valid only for"
+ " migrate mode\n");
+ return CMD_HELP;
+ }
+ migration_flags |= MIGRATION_NONBLOCK;
+ break;
#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 53, 0)
case 's':
#if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 6, 53, 0)
case 'p':
pool_name_arg = optarg;
break;
+ case 'v':
+ if (!migrate_mode) {
+ fprintf(stderr, "--verbose is valid only for"
+ " migrate mode\n");
+ return CMD_HELP;
+ }
+ migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
+ break;
default:
return CMD_HELP;
}
return CMD_HELP;
}
- if (pool_name_arg && strlen(pool_name_arg) > LOV_MAXPOOLNAME) {
+ if (mdt_idx_arg != NULL && optind > 3) {
+ fprintf(stderr, "error: %s: cannot specify -m with other "
+ "options\n", argv[0]);
+ return CMD_HELP;
+ }
+
+ if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
fprintf(stderr,
- "error: %s: pool name '%s' is too long (max is %d characters)\n",
- argv[0], pool_name_arg, LOV_MAXPOOLNAME);
+ "error: %s: cannot specify --non-block and --block\n",
+ argv[0]);
return CMD_HELP;
}
+ if (pool_name_arg != NULL) {
+ char *ptr;
+ int rc;
+
+ ptr = strchr(pool_name_arg, '.');
+ if (ptr == NULL) {
+ ptr = pool_name_arg;
+ } else {
+ if ((ptr - pool_name_arg) == 0) {
+ fprintf(stderr, "error: %s: fsname is empty "
+ "in pool name '%s'\n",
+ argv[0], pool_name_arg);
+ return CMD_HELP;
+ }
+
+ ++ptr;
+ }
+
+ rc = lustre_is_poolname_valid(ptr, 1, LOV_MAXPOOLNAME);
+ if (rc == -1) {
+ fprintf(stderr, "error: %s: poolname '%s' is "
+ "empty\n",
+ argv[0], pool_name_arg);
+ return CMD_HELP;
+ } else if (rc == -2) {
+ fprintf(stderr, "error: %s: pool name '%s' is too long "
+ "(max is %d characters)\n",
+ argv[0], pool_name_arg, LOV_MAXPOOLNAME);
+ return CMD_HELP;
+ } else if (rc > 0) {
+ fprintf(stderr, "error: %s: char '%c' not allowed in "
+ "pool name '%s'\n",
+ argv[0], rc, pool_name_arg);
+ return CMD_HELP;
+ }
+ }
+
/* get the stripe size */
if (stripe_size_arg != NULL) {
result = llapi_parse_size(stripe_size_arg, &st_size,
}
}
- /* initialize stripe parameters */
- param = calloc(1, offsetof(typeof(*param), lsp_osts[nr_osts]));
- if (param == NULL) {
- fprintf(stderr, "error: %s: run out of memory\n", argv[0]);
- return CMD_HELP;
- }
-
- param->lsp_stripe_size = st_size;
- param->lsp_stripe_offset = st_offset;
- param->lsp_stripe_count = st_count;
- param->lsp_stripe_pattern = 0;
- param->lsp_pool = pool_name_arg;
- param->lsp_is_specific = false;
- if (nr_osts > 0) {
- if (st_count > 0 && nr_osts != st_count) {
- fprintf(stderr, "error: %s: stripe count '%d' doesn't "
- "match the number of OSTs: %d\n",
- argv[0], st_count, nr_osts);
+ if (mdt_idx_arg != NULL) {
+ /* initialize migrate mdt parameters */
+ migrate_mdt_param.fp_mdt_index = strtoul(mdt_idx_arg, &end, 0);
+ if (*end != '\0') {
+ fprintf(stderr, "error: %s: bad MDT index '%s'\n",
+ argv[0], mdt_idx_arg);
+ return CMD_HELP;
+ }
+ migrate_mdt_param.fp_migrate = 1;
+ } else {
+ /* initialize stripe parameters */
+ param = calloc(1, offsetof(typeof(*param), lsp_osts[nr_osts]));
+ if (param == NULL) {
+ fprintf(stderr, "error: %s: run out of memory\n",
+ argv[0]);
return CMD_HELP;
}
- param->lsp_is_specific = true;
- param->lsp_stripe_count = nr_osts;
- memcpy(param->lsp_osts, osts, sizeof(*osts) * nr_osts);
+ param->lsp_stripe_size = st_size;
+ param->lsp_stripe_offset = st_offset;
+ param->lsp_stripe_count = st_count;
+ param->lsp_stripe_pattern = 0;
+ param->lsp_pool = pool_name_arg;
+ param->lsp_is_specific = false;
+ if (nr_osts > 0) {
+ if (st_count > 0 && nr_osts != st_count) {
+ fprintf(stderr, "error: %s: stripe count '%d' "
+ "doesn't match the number of OSTs: %d\n"
+ , argv[0], st_count, nr_osts);
+ free(param);
+ return CMD_HELP;
+ }
+
+ param->lsp_is_specific = true;
+ param->lsp_stripe_count = nr_osts;
+ memcpy(param->lsp_osts, osts, sizeof(*osts) * nr_osts);
+ }
}
- do {
+ for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
if (!migrate_mode) {
result = llapi_file_open_param(fname,
O_CREAT | O_WRONLY,
close(result);
result = 0;
}
+ } else if (mdt_idx_arg != NULL) {
+ result = llapi_migrate_mdt(fname, &migrate_mdt_param);
} else {
result = lfs_migrate(fname, migration_flags, param);
}
if (result) {
+ /* Save the first error encountered. */
+ if (result2 == 0)
+ result2 = result;
fprintf(stderr,
- "error: %s: %s stripe file '%s' failed\n",
+ "error: %s: %s file '%s' failed\n",
argv[0], migrate_mode ? "migrate" : "create",
fname);
- break;
+ continue;
}
- fname = argv[++optind];
- } while (fname != NULL);
+ }
free(param);
- return result;
+ return result2;
}
static int lfs_poollist(int argc, char **argv)
}
param.fp_migrate = 1;
- rc = llapi_mv(argv[optind], ¶m);
+ rc = llapi_migrate_mdt(argv[optind], ¶m);
if (rc != 0)
fprintf(stderr, "%s: cannot migrate '%s' to MDT%04x: %s\n",
argv[0], argv[optind], param.fp_mdt_index,
}
#ifdef HAVE_SYS_QUOTA_H
-static int lfs_quotacheck(int argc, char **argv)
-{
- int c, check_type = 0;
- char *mnt;
- struct if_quotacheck qchk;
- struct if_quotactl qctl;
- char *obd_type = (char *)qchk.obd_type;
- int rc;
-
- memset(&qchk, 0, sizeof(qchk));
-
- while ((c = getopt(argc, argv, "gu")) != -1) {
- switch (c) {
- case 'u':
- check_type |= 0x01;
- break;
- case 'g':
- check_type |= 0x02;
- break;
- default:
- fprintf(stderr, "error: %s: option '-%c' "
- "unrecognized\n", argv[0], c);
- return CMD_HELP;
- }
- }
-
- if (check_type)
- check_type--;
- else /* do quotacheck for both user & group quota by default */
- check_type = 0x02;
-
- if (argc == optind)
- return CMD_HELP;
-
- mnt = argv[optind];
-
- rc = llapi_quotacheck(mnt, check_type);
- if (rc == -EOPNOTSUPP) {
- fprintf(stderr, "error: quotacheck not supported by the quota "
- "master.\nPlease note that quotacheck is deprecated as "
- "of lustre 2.4.0 since space accounting is always "
- "enabled.\nFilesystems not formatted with 2.4 utils or "
- "beyond can be upgraded with tunefs.lustre --quota.\n");
- return rc;
- } else if (rc) {
- fprintf(stderr, "quotacheck failed: %s\n", strerror(-rc));
- return rc;
- }
-
- rc = llapi_poll_quotacheck(mnt, &qchk);
- if (rc) {
- if (*obd_type)
- fprintf(stderr, "%s %s ", obd_type,
- obd_uuid2str(&qchk.obd_uuid));
- fprintf(stderr, "quota check failed: %s\n", strerror(-rc));
- return rc;
- }
-
- memset(&qctl, 0, sizeof(qctl));
- qctl.qc_cmd = LUSTRE_Q_QUOTAON;
- qctl.qc_type = check_type;
- rc = llapi_quotactl(mnt, &qctl);
- if (rc && rc != -EALREADY) {
- if (*obd_type)
- fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
- obd_uuid2str(&qctl.obd_uuid));
- fprintf(stderr, "%s turn on quota failed: %s\n",
- argv[0], strerror(-rc));
- return rc;
- }
-
- return 0;
-}
-
-static int lfs_quotaon(int argc, char **argv)
-{
- int c;
- char *mnt;
- struct if_quotactl qctl;
- char *obd_type = (char *)qctl.obd_type;
- int rc;
-
- memset(&qctl, 0, sizeof(qctl));
- qctl.qc_cmd = LUSTRE_Q_QUOTAON;
-
- while ((c = getopt(argc, argv, "fgu")) != -1) {
- switch (c) {
- case 'u':
- qctl.qc_type |= 0x01;
- break;
- case 'g':
- qctl.qc_type |= 0x02;
- break;
- case 'f':
- qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
- break;
- default:
- fprintf(stderr, "error: %s: option '-%c' "
- "unrecognized\n", argv[0], c);
- return CMD_HELP;
- }
- }
-
- if (qctl.qc_type)
- qctl.qc_type--;
- else /* by default, enable quota for both user & group */
- qctl.qc_type = 0x02;
-
- if (argc == optind)
- return CMD_HELP;
-
- mnt = argv[optind];
-
- rc = llapi_quotactl(mnt, &qctl);
- if (rc) {
- if (rc == -EOPNOTSUPP) {
- fprintf(stderr, "error: quotaon not supported by the "
- "quota master.\nPlease note that quotaon/off is"
- " deprecated as of lustre 2.4.0.\nQuota "
- "enforcement should now be enabled on the MGS "
- "via:\nmgs# lctl conf_param ${FSNAME}.quota."
- "<ost|mdt>=<u|g|ug>\n(ost for block quota, mdt "
- "for inode quota, u for user and g for group"
- "\n");
- } else if (rc == -EALREADY) {
- rc = 0;
- } else if (rc == -ENOENT) {
- fprintf(stderr, "error: cannot find quota database, "
- "make sure you have run quotacheck\n");
- } else {
- if (*obd_type)
- fprintf(stderr, "%s %s ", obd_type,
- obd_uuid2str(&qctl.obd_uuid));
- fprintf(stderr, "%s failed: %s\n", argv[0],
- strerror(-rc));
- }
- }
-
- return rc;
-}
-
-static int lfs_quotaoff(int argc, char **argv)
-{
- int c;
- char *mnt;
- struct if_quotactl qctl;
- char *obd_type = (char *)qctl.obd_type;
- int rc;
-
- memset(&qctl, 0, sizeof(qctl));
- qctl.qc_cmd = LUSTRE_Q_QUOTAOFF;
-
- while ((c = getopt(argc, argv, "gu")) != -1) {
- switch (c) {
- case 'u':
- qctl.qc_type |= 0x01;
- break;
- case 'g':
- qctl.qc_type |= 0x02;
- break;
- default:
- fprintf(stderr, "error: %s: option '-%c' "
- "unrecognized\n", argv[0], c);
- return CMD_HELP;
- }
- }
-
- if (qctl.qc_type)
- qctl.qc_type--;
- else /* by default, disable quota for both user & group */
- qctl.qc_type = 0x02;
-
- if (argc == optind)
- return CMD_HELP;
-
- mnt = argv[optind];
-
- rc = llapi_quotactl(mnt, &qctl);
- if (rc) {
- if (rc == -EOPNOTSUPP) {
- fprintf(stderr, "error: quotaoff not supported by the "
- "quota master.\nPlease note that quotaon/off is"
- " deprecated as of lustre 2.4.0.\nQuota "
- "enforcement can be disabled on the MGS via:\n"
- "mgs# lctl conf_param ${FSNAME}.quota.<ost|mdt>"
- "=\"\"\n");
- } else if (rc == -EALREADY) {
- rc = 0;
- } else {
- if (*obd_type)
- fprintf(stderr, "%s %s ", obd_type,
- obd_uuid2str(&qctl.obd_uuid));
- fprintf(stderr, "quotaoff failed: %s\n",
- strerror(-rc));
- }
- }
-
- return rc;
-}
-
#define ARG2INT(nr, str, msg) \
do { \
char *endp; \
"files", "quota", "limit", "grace");
}
-static void kbytes2str(__u64 num, char *buf, bool h)
+static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
{
if (!h) {
- sprintf(buf, LPU64, num);
+ snprintf(buf, buflen, "%ju", (uintmax_t)num);
} else {
- if (num >> 30)
- sprintf(buf, "%5.4gT", (double)num / (1 << 30));
+ if (num >> 40)
+ snprintf(buf, buflen, "%5.4gP",
+ (double)num / ((__u64)1 << 40));
+ else if (num >> 30)
+ snprintf(buf, buflen, "%5.4gT",
+ (double)num / (1 << 30));
else if (num >> 20)
- sprintf(buf, "%5.4gG", (double)num / (1 << 20));
+ snprintf(buf, buflen, "%5.4gG",
+ (double)num / (1 << 20));
else if (num >> 10)
- sprintf(buf, "%5.4gM", (double)num / (1 << 10));
+ snprintf(buf, buflen, "%5.4gM",
+ (double)num / (1 << 10));
else
- sprintf(buf, LPU64"%s", num, "k");
+ snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
}
}
+#define STRBUF_LEN 32
static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
int rc, bool h)
{
if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA) {
int bover = 0, iover = 0;
struct obd_dqblk *dqb = &qctl->qc_dqblk;
- char numbuf[3][32];
+ char numbuf[3][STRBUF_LEN];
char timebuf[40];
- char strbuf[32];
+ char strbuf[STRBUF_LEN];
if (dqb->dqb_bhardlimit &&
lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
if (bover)
diff2str(dqb->dqb_btime, timebuf, now);
- kbytes2str(lustre_stoqb(dqb->dqb_curspace), strbuf, h);
+ kbytes2str(lustre_stoqb(dqb->dqb_curspace),
+ strbuf, sizeof(strbuf), h);
if (rc == -EREMOTEIO)
sprintf(numbuf[0], "%s*", strbuf);
else
sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
"%s" : "[%s]", strbuf);
- kbytes2str(dqb->dqb_bsoftlimit, strbuf, h);
+ kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
if (type == QC_GENERAL)
sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
"%s" : "[%s]", strbuf);
else
sprintf(numbuf[1], "%s", "-");
- kbytes2str(dqb->dqb_bhardlimit, strbuf, h);
+ kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
"%s" : "[%s]", strbuf);
diff2str(dqb->dqb_itime, timebuf, now);
sprintf(numbuf[0], (dqb->dqb_valid & QIF_INODES) ?
- LPU64 : "["LPU64"]", dqb->dqb_curinodes);
+ "%ju" : "[%ju]", (uintmax_t)dqb->dqb_curinodes);
if (type == QC_GENERAL)
sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
- LPU64 : "["LPU64"]", dqb->dqb_isoftlimit);
+ "%ju" : "[%ju]",
+ (uintmax_t)dqb->dqb_isoftlimit);
else
sprintf(numbuf[1], "%s", "-");
sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
- LPU64 : "["LPU64"]", dqb->dqb_ihardlimit);
+ "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
if (type != QC_OSTIDX)
printf(" %7s%c %6s %7s %7s",
if (qctl.qc_valid == QC_GENERAL && qctl.qc_cmd != LUSTRE_Q_GETINFO &&
verbose) {
- char strbuf[32];
+ char strbuf[STRBUF_LEN];
rc2 = print_obd_quota(mnt, &qctl, 1, human_readable,
&total_ialloc);
rc3 = print_obd_quota(mnt, &qctl, 0, human_readable,
&total_balloc);
- kbytes2str(total_balloc, strbuf, human_readable);
- printf("Total allocated inode limit: "LPU64", total "
- "allocated block limit: %s\n", total_ialloc, strbuf);
+ kbytes2str(total_balloc, strbuf, sizeof(strbuf),
+ human_readable);
+ printf("Total allocated inode limit: %ju, total "
+ "allocated block limit: %s\n", (uintmax_t)total_ialloc,
+ strbuf);
}
if (rc1 || rc2 || rc3 || inacc)
secs = rec->cr_time >> 30;
gmtime_r(&secs, &ts);
- printf(LPU64" %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
- "0x%x t="DFID, rec->cr_index, rec->cr_type,
+ printf("%ju %02d%-5s %02d:%02d:%02d.%06d %04d.%02d.%02d "
+ "0x%x t="DFID, (uintmax_t) rec->cr_index, rec->cr_type,
changelog_type2str(rec->cr_type),
ts.tm_hour, ts.tm_min, ts.tm_sec,
(int)(rec->cr_time & ((1<<30) - 1)),
if (rc < 0)
err(errno, "cannot get version for %s", path);
else
- printf(LPU64 "\n", data_version);
+ printf("%ju" "\n", (uintmax_t)data_version);
close(fd);
return rc;
*
* \return 0 on success.
*/
-static int lfs_hsm_prepare_file(char *file, struct lu_fid *fid,
+static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
dev_t *last_dev)
{
struct stat st;
return 0;
}
+/* Fill an HSM HUR item with a given file name.
+ *
+ * If mntpath is set, then the filename is actually a FID, and no
+ * lookup on the filesystem will be performed.
+ *
+ * \param[in] hur the user request to fill
+ * \param[in] idx index of the item inside the HUR to fill
+ * \param[in] mntpath mountpoint of Lustre
+ * \param[in] fname filename (if mtnpath is NULL)
+ * or FID (if mntpath is set)
+ * \param[in] last_dev pointer to last device id used
+ *
+ * \retval 0 on success
+ * \retval CMD_HELP or a negative errno on error
+ */
+static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
+ const char *mntpath, const char *fname,
+ dev_t *last_dev)
+{
+ struct hsm_user_item *hui = &hur->hur_user_item[idx];
+ int rc;
+
+ hui->hui_extent.length = -1;
+
+ if (mntpath != NULL) {
+ if (*fname == '[')
+ fname++;
+ rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
+ if (rc == 3) {
+ rc = 0;
+ } else {
+ fprintf(stderr, "hsm: '%s' is not a valid FID\n",
+ fname);
+ rc = -EINVAL;
+ }
+ } else {
+ rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
+ }
+
+ if (rc == 0)
+ hur->hur_request.hr_itemcount++;
+
+ return rc;
+}
+
static int lfs_hsm_request(int argc, char **argv, int action)
{
struct option long_opts[] = {
{"filelist", 1, 0, 'l'},
{"data", 1, 0, 'D'},
{"archive", 1, 0, 'a'},
+ {"mntpath", 1, 0, 'm'},
{0, 0, 0, 0}
};
dev_t last_dev = 0;
- char short_opts[] = "l:D:a:";
+ char short_opts[] = "l:D:a:m:";
struct hsm_user_request *hur, *oldhur;
int c, i;
size_t len;
int archive_id = 0;
FILE *fp;
int nbfile_alloc = 0;
- char some_file[PATH_MAX+1] = "";
+ char *some_file = NULL;
+ char *mntpath = NULL;
int rc;
if (argc < 2)
opaque = optarg;
break;
case 'a':
- if (action != HUA_ARCHIVE) {
+ if (action != HUA_ARCHIVE &&
+ action != HUA_REMOVE) {
fprintf(stderr,
"error: -a is supported only "
- "when archiving\n");
+ "when archiving or removing\n");
return CMD_HELP;
}
archive_id = atoi(optarg);
break;
+ case 'm':
+ if (some_file == NULL) {
+ mntpath = optarg;
+ some_file = strdup(optarg);
+ }
+ break;
case '?':
return CMD_HELP;
default:
hur->hur_request.hr_flags = 0;
/* All remaining args are files, add them */
- if (nbfile != 0) {
- if (strlen(argv[optind]) > sizeof(some_file)-1) {
- free(hur);
- return -E2BIG;
- }
- strncpy(some_file, argv[optind], sizeof(some_file));
- }
+ if (nbfile != 0 && some_file == NULL)
+ some_file = strdup(argv[optind]);
for (i = 0; i < nbfile; i++) {
- hur->hur_user_item[i].hui_extent.length = -1;
- rc = lfs_hsm_prepare_file(argv[optind + i],
- &hur->hur_user_item[i].hui_fid,
- &last_dev);
- hur->hur_request.hr_itemcount++;
+ rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
+ &last_dev);
if (rc)
goto out_free;
}
}
while ((rc = getline(&line, &len, fp)) != -1) {
- struct hsm_user_item *hui;
-
/* If allocated buffer was too small, get something
* larger */
if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
ssize_t size;
+
nbfile_alloc = nbfile_alloc * 2 + 1;
oldhur = hur;
hur = llapi_hsm_user_request_alloc(nbfile_alloc,
if (line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0';
- hui =
- &hur->hur_user_item[hur->hur_request.hr_itemcount];
- hui->hui_extent.length = -1;
- rc = lfs_hsm_prepare_file(line, &hui->hui_fid,
- &last_dev);
- hur->hur_request.hr_itemcount++;
+ rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
+ mntpath, line, &last_dev);
if (rc) {
fclose(fp);
goto out_free;
}
- if ((some_file[0] == '\0') &&
- (strlen(line) < sizeof(some_file)))
- strcpy(some_file, line);
+ if (some_file == NULL) {
+ some_file = line;
+ line = NULL;
+ }
}
rc = fclose(fp);
- if (line)
- free(line);
+ free(line);
}
/* If a --data was used, add it to the request */
}
out_free:
+ free(some_file);
free(hur);
return rc;
}
SWAP_LAYOUTS_KEEP_ATIME);
}
+static const char *const ladvise_names[] = LU_LADVISE_NAMES;
+
+static enum lu_ladvise_type lfs_get_ladvice(const char *string)
+{
+ enum lu_ladvise_type advice;
+
+ for (advice = 0;
+ advice < ARRAY_SIZE(ladvise_names); advice++) {
+ if (ladvise_names[advice] == NULL)
+ continue;
+ if (strcmp(string, ladvise_names[advice]) == 0)
+ return advice;
+ }
+
+ return LU_LADVISE_INVALID;
+}
+
+static int lfs_ladvise(int argc, char **argv)
+{
+ struct option long_opts[] = {
+ {"advice", required_argument, 0, 'a'},
+ {"background", no_argument, 0, 'b'},
+ {"end", required_argument, 0, 'e'},
+ {"start", required_argument, 0, 's'},
+ {"length", required_argument, 0, 'l'},
+ {0, 0, 0, 0}
+ };
+ char short_opts[] = "a:be:l:s:";
+ int c;
+ int rc = 0;
+ const char *path;
+ int fd;
+ struct lu_ladvise advice;
+ enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
+ unsigned long long start = 0;
+ unsigned long long end = LUSTRE_EOF;
+ unsigned long long length = 0;
+ unsigned long long size_units;
+ unsigned long long flags = 0;
+
+ optind = 0;
+ while ((c = getopt_long(argc, argv, short_opts,
+ long_opts, NULL)) != -1) {
+ switch (c) {
+ case 'a':
+ advice_type = lfs_get_ladvice(optarg);
+ if (advice_type == LU_LADVISE_INVALID) {
+ fprintf(stderr, "%s: invalid advice type "
+ "'%s'\n", argv[0], optarg);
+ fprintf(stderr, "Valid types:");
+
+ for (advice_type = 0;
+ advice_type < ARRAY_SIZE(ladvise_names);
+ advice_type++) {
+ if (ladvise_names[advice_type] == NULL)
+ continue;
+ fprintf(stderr, " %s",
+ ladvise_names[advice_type]);
+ }
+ fprintf(stderr, "\n");
+
+ return CMD_HELP;
+ }
+ break;
+ case 'b':
+ flags |= LF_ASYNC;
+ break;
+ case 'e':
+ size_units = 1;
+ rc = llapi_parse_size(optarg, &end,
+ &size_units, 0);
+ if (rc) {
+ fprintf(stderr, "%s: bad end offset '%s'\n",
+ argv[0], optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 's':
+ size_units = 1;
+ rc = llapi_parse_size(optarg, &start,
+ &size_units, 0);
+ if (rc) {
+ fprintf(stderr, "%s: bad start offset "
+ "'%s'\n", argv[0], optarg);
+ return CMD_HELP;
+ }
+ break;
+ case 'l':
+ size_units = 1;
+ rc = llapi_parse_size(optarg, &length,
+ &size_units, 0);
+ if (rc) {
+ fprintf(stderr, "%s: bad length '%s'\n",
+ argv[0], optarg);
+ return CMD_HELP;
+ }
+ break;
+ case '?':
+ return CMD_HELP;
+ default:
+ fprintf(stderr, "%s: option '%s' unrecognized\n",
+ argv[0], argv[optind - 1]);
+ return CMD_HELP;
+ }
+ }
+
+ if (advice_type == LU_LADVISE_INVALID) {
+ fprintf(stderr, "%s: please give an advice type\n", argv[0]);
+ fprintf(stderr, "Valid types:");
+ for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
+ advice_type++) {
+ if (ladvise_names[advice_type] == NULL)
+ continue;
+ fprintf(stderr, " %s", ladvise_names[advice_type]);
+ }
+ fprintf(stderr, "\n");
+ return CMD_HELP;
+ }
+
+ if (argc <= optind) {
+ fprintf(stderr, "%s: please give one or more file names\n",
+ argv[0]);
+ return CMD_HELP;
+ }
+
+ if (end != LUSTRE_EOF && length != 0 && end != start + length) {
+ fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
+ argv[0]);
+ return CMD_HELP;
+ }
+
+ if (end == LUSTRE_EOF && length != 0)
+ end = start + length;
+
+ if (end <= start) {
+ fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
+ argv[0], start, end);
+ return CMD_HELP;
+ }
+
+ while (optind < argc) {
+ int rc2;
+
+ path = argv[optind++];
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "%s: cannot open file '%s': %s\n",
+ argv[0], path, strerror(errno));
+ rc2 = -errno;
+ goto next;
+ }
+
+ advice.lla_start = start;
+ advice.lla_end = end;
+ advice.lla_advice = advice_type;
+ advice.lla_padding = 0;
+ rc2 = llapi_ladvise(fd, flags, 1, &advice);
+ close(fd);
+ if (rc2 < 0) {
+ fprintf(stderr, "%s: cannot give advice '%s' to file "
+ "'%s': %s\n", argv[0],
+ ladvise_names[advice_type],
+ path, strerror(errno));
+ }
+next:
+ if (rc == 0 && rc2 < 0)
+ rc = rc2;
+ }
+ return rc;
+}
+
int main(int argc, char **argv)
{
int rc;
Parser_init("lfs > ", cmdlist);
+ progname = argv[0]; /* Used in error messages */
if (argc > 1) {
rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
} else {