X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=blobdiff_plain;f=lustre%2Futils%2Fliblustreapi.c;h=6fbf5d614219c3308156d94ef5cf9bce1edaf7f3;hp=7e8705461a921e2dff08ad4d9aaf7f30175001c3;hb=87a333f85f29fe989997638ee2b7fd5c3a53608b;hpb=70e80ade90af09300396706b8910e196a7928520 diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index 7e870546..6fbf5d6 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -16,8 +16,8 @@ * in the LICENSE file that accompanied this code). * * You should have received a copy of the GNU General Public License - * version 2 along with this program; If not, see [sun.com URL with a - * copy of GPLv2]. + * version 2 along with this program; If not, see + * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or @@ -58,15 +58,15 @@ #include #include #include +#include #include -#ifdef HAVE_ASM_TYPES_H -#include -#endif +#include #ifdef HAVE_LINUX_UNISTD_H #include #else #include #endif +#include #include #include @@ -162,18 +162,25 @@ void llapi_printf(int level, char *fmt, ...) va_end(args); } +/** + * size_units is unchanged if no specifier used + */ int parse_size(char *optarg, unsigned long long *size, - unsigned long long *size_units) + unsigned long long *size_units, int bytes_spec) { char *end; - *size = strtoul(optarg, &end, 0); + *size = strtoull(optarg, &end, 0); if (*end != '\0') { if ((*end == 'b') && *(end+1) == '\0' && - (*size & (~0ULL << (64 - 9))) == 0) { + (*size & (~0ULL << (64 - 9))) == 0 && + !bytes_spec) { *size <<= 9; *size_units = 1 << 9; + } else if ((*end == 'b') && *(end+1) == '\0' && + bytes_spec) { + *size_units = 1; } else if ((*end == 'k' || *end == 'K') && *(end+1) == '\0' && (*size & (~0ULL << (64 - 10))) == 0) { @@ -212,70 +219,213 @@ int parse_size(char *optarg, unsigned long long *size, return 0; } -int llapi_file_open(const char *name, int flags, int mode, - unsigned long stripe_size, int stripe_offset, - int stripe_count, int stripe_pattern) +int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset, + int stripe_count, int stripe_pattern) { - struct lov_user_md lum = { 0 }; - int fd, rc = 0; - int isdir = 0; int page_size; - fd = open(name, flags | O_LOV_DELAY_CREATE, mode); - if (fd < 0 && errno == EISDIR) { - fd = open(name, O_DIRECTORY | O_RDONLY); - isdir++; - } - - if (fd < 0) { - rc = -errno; - llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name); - return rc; - } - /* 64 KB is the largest common page size I'm aware of (on ia64), but * check the local page size just in case. */ page_size = LOV_MIN_STRIPE_SIZE; if (getpagesize() > page_size) { page_size = getpagesize(); - llapi_err_noerrno(LLAPI_MSG_WARN, + llapi_err_noerrno(LLAPI_MSG_WARN, "warning: your page size (%u) is " - "larger than expected (%u)", page_size, + "larger than expected (%u)", page_size, LOV_MIN_STRIPE_SIZE); } if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) { - errno = rc = -EINVAL; llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, " - "must be an even multiple of %d bytes", + "must be an even multiple of %d bytes", stripe_size, page_size); - goto out; + return -EINVAL; } if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) { - errno = rc = -EINVAL; - llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d", + errno = -EINVAL; + llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d", stripe_offset); - goto out; + return -EINVAL; } if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) { - errno = rc = -EINVAL; - llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d", + errno = -EINVAL; + llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d", stripe_count); - goto out; + return -EINVAL; } - if (stripe_count > 0 && (__u64)stripe_size * stripe_count > 0xffffffff){ - errno = rc = -EINVAL; - llapi_err(LLAPI_MSG_ERROR, "error: stripe_size %lu * " - "stripe_count %u exceeds 4GB", stripe_size, - stripe_count); + if (stripe_size >= (1ULL << 32)){ + errno = -EINVAL; + llapi_err(LLAPI_MSG_ERROR, "warning: stripe size larger than 4G" + " is not currently supported and would wrap"); + return -EINVAL; + } + return 0; +} + +static int find_target_obdpath(char *fsname, char *path) +{ + glob_t glob_info; + char pattern[PATH_MAX + 1]; + int rc; + + snprintf(pattern, PATH_MAX, + "/proc/fs/lustre/lov/%s-*/target_obd", + fsname); + rc = glob(pattern, GLOB_BRACE, NULL, &glob_info); + if (rc == GLOB_NOMATCH) + return -ENODEV; + else if (rc) + return -EINVAL; + + strcpy(path, glob_info.gl_pathv[0]); + globfree(&glob_info); + return 0; +} + +static int find_poolpath(char *fsname, char *poolname, char *poolpath) +{ + glob_t glob_info; + char pattern[PATH_MAX + 1]; + int rc; + + snprintf(pattern, PATH_MAX, + "/proc/fs/lustre/lov/%s-*/pools/%s", + fsname, poolname); + rc = glob(pattern, GLOB_BRACE, NULL, &glob_info); + /* If no pools, make sure the lov is available */ + if ((rc == GLOB_NOMATCH) && + (find_target_obdpath(fsname, poolpath) == -ENODEV)) + return -ENODEV; + if (rc) + return -EINVAL; + + strcpy(poolpath, glob_info.gl_pathv[0]); + globfree(&glob_info); + return 0; +} + +/* + * if pool is NULL, search ostname in target_obd + * if pool is not NULL: + * if pool not found returns errno < 0 + * if ostname is NULL, returns 1 if pool is not empty and 0 if pool empty + * if ostname is not NULL, returns 1 if OST is in pool and 0 if not + */ +int llapi_search_ost(char *fsname, char *poolname, char *ostname) +{ + FILE *fd; + char buffer[PATH_MAX + 1]; + int len = 0, rc; + + if (ostname != NULL) + len = strlen(ostname); + + if (poolname == NULL) + rc = find_target_obdpath(fsname, buffer); + else + rc = find_poolpath(fsname, poolname, buffer); + if (rc) + return rc; + + if ((fd = fopen(buffer, "r")) == NULL) + return -EINVAL; + + while (fgets(buffer, sizeof(buffer), fd) != NULL) { + if (poolname == NULL) { + char *ptr; + /* Search for an ostname in the list of OSTs + Line format is IDX: fsname-OSTxxxx_UUID STATUS */ + ptr = strchr(buffer, ' '); + if ((ptr != NULL) && + (strncmp(ptr + 1, ostname, len) == 0)) { + fclose(fd); + return 1; + } + } else { + /* Search for an ostname in a pool, + (or an existing non-empty pool if no ostname) */ + if ((ostname == NULL) || + (strncmp(buffer, ostname, len) == 0)) { + fclose(fd); + return 1; + } + } + } + fclose(fd); + return 0; +} + +int llapi_file_open_pool(const char *name, int flags, int mode, + unsigned long long stripe_size, int stripe_offset, + int stripe_count, int stripe_pattern, char *pool_name) +{ + struct lov_user_md_v3 lum = { 0 }; + int fd, rc = 0; + int isdir = 0; + + /* Make sure we have a good pool */ + if (pool_name != NULL) { + char fsname[MAX_OBD_NAME + 1], *ptr; + + if (llapi_search_fsname(name, fsname)) { + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "'%s' is not on a Lustre filesystem", name); + return -EINVAL; + } + + /* in case user gives the full pool name ., + * strip the fsname */ + ptr = strchr(pool_name, '.'); + if (ptr != NULL) { + *ptr = '\0'; + if (strcmp(pool_name, fsname) != 0) { + *ptr = '.'; + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "Pool '%s' is not on filesystem '%s'", + pool_name, fsname); + return -EINVAL; + } + pool_name = ptr + 1; + } + + /* Make sure the pool exists and is non-empty */ + if ((rc = llapi_search_ost(fsname, pool_name, NULL)) < 1) { + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "pool '%s.%s' %s", fsname, pool_name, + rc == 0 ? "has no OSTs" : "does not exist"); + return -EINVAL; + } + } + + fd = open(name, flags | O_LOV_DELAY_CREATE, mode); + if (fd < 0 && errno == EISDIR) { + fd = open(name, O_DIRECTORY | O_RDONLY); + isdir++; + } + + if (fd < 0) { + rc = -errno; + llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name); + return rc; + } + + if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset, + stripe_count, stripe_pattern)) != 0){ + errno = rc; goto out; } /* Initialize IOCTL striping pattern structure */ - lum.lmm_magic = LOV_USER_MAGIC; + lum.lmm_magic = LOV_USER_MAGIC_V3; lum.lmm_pattern = stripe_pattern; lum.lmm_stripe_size = stripe_size; lum.lmm_stripe_count = stripe_count; lum.lmm_stripe_offset = stripe_offset; + if (pool_name != NULL) { + strncpy(lum.lmm_pool_name, pool_name, LOV_MAXPOOLNAME); + } else { + /* If no pool is specified at all, use V1 request */ + lum.lmm_magic = LOV_USER_MAGIC_V1; + } if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum)) { char *errmsg = "stripe already set"; @@ -285,7 +435,7 @@ int llapi_file_open(const char *name, int flags, int mode, llapi_err_noerrno(LLAPI_MSG_ERROR, "error on ioctl "LPX64" for '%s' (%d): %s", - (__u64)LL_IOC_LOV_SETSTRIPE, name, fd, errmsg); + (__u64)LL_IOC_LOV_SETSTRIPE, name, fd,errmsg); } out: if (rc) { @@ -296,13 +446,23 @@ out: return fd; } -int llapi_file_create(const char *name, unsigned long stripe_size, +int llapi_file_open(const char *name, int flags, int mode, + unsigned long long stripe_size, int stripe_offset, + int stripe_count, int stripe_pattern) +{ + return llapi_file_open_pool(name, flags, mode, stripe_size, + stripe_offset, stripe_count, + stripe_pattern, NULL); +} + +int llapi_file_create(const char *name, unsigned long long stripe_size, int stripe_offset, int stripe_count, int stripe_pattern) { int fd; - fd = llapi_file_open(name, O_CREAT | O_WRONLY, 0644, stripe_size, - stripe_offset, stripe_count, stripe_pattern); + fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size, + stripe_offset, stripe_count, stripe_pattern, + NULL); if (fd < 0) return fd; @@ -310,6 +470,414 @@ int llapi_file_create(const char *name, unsigned long stripe_size, return 0; } +int llapi_file_create_pool(const char *name, unsigned long long stripe_size, + int stripe_offset, int stripe_count, + int stripe_pattern, char *pool_name) +{ + int fd; + + fd = llapi_file_open_pool(name, O_CREAT | O_WRONLY, 0644, stripe_size, + stripe_offset, stripe_count, stripe_pattern, + pool_name); + if (fd < 0) + return fd; + + close(fd); + return 0; +} + +/* + * Find the fsname, the full path, and/or an open fd. + * Either the fsname or path must not be NULL + */ +#define WANT_PATH 0x1 +#define WANT_FSNAME 0x2 +#define WANT_FD 0x4 +#define WANT_INDEX 0x8 +#define WANT_ERROR 0x10 +static int get_root_path(int want, char *fsname, int *outfd, char *path, + int index) +{ + struct mntent mnt; + char buf[PATH_MAX], mntdir[PATH_MAX]; + char *ptr; + FILE *fp; + int idx = 0, len = 0, mntlen, fd; + int rc = -ENODEV; + + /* get the mount point */ + fp = setmntent(MOUNTED, "r"); + if (fp == NULL) { + llapi_err(LLAPI_MSG_ERROR, + "setmntent(%s) failed: %s:", MOUNTED, + strerror (errno)); + return -EIO; + } + while (1) { + if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL) + break; + + if (!llapi_is_lustre_mnt(&mnt)) + continue; + + if ((want & WANT_INDEX) && (idx++ != index)) + continue; + + mntlen = strlen(mnt.mnt_dir); + ptr = strrchr(mnt.mnt_fsname, '/'); + if (!ptr && !len) { + rc = -EINVAL; + break; + } + ptr++; + + /* Check the fsname for a match, if given */ + if (!(want & WANT_FSNAME) && fsname != NULL && + (strlen(fsname) > 0) && (strcmp(ptr, fsname) != 0)) + continue; + + /* If the path isn't set return the first one we find */ + if (path == NULL || strlen(path) == 0) { + strcpy(mntdir, mnt.mnt_dir); + if ((want & WANT_FSNAME) && fsname != NULL) + strcpy(fsname, ptr); + rc = 0; + break; + /* Otherwise find the longest matching path */ + } else if ((strlen(path) >= mntlen) && (mntlen >= len) && + (strncmp(mnt.mnt_dir, path, mntlen) == 0)) { + strcpy(mntdir, mnt.mnt_dir); + len = mntlen; + if ((want & WANT_FSNAME) && fsname != NULL) + strcpy(fsname, ptr); + rc = 0; + } + } + endmntent(fp); + + /* Found it */ + if (rc == 0) { + if ((want & WANT_PATH) && path != NULL) + strcpy(path, mntdir); + if (want & WANT_FD) { + fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK); + if (fd < 0) { + perror("open"); + rc = -errno; + } else { + *outfd = fd; + } + } + } else if (want & WANT_ERROR) + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "can't find fs root for '%s': %d", + (want & WANT_PATH) ? fsname : path, rc); + return rc; +} + +/* + * search lustre mounts + * + * Calling this function will return to the user the mount point, mntdir, and + * the file system name, fsname, if the user passed a buffer to this routine. + * + * The user inputs are pathname and index. If the pathname is supplied then + * the value of the index will be ignored. The pathname will return data if + * the pathname is located on a lustre mount. Index is used to pick which + * mount point you want in the case of multiple mounted lustre file systems. + * See function lfs_osts in lfs.c for a example of the index use. + */ +int llapi_search_mounts(const char *pathname, int index, char *mntdir, + char *fsname) +{ + int want = WANT_PATH, idx = -1; + + if (!pathname) { + want |= WANT_INDEX; + idx = index; + } else + strcpy(mntdir, pathname); + + if (fsname) + want |= WANT_FSNAME; + return get_root_path(want, fsname, NULL, mntdir, idx); +} + +/* Given a path, find the corresponding Lustre fsname */ +int llapi_search_fsname(const char *pathname, char *fsname) +{ + char *path = (char*)pathname, buf[PATH_MAX + 1]; + + if (pathname[0] != '/') { /* Need a absolute path */ + memset(buf, '\0', sizeof(buf)); + if (realpath(pathname, buf) == NULL) { + llapi_err(LLAPI_MSG_ERROR, "pathname '%s' cannot expand", + pathname); + return -EINVAL; + } + path = buf; + } + return get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, + path, -1); +} + +/* return the first file matching this pattern */ +static int first_match(char *pattern, char *buffer) +{ + glob_t glob_info; + + if (glob(pattern, GLOB_BRACE, NULL, &glob_info)) + return -ENOENT; + + if (glob_info.gl_pathc < 1) { + globfree(&glob_info); + return -ENOENT; + } + + strcpy(buffer, glob_info.gl_pathv[0]); + + globfree(&glob_info); + return 0; +} + +/* + * find the pool directory path under /proc + * (can be also used to test if a fsname is known) + */ +static int poolpath(char *fsname, char *pathname, char *pool_pathname) +{ + int rc = 0; + char pattern[PATH_MAX + 1]; + char buffer[PATH_MAX]; + + if (fsname == NULL) { + rc = llapi_search_fsname(pathname, buffer); + if (rc != 0) + return rc; + fsname = buffer; + strcpy(pathname, fsname); + } + + snprintf(pattern, PATH_MAX, "/proc/fs/lustre/lov/%s-*/pools", fsname); + rc = first_match(pattern, buffer); + if (rc) + return rc; + + /* in fsname test mode, pool_pathname is NULL */ + if (pool_pathname != NULL) + strcpy(pool_pathname, buffer); + + return 0; +} + +/** + * Get the list of pool members. + * \param poolname string of format \.\ + * \param members caller-allocated array of char* + * \param list_size size of the members array + * \param buffer caller-allocated buffer for storing OST names + * \param buffer_size size of the buffer + * + * \return number of members retrieved for this pool + * \retval -error failure + */ +int llapi_get_poolmembers(const char *poolname, char **members, + int list_size, char *buffer, int buffer_size) +{ + char fsname[PATH_MAX + 1]; + char *pool, *tmp; + char pathname[PATH_MAX + 1]; + char path[PATH_MAX + 1]; + char buf[1024]; + FILE *fd; + int rc = 0; + int nb_entries = 0; + int used = 0; + + /* name is FSNAME.POOLNAME */ + if (strlen(poolname) > PATH_MAX) + return -EOVERFLOW; + strcpy(fsname, poolname); + pool = strchr(fsname, '.'); + if (pool == NULL) + return -EINVAL; + + *pool = '\0'; + pool++; + + rc = poolpath(fsname, NULL, pathname); + if (rc != 0) { + errno = -rc; + llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found", + fsname); + return rc; + } + + llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool); + sprintf(path, "%s/%s", pathname, pool); + if ((fd = fopen(path, "r")) == NULL) { + llapi_err(LLAPI_MSG_ERROR, "Cannot open %s", path); + return -EINVAL; + } + + rc = 0; + while (fgets(buf, sizeof(buf), fd) != NULL) { + if (nb_entries >= list_size) { + rc = -EOVERFLOW; + break; + } + /* remove '\n' */ + if ((tmp = strchr(buf, '\n')) != NULL) + *tmp='\0'; + if (used + strlen(buf) + 1 > buffer_size) { + rc = -EOVERFLOW; + break; + } + + strcpy(buffer + used, buf); + members[nb_entries] = buffer + used; + used += strlen(buf) + 1; + nb_entries++; + rc = nb_entries; + } + + fclose(fd); + return rc; +} + +/** + * Get the list of pools in a filesystem. + * \param name filesystem name or path + * \param poollist caller-allocated array of char* + * \param list_size size of the poollist array + * \param buffer caller-allocated buffer for storing pool names + * \param buffer_size size of the buffer + * + * \return number of pools retrieved for this filesystem + * \retval -error failure + */ +int llapi_get_poollist(const char *name, char **poollist, int list_size, + char *buffer, int buffer_size) +{ + char fsname[PATH_MAX + 1], rname[PATH_MAX + 1], pathname[PATH_MAX + 1]; + char *ptr; + DIR *dir; + struct dirent pool; + struct dirent *cookie = NULL; + int rc = 0; + unsigned int nb_entries = 0; + unsigned int used = 0; + unsigned int i; + + /* initilize output array */ + for (i = 0; i < list_size; i++) + poollist[i] = NULL; + + /* is name a pathname ? */ + ptr = strchr(name, '/'); + if (ptr != NULL) { + /* only absolute pathname is supported */ + if (*name != '/') + return -EINVAL; + if (!realpath(name, rname)) { + rc = -errno; + llapi_err(LLAPI_MSG_ERROR, "invalid path '%s'", name); + return rc; + } + + rc = poolpath(NULL, rname, pathname); + if (rc != 0) { + errno = -rc; + llapi_err(LLAPI_MSG_ERROR, "'%s' is not" + " a Lustre filesystem", name); + return rc; + } + strcpy(fsname, rname); + } else { + /* name is FSNAME */ + strcpy(fsname, name); + rc = poolpath(fsname, NULL, pathname); + } + if (rc != 0) { + errno = -rc; + llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found", + name); + return rc; + } + + llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname); + if ((dir = opendir(pathname)) == NULL) { + llapi_err(LLAPI_MSG_ERROR, "Could not open pool list for '%s'", + name); + return -errno; + } + + while(1) { + rc = readdir_r(dir, &pool, &cookie); + + if (rc != 0) { + llapi_err(LLAPI_MSG_ERROR, + "Error reading pool list for '%s'", name); + return -errno; + } else if ((rc == 0) && (cookie == NULL)) + /* end of directory */ + break; + + /* ignore . and .. */ + if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, "..")) + continue; + + /* check output bounds */ + if (nb_entries >= list_size) + return -EOVERFLOW; + + /* +2 for '.' and final '\0' */ + if (used + strlen(pool.d_name) + strlen(fsname) + 2 + > buffer_size) + return -EOVERFLOW; + + sprintf(buffer + used, "%s.%s", fsname, pool.d_name); + poollist[nb_entries] = buffer + used; + used += strlen(pool.d_name) + strlen(fsname) + 2; + nb_entries++; + } + + closedir(dir); + return nb_entries; +} + +/* wrapper for lfs.c and obd.c */ +int llapi_poollist(const char *name) +{ + /* list of pool names (assume that pool count is smaller + than OST count) */ + char *list[FIND_MAX_OSTS]; + char *buffer; + /* fsname-OST0000_UUID < 32 char, 1 per OST */ + int bufsize = FIND_MAX_OSTS * 32; + int i, nb; + + buffer = malloc(bufsize); + if (buffer == NULL) + return -ENOMEM; + + if ((name[0] == '/') || (strchr(name, '.') == NULL)) + /* name is a path or fsname */ + nb = llapi_get_poollist(name, list, FIND_MAX_OSTS, buffer, + bufsize); + else + /* name is a pool name (.) */ + nb = llapi_get_poolmembers(name, list, FIND_MAX_OSTS, buffer, + bufsize); + + for (i = 0; i < nb; i++) + llapi_printf(LLAPI_MSG_NORMAL, "%s\n", list[i]); + + free(buffer); + return (nb < 0 ? nb : 0); +} + + typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d, void *data, cfs_dirent_t *de); @@ -318,9 +886,9 @@ typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d, static int common_param_init(struct find_param *param) { - param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT); + param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3); if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) { - llapi_err(LLAPI_MSG_ERROR, + llapi_err(LLAPI_MSG_ERROR, "error: allocation of %d bytes for ioctl", sizeof(lstat_t) + param->lumlen); return -ENOMEM; @@ -341,7 +909,206 @@ static void find_param_fini(struct find_param *param) free(param->lmd); } -int llapi_file_get_lov_fuuid(int fd, struct obd_uuid *lov_name) +static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data, + cfs_dirent_t *de) +{ + struct find_param *param = (struct find_param *)data; + param->depth--; + return 0; +} + +static DIR *opendir_parent(char *path) +{ + DIR *parent; + char *fname; + char c; + + fname = strrchr(path, '/'); + if (fname == NULL) + return opendir("."); + + c = fname[1]; + fname[1] = '\0'; + parent = opendir(path); + fname[1] = c; + return parent; +} + +int llapi_mds_getfileinfo(char *path, DIR *parent, + struct lov_user_mds_data *lmd) +{ + lstat_t *st = &lmd->lmd_st; + char *fname = strrchr(path, '/'); + int ret = 0; + + if (parent == NULL) + return -EINVAL; + + fname = (fname == NULL ? path : fname + 1); + /* retrieve needed file info */ + strncpy((char *)lmd, fname, + lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC)); + ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd); + + if (ret) { + if (errno == ENOTTY) { + /* ioctl is not supported, it is not a lustre fs. + * Do the regular lstat(2) instead. */ + ret = lstat_f(path, st); + if (ret) { + llapi_err(LLAPI_MSG_ERROR, + "error: %s: lstat failed for %s", + __func__, path); + return ret; + } + } else if (errno == ENOENT) { + llapi_err(LLAPI_MSG_WARN, + "warning: %s: %s does not exist", + __func__, path); + return -ENOENT; + } else { + llapi_err(LLAPI_MSG_ERROR, + "error: %s: IOC_MDC_GETFILEINFO failed for %s", + __func__, path); + return ret; + } + } + + return 0; +} + +static int llapi_semantic_traverse(char *path, int size, DIR *parent, + semantic_func_t sem_init, + semantic_func_t sem_fini, void *data, + cfs_dirent_t *de) +{ + cfs_dirent_t *dent; + int len, ret; + DIR *d, *p = NULL; + + ret = 0; + len = strlen(path); + + d = opendir(path); + if (!d && errno != ENOTDIR) { + llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'", + __func__, path); + return -EINVAL; + } else if (!d && !parent) { + /* ENOTDIR. Open the parent dir. */ + p = opendir_parent(path); + if (!p) + GOTO(out, ret = -EINVAL); + } + + if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de))) + goto err; + + if (!d) + GOTO(out, ret = 0); + + while ((dent = readdir64(d)) != NULL) { + ((struct find_param *)data)->have_fileinfo = 0; + + if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) + continue; + + /* Don't traverse .lustre directory */ + if (!(strcmp(dent->d_name, dot_lustre_name))) + continue; + + path[len] = 0; + if ((len + dent->d_reclen + 2) > size) { + llapi_err(LLAPI_MSG_ERROR, + "error: %s: string buffer is too small", + __func__); + break; + } + strcat(path, "/"); + strcat(path, dent->d_name); + + if (dent->d_type == DT_UNKNOWN) { + lstat_t *st = &((struct find_param *)data)->lmd->lmd_st; + + ret = llapi_mds_getfileinfo(path, d, + ((struct find_param *)data)->lmd); + if (ret == 0) { + ((struct find_param *)data)->have_fileinfo = 1; + dent->d_type = + llapi_filetype_dir_table[st->st_mode & + S_IFMT]; + } + if (ret == -ENOENT) + continue; + } + + switch (dent->d_type) { + case DT_UNKNOWN: + llapi_err(LLAPI_MSG_ERROR, + "error: %s: '%s' is UNKNOWN type %d", + __func__, dent->d_name, dent->d_type); + break; + case DT_DIR: + ret = llapi_semantic_traverse(path, size, d, sem_init, + sem_fini, data, dent); + if (ret < 0) + goto out; + break; + default: + ret = 0; + if (sem_init) { + ret = sem_init(path, d, NULL, data, dent); + if (ret < 0) + goto out; + } + if (sem_fini && ret == 0) + sem_fini(path, d, NULL, data, dent); + } + } + +out: + path[len] = 0; + + if (sem_fini) + sem_fini(path, parent, d, data, de); +err: + if (d) + closedir(d); + if (p) + closedir(p); + return ret; +} + +static int param_callback(char *path, semantic_func_t sem_init, + semantic_func_t sem_fini, struct find_param *param) +{ + int ret, len = strlen(path); + char *buf; + + if (len > PATH_MAX) { + llapi_err(LLAPI_MSG_ERROR, "Path name '%s' is too long", path); + return -EINVAL; + } + + buf = (char *)malloc(PATH_MAX + 1); + if (!buf) + return -ENOMEM; + + ret = common_param_init(param); + if (ret) + goto out; + param->depth = 0; + + strncpy(buf, path, PATH_MAX + 1); + ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, sem_init, + sem_fini, param, NULL); +out: + find_param_fini(param); + free(buf); + return ret < 0 ? ret : 0; +} + +int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name) { int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name); if (rc) { @@ -358,11 +1125,11 @@ int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid) fd = open(path, O_RDONLY); if (fd < 0) { rc = errno; - llapi_err(LLAPI_MSG_ERROR, "error opening %s\n", path); + llapi_err(LLAPI_MSG_ERROR, "error opening %s", path); return rc; } - rc = llapi_file_get_lov_fuuid(fd, lov_uuid); + rc = llapi_file_fget_lov_uuid(fd, lov_uuid); close(fd); @@ -383,7 +1150,7 @@ int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count) int rc = 0, index = 0; /* Get the lov name */ - rc = llapi_file_get_lov_fuuid(fd, &lov_name); + rc = llapi_file_fget_lov_uuid(fd, &lov_name); if (rc) return rc; @@ -405,15 +1172,56 @@ int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count) index++; } - fclose(fp); - - if (uuidp && (index >= *ost_count)) - return -EOVERFLOW; + fclose(fp); + + if (uuidp && (index >= *ost_count)) + return -EOVERFLOW; + + *ost_count = index; + return rc; +} + +int llapi_get_obd_count(char *mnt, int *count, int is_mdt) +{ + DIR *root; + int rc; + + root = opendir(mnt); + if (!root) { + llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt); + return -1; + } + + *count = is_mdt; + rc = ioctl(dirfd(root), LL_IOC_GETOBDCOUNT, count); - *ost_count = index; + closedir(root); return rc; } +/* Check if user specified value matches a real uuid. Ignore _UUID, + * -osc-4ba41334, other trailing gunk in comparison. + * @param real_uuid ends in "_UUID" + * @param search_uuid may or may not end in "_UUID" + */ +int llapi_uuid_match(char *real_uuid, char *search_uuid) +{ + int cmplen = strlen(real_uuid); + int searchlen = strlen(search_uuid); + + if (cmplen > 5 && strcmp(real_uuid + cmplen - 5, "_UUID") == 0) + cmplen -= 5; + if (searchlen > 5 && strcmp(search_uuid + searchlen - 5, "_UUID") == 0) + searchlen -= 5; + + /* The UUIDs may legitimately be different lengths, if + * the system was upgraded from an older version. */ + if (cmplen != searchlen) + return 0; + + return (strncmp(search_uuid, real_uuid, cmplen) == 0); +} + /* Here, param->obduuid points to a single obduuid, the index of which is * returned in param->obdindex */ static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param) @@ -424,12 +1232,15 @@ static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param) FILE *fp; int rc = 0, index; + if (param->got_uuids) + return rc; + /* Get the lov name */ - rc = llapi_file_get_lov_fuuid(dirfd(dir), &lov_uuid); + rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid); if (rc) { if (errno != ENOTTY) { rc = errno; - llapi_err(LLAPI_MSG_ERROR, + llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name: %s", dname); } else { rc = 0; @@ -457,8 +1268,7 @@ static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param) break; if (param->obduuid) { - if (strncmp(param->obduuid->uuid, uuid, - sizeof(uuid)) == 0) { + if (llapi_uuid_match(uuid, param->obduuid->uuid)) { param->obdindex = index; break; } @@ -471,12 +1281,11 @@ static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param) fclose(fp); - if (!param->quiet && param->obduuid && - (param->obdindex == OBD_NOT_FOUND)) { - llapi_err_noerrno(LLAPI_MSG_ERROR, + if (param->obduuid && (param->obdindex == OBD_NOT_FOUND)) { + llapi_err_noerrno(LLAPI_MSG_ERROR, "error: %s: unknown obduuid: %s", - __FUNCTION__, param->obduuid->uuid); - //rc = EINVAL; + __func__, param->obduuid->uuid); + rc = -EINVAL; } return (rc); @@ -520,16 +1329,22 @@ retry_get_uuids: return -ENOMEM; for (obdnum = 0; obdnum < param->num_obds; obdnum++) { - for (i = 0; i <= obdcount; i++) { - if (strcmp((char *)¶m->obduuid[obdnum].uuid, - (char *)&uuids[i]) == 0) { + for (i = 0; i < obdcount; i++) { + if (llapi_uuid_match(uuids[i].uuid, + param->obduuid[obdnum].uuid)) { param->obdindexes[obdnum] = i; obd_valid++; break; } } - if (i == obdcount) + if (i >= obdcount) { param->obdindexes[obdnum] = OBD_NOT_FOUND; + llapi_err_noerrno(LLAPI_MSG_ERROR, + "error: %s: unknown obduuid: %s", + __func__, + param->obduuid[obdnum].uuid); + ret = -EINVAL; + } } if (obd_valid == 0) @@ -539,146 +1354,122 @@ retry_get_uuids: param->got_uuids = 1; - return 0; + return ret; } -void lov_dump_user_lmm_v1(struct lov_user_md_v1 *lum, char *path, int is_dir, - int obdindex, int quiet, int header, int body) +static int cb_ostlist(char *path, DIR *parent, DIR *d, void *data, + struct dirent64 *de) { - int i, obdstripe = 0; + struct find_param *param = (struct find_param *)data; - if (obdindex != OBD_NOT_FOUND) { - for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) { - if (obdindex == lum->lmm_objects[i].l_ost_idx) { - llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path); - obdstripe = 1; - break; - } - } - } else if (!quiet) { + LASSERT(parent != NULL || d != NULL); + + /* Prepare odb. */ + return setup_obd_uuid(d ? d : parent, path, param); +} + +int llapi_ostlist(char *path, struct find_param *param) +{ + return param_callback(path, cb_ostlist, cb_common_fini, param); +} + +static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path, + int is_dir, int verbose, int depth, + char *pool_name) +{ + char *prefix = is_dir ? "" : "lmm_"; + char nl = is_dir ? ' ' : '\n'; + + if (is_dir && lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) { + lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR; + if (verbose & VERBOSE_DETAIL) + llapi_printf(LLAPI_MSG_NORMAL, "(Default) "); + } + + if (depth && path && ((verbose != VERBOSE_OBJID) || !is_dir)) llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path); - obdstripe = 1; + + if ((verbose & VERBOSE_DETAIL) && !is_dir) { + llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n", + lum->lmm_magic); + llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n", + lum->lmm_object_gr); + llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n", + lum->lmm_object_id); } - /* if it's a directory */ - if (is_dir) { - if (obdstripe == 1) { - if (lum->lmm_object_gr == LOV_OBJECT_GROUP_DEFAULT) { - llapi_printf(LLAPI_MSG_NORMAL, "(Default) "); - lum->lmm_object_gr = LOV_OBJECT_GROUP_CLEAR; - } - llapi_printf(LLAPI_MSG_NORMAL, - "stripe_count: %d stripe_size: %u " - "stripe_offset: %d\n", - lum->lmm_stripe_count == (__u16)-1 ? -1 : - lum->lmm_stripe_count, - lum->lmm_stripe_size, - lum->lmm_stripe_offset == (__u16)-1 ? -1 : - lum->lmm_stripe_offset); - } - return; + if (verbose & VERBOSE_COUNT) { + if (verbose & ~VERBOSE_COUNT) + llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_count: ", + prefix); + llapi_printf(LLAPI_MSG_NORMAL, "%hd%c", + (__s16)lum->lmm_stripe_count, nl); } - if (header && (obdstripe == 1)) { - llapi_printf(LLAPI_MSG_NORMAL, - "lmm_magic: 0x%08X\n", lum->lmm_magic); - llapi_printf(LLAPI_MSG_NORMAL, - "lmm_object_gr: "LPX64"\n", lum->lmm_object_gr); - llapi_printf(LLAPI_MSG_NORMAL, - "lmm_object_id: "LPX64"\n", lum->lmm_object_id); - llapi_printf(LLAPI_MSG_NORMAL, - "lmm_stripe_count: %u\n", (int)lum->lmm_stripe_count); - llapi_printf(LLAPI_MSG_NORMAL, - "lmm_stripe_size: %u\n", lum->lmm_stripe_size); - llapi_printf(LLAPI_MSG_NORMAL, - "lmm_stripe_pattern: %x\n", lum->lmm_pattern); - } - - if (body) { - if ((!quiet) && (obdstripe == 1)) - llapi_printf(LLAPI_MSG_NORMAL, - "\tobdidx\t\t objid\t\tobjid\t\t group\n"); + if (verbose & VERBOSE_SIZE) { + if (verbose & ~VERBOSE_SIZE) + llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_size: ", + prefix); + llapi_printf(LLAPI_MSG_NORMAL, "%u%c", lum->lmm_stripe_size, + nl); + } - for (i = 0; i < lum->lmm_stripe_count; i++) { - int idx = lum->lmm_objects[i].l_ost_idx; - long long oid = lum->lmm_objects[i].l_object_id; - long long gr = lum->lmm_objects[i].l_object_gr; - if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) - llapi_printf(LLAPI_MSG_NORMAL, - "\t%6u\t%14llu\t%#13llx\t%14llu%s\n", - idx, oid, oid, gr, - obdindex == idx ? " *" : ""); - } - llapi_printf(LLAPI_MSG_NORMAL, "\n"); + if ((verbose & VERBOSE_DETAIL) && !is_dir) { + llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x%c", + lum->lmm_pattern, nl); + } + + if (verbose & VERBOSE_OFFSET) { + if (verbose & ~VERBOSE_OFFSET) + llapi_printf(LLAPI_MSG_NORMAL, "%sstripe_offset: ", + prefix); + llapi_printf(LLAPI_MSG_NORMAL, "%u%c", + lum->lmm_objects[0].l_ost_idx, nl); + } + + if ((verbose & VERBOSE_POOL) && (pool_name != NULL)) { + llapi_printf(LLAPI_MSG_NORMAL, "pool: %s", pool_name); + is_dir = 1; } + + if (is_dir && (verbose != VERBOSE_OBJID)) + llapi_printf(LLAPI_MSG_NORMAL, "\n"); } -void lov_dump_user_lmm_join(struct lov_user_md_v1 *lum, char *path, - int is_dir, int obdindex, int quiet, - int header, int body) +void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name, + struct lov_user_ost_data_v1 *objects, + char *path, int is_dir, + int obdindex, int depth, int header) { - struct lov_user_md_join *lumj = (struct lov_user_md_join *)lum; - int i, obdstripe = 0; + int i, obdstripe = (obdindex != OBD_NOT_FOUND) ? 0 : 1; - if (obdindex != OBD_NOT_FOUND) { - for (i = 0; i < lumj->lmm_stripe_count; i++) { - if (obdindex == lumj->lmm_objects[i].l_ost_idx) { - llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path); + if (!obdstripe) { + for (i = 0; !is_dir && i < lum->lmm_stripe_count; i++) { + if (obdindex == objects[i].l_ost_idx) { obdstripe = 1; break; } } - } else if (!quiet) { - llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path); - obdstripe = 1; - } - - if (header && obdstripe == 1) { - llapi_printf(LLAPI_MSG_NORMAL, "lmm_magic: 0x%08X\n", - lumj->lmm_magic); - llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_gr: "LPX64"\n", - lumj->lmm_object_gr); - llapi_printf(LLAPI_MSG_NORMAL, "lmm_object_id: "LPX64"\n", - lumj->lmm_object_id); - llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_count: %u\n", - (int)lumj->lmm_stripe_count); - llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_size: %u\n", - lumj->lmm_stripe_size); - llapi_printf(LLAPI_MSG_NORMAL, "lmm_stripe_pattern: %x\n", - lumj->lmm_pattern); - llapi_printf(LLAPI_MSG_NORMAL, "lmm_extent_count: %x\n", - lumj->lmm_extent_count); - } - - if (body) { - unsigned long long start = -1, end = 0; - if (!quiet && obdstripe == 1) - llapi_printf(LLAPI_MSG_NORMAL, - "joined\tobdidx\t\t objid\t\tobjid\t\t group" - "\t\tstart\t\tend\n"); - for (i = 0; i < lumj->lmm_stripe_count; i++) { - int idx = lumj->lmm_objects[i].l_ost_idx; - long long oid = lumj->lmm_objects[i].l_object_id; - long long gr = lumj->lmm_objects[i].l_object_gr; - if (obdindex == OBD_NOT_FOUND || obdindex == idx) - llapi_printf(LLAPI_MSG_NORMAL, - "\t%6u\t%14llu\t%#13llx\t%14llu%s", - idx, oid, oid, gr, - obdindex == idx ? " *" : ""); - if (start != lumj->lmm_objects[i].l_extent_start || - end != lumj->lmm_objects[i].l_extent_end) { - start = lumj->lmm_objects[i].l_extent_start; - llapi_printf(LLAPI_MSG_NORMAL, "\t%14llu", start); - end = lumj->lmm_objects[i].l_extent_end; - if (end == (unsigned long long)-1) - llapi_printf(LLAPI_MSG_NORMAL, - "\t\tEOF\n"); - else - llapi_printf(LLAPI_MSG_NORMAL, - "\t\t%llu\n", end); - } else { - llapi_printf(LLAPI_MSG_NORMAL, "\t\t\t\t\n"); - } + } + + if (obdstripe == 1) + lov_dump_user_lmm_header(lum, path, is_dir, header, depth, + pool_name); + + if (!is_dir && (header & VERBOSE_OBJID)) { + if (obdstripe == 1) + llapi_printf(LLAPI_MSG_NORMAL, + "\tobdidx\t\t objid\t\tobjid\t\t group\n"); + + for (i = 0; i < lum->lmm_stripe_count; i++) { + int idx = objects[i].l_ost_idx; + long long oid = objects[i].l_object_id; + long long gr = objects[i].l_object_gr; + if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx)) + llapi_printf(LLAPI_MSG_NORMAL, + "\t%6u\t%14llu\t%#13llx\t%14llu%s\n", + idx, oid, oid, gr, + obdindex == idx ? " *" : ""); } llapi_printf(LLAPI_MSG_NORMAL, "\n"); } @@ -689,21 +1480,31 @@ void llapi_lov_dump_user_lmm(struct find_param *param, { switch(*(__u32 *)¶m->lmd->lmd_lmm) { /* lum->lmm_magic */ case LOV_USER_MAGIC_V1: - lov_dump_user_lmm_v1(¶m->lmd->lmd_lmm, path, is_dir, - param->obdindex, param->quiet, - param->verbose, - (param->verbose || !param->obduuid)); + lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, NULL, + param->lmd->lmd_lmm.lmm_objects, + path, is_dir, + param->obdindex, param->maxdepth, + param->verbose); break; - case LOV_USER_MAGIC_JOIN: - lov_dump_user_lmm_join(¶m->lmd->lmd_lmm, path, is_dir, - param->obdindex, param->quiet, - param->verbose, - (param->verbose || !param->obduuid)); + case LOV_USER_MAGIC_V3: { + char pool_name[LOV_MAXPOOLNAME + 1]; + struct lov_user_ost_data_v1 *objects; + struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm; + + strncpy(pool_name, lmmv3->lmm_pool_name, LOV_MAXPOOLNAME); + pool_name[LOV_MAXPOOLNAME] = '\0'; + objects = lmmv3->lmm_objects; + lov_dump_user_lmm_v1v3(¶m->lmd->lmd_lmm, pool_name, + objects, path, is_dir, + param->obdindex, param->maxdepth, + param->verbose); break; + } default: - llapi_printf(LLAPI_MSG_NORMAL, - "unknown lmm_magic: %#x (expecting %#x)\n", - *(__u32 *)¶m->lmd->lmd_lmm, LOV_USER_MAGIC_V1); + llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic: %#x " + "(expecting one of %#x %#x %#x)\n", + *(__u32 *)¶m->lmd->lmd_lmm, + LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3); return; } } @@ -776,167 +1577,11 @@ int llapi_file_lookup(int dirfd, const char *name) return ioctl(dirfd, IOC_MDC_LOOKUP, buf); } -int llapi_mds_getfileinfo(char *path, DIR *parent, - struct lov_user_mds_data *lmd) -{ - lstat_t *st = &lmd->lmd_st; - char *fname = strrchr(path, '/'); - int ret = 0; - - if (parent == NULL) - return -EINVAL; - - fname = (fname == NULL ? path : fname + 1); - /* retrieve needed file info */ - strncpy((char *)lmd, fname, lov_mds_md_size(MAX_LOV_UUID_COUNT)); - ret = ioctl(dirfd(parent), IOC_MDC_GETFILEINFO, (void *)lmd); - - if (ret) { - if (errno == ENOTTY) { - /* ioctl is not supported, it is not a lustre fs. - * Do the regular lstat(2) instead. */ - ret = lstat_f(path, st); - if (ret) { - llapi_err(LLAPI_MSG_ERROR, - "error: %s: lstat failed for %s", - __FUNCTION__, path); - return ret; - } - } else if (errno == ENOENT) { - llapi_err(LLAPI_MSG_WARN, - "warning: %s: %s does not exist", - __FUNCTION__, path); - return -ENOENT; - } else { - llapi_err(LLAPI_MSG_ERROR, - "error: %s: IOC_MDC_GETFILEINFO failed for %s", - __FUNCTION__, path); - return ret; - } - } - - return 0; -} - -static DIR *opendir_parent(char *path) -{ - DIR *parent; - char *fname; - char c; - - fname = strrchr(path, '/'); - if (fname == NULL) - return opendir("."); - - c = fname[1]; - fname[1] = '\0'; - parent = opendir(path); - fname[1] = c; - return parent; -} - -static int llapi_semantic_traverse(char *path, int size, DIR *parent, - semantic_func_t sem_init, - semantic_func_t sem_fini, void *data, - cfs_dirent_t *de) -{ - cfs_dirent_t *dent; - int len, ret; - DIR *d, *p = NULL; - - ret = 0; - len = strlen(path); - - d = opendir(path); - if (!d && errno != ENOTDIR) { - llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'", - __FUNCTION__, path); - return -EINVAL; - } else if (!d && !parent) { - /* ENOTDIR. Open the parent dir. */ - p = opendir_parent(path); - if (!p) - GOTO(out, ret = -EINVAL); - } - - if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de))) - goto err; - - if (!d) - GOTO(out, ret = 0); - - while ((dent = readdir64(d)) != NULL) { - ((struct find_param *)data)->have_fileinfo = 0; - - if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) - continue; - - path[len] = 0; - if ((len + dent->d_reclen + 2) > size) { - llapi_err(LLAPI_MSG_ERROR, - "error: %s: string buffer is too small", - __FUNCTION__); - break; - } - strcat(path, "/"); - strcat(path, dent->d_name); - - if (dent->d_type == DT_UNKNOWN) { - lstat_t *st = &((struct find_param *)data)->lmd->lmd_st; - - ret = llapi_mds_getfileinfo(path, d, - ((struct find_param *)data)->lmd); - if (ret == 0) { - ((struct find_param *)data)->have_fileinfo = 1; - dent->d_type = llapi_filetype_dir_table[st->st_mode & - S_IFMT]; - } - if (ret == -ENOENT) - continue; - } - - switch (dent->d_type) { - case DT_UNKNOWN: - llapi_err(LLAPI_MSG_ERROR, - "error: %s: '%s' is UNKNOWN type %d", - __FUNCTION__, dent->d_name, dent->d_type); - break; - case DT_DIR: - ret = llapi_semantic_traverse(path, size, d, sem_init, - sem_fini, data, dent); - if (ret < 0) - goto out; - break; - default: - ret = 0; - if (sem_init) { - ret = sem_init(path, d, NULL, data, dent); - if (ret < 0) - goto out; - } - if (sem_fini && ret == 0) - sem_fini(path, d, NULL, data, dent); - } - } - -out: - path[len] = 0; - - if (sem_fini) - sem_fini(path, parent, d, data, de); -err: - if (d) - closedir(d); - if (p) - closedir(p); - return ret; -} - /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N). * @mds indicates if this is MDS timestamps and there are attributes on OSTs. * * The result is -1 if it does not match, 0 if not yet clear, 1 if matches. - * The table bolow gives the answers for the specified parameters (value and + * The table below gives the answers for the specified parameters (value and * sign), 1st column is the answer for the MDS value, the 2nd is for the OST: * -------------------------------------- * 1 | file > limit; sign > 0 | -1 / -1 | @@ -1092,19 +1737,19 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir, lustre_fs = 0; ret = lstat_f(path, st); if (ret) { - llapi_err(LLAPI_MSG_ERROR, + llapi_err(LLAPI_MSG_ERROR, "error: %s: lstat failed for %s", - __FUNCTION__, path); + __func__, path); return ret; } } else if (errno == ENOENT) { - llapi_err(LLAPI_MSG_WARN, + llapi_err(LLAPI_MSG_WARN, "warning: %s: %s does not exist", - __FUNCTION__, path); + __func__, path); goto decided; } else { - llapi_err(LLAPI_MSG_ERROR, "error: %s: %s failed for %s", - __FUNCTION__, dir ? "LL_IOC_MDC_GETINFO" : + llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s", + __func__, dir ? "LL_IOC_MDC_GETINFO" : "IOC_MDC_GETFILEINFO", path); return ret; } @@ -1158,17 +1803,35 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir, goto decided; } else { int i, j; + struct lov_user_ost_data_v1 *lmm_objects; + + if (param->lmd->lmd_lmm.lmm_magic == + LOV_USER_MAGIC_V3) { + struct lov_user_md_v3 *lmmv3 = + (void *)¶m->lmd->lmd_lmm; + + lmm_objects = lmmv3->lmm_objects; + } else { + lmm_objects = param->lmd->lmd_lmm.lmm_objects; + } + for (i = 0; i < param->lmd->lmd_lmm.lmm_stripe_count; i++) { for (j = 0; j < param->num_obds; j++) { if (param->obdindexes[j] == - param->lmd->lmd_lmm.lmm_objects[i].l_ost_idx) + lmm_objects[i].l_ost_idx) { + if (param->exclude_obd) + goto decided; goto obd_matches; + } } } - if (i == param->lmd->lmd_lmm.lmm_stripe_count) + if (i == param->lmd->lmd_lmm.lmm_stripe_count) { + if (param->exclude_obd) + goto obd_matches; goto decided; + } } } @@ -1192,6 +1855,25 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir, } } + if (param->check_pool) { + struct lov_user_md_v3 *lmmv3 = (void *)¶m->lmd->lmd_lmm; + + /* empty requested pool is taken as no pool search => V1 */ + if (((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V1) && + (param->poolname[0] == '\0')) || + ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) && + (strncmp(lmmv3->lmm_pool_name, + param->poolname, LOV_MAXPOOLNAME) == 0)) || + ((param->lmd->lmd_lmm.lmm_magic == LOV_USER_MAGIC_V3) && + (strcmp(param->poolname, "*") == 0))) { + if (param->exclude_pool) + goto decided; + } else { + if (!param->exclude_pool) + goto decided; + } + } + /* Check the time on mds. */ if (!decision) { int for_mds; @@ -1203,15 +1885,16 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir, } obd_matches: - /* If file still fits the request, ask osd for updated info. - The regulat stat is almost of the same speed as some new + /* If file still fits the request, ask ost for updated info. + The regular stat is almost of the same speed as some new 'glimpse-size-ioctl'. */ if (!decision && S_ISREG(st->st_mode) && - (param->lmd->lmd_lmm.lmm_stripe_count || param->size)) { + param->lmd->lmd_lmm.lmm_stripe_count && + (param->size ||param->atime || param->mtime || param->ctime)) { if (param->obdindex != OBD_NOT_FOUND) { /* Check whether the obd is active or not, if it is * not active, just print the object affected by this - * failed ost + * failed ost * */ struct obd_statfs stat_buf; struct obd_uuid uuid_buf; @@ -1219,15 +1902,15 @@ obd_matches: memset(&stat_buf, 0, sizeof(struct obd_statfs)); memset(&uuid_buf, 0, sizeof(struct obd_uuid)); ret = llapi_obd_statfs(path, LL_STATFS_LOV, - param->obdindex, &stat_buf, + param->obdindex, &stat_buf, &uuid_buf); if (ret) { - if (ret == -ENODATA || ret == -ENODEV + if (ret == -ENODATA || ret == -ENODEV || ret == -EIO) errno = EIO; - llapi_printf(LLAPI_MSG_NORMAL, + llapi_printf(LLAPI_MSG_NORMAL, "obd_uuid: %s failed %s ", - param->obduuid->uuid, + param->obduuid->uuid, strerror(errno)); goto print_path; } @@ -1242,14 +1925,14 @@ obd_matches: if (ret) { if (errno == ENOENT) { - llapi_err(LLAPI_MSG_ERROR, + llapi_err(LLAPI_MSG_ERROR, "warning: %s: %s does not exist", - __FUNCTION__, path); + __func__, path); goto decided; } else { - llapi_err(LLAPI_MSG_ERROR, + llapi_err(LLAPI_MSG_ERROR, "%s: IOC_LOV_GETINFO on %s failed", - __FUNCTION__, path); + __func__, path); return ret; } } @@ -1283,44 +1966,82 @@ decided: return 0; } -static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data, - cfs_dirent_t *de) +int llapi_find(char *path, struct find_param *param) { - struct find_param *param = (struct find_param *)data; - param->depth--; + return param_callback(path, cb_find_init, cb_common_fini, param); +} + +/* + * Get MDT number that the file/directory inode referenced + * by the open fd resides on. + * Return 0 and mdtidx on success, or -ve errno. + */ +int llapi_file_fget_mdtidx(int fd, int *mdtidx) +{ + if (ioctl(fd, LL_IOC_GET_MDTIDX, &mdtidx) < 0) + return -errno; return 0; } -int llapi_find(char *path, struct find_param *param) +static int cb_get_mdt_index(char *path, DIR *parent, DIR *d, void *data, + cfs_dirent_t *de) { - char *buf; - int ret, len = strlen(path); + struct find_param *param = (struct find_param *)data; + int ret = 0; + int mdtidx; - if (len > PATH_MAX) { - llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long", - __FUNCTION__, path); - return -EINVAL; - } + LASSERT(parent != NULL || d != NULL); - buf = (char *)malloc(PATH_MAX + 1); - if (!buf) - return -ENOMEM; + if (d) { + ret = llapi_file_fget_mdtidx(dirfd(d), &mdtidx); + } else if (parent) { + int fd; + + fd = open(path, O_RDONLY); + if (fd > 0) { + ret = llapi_file_fget_mdtidx(fd, &mdtidx); + close(fd); + } else { + ret = fd; + } + } - ret = common_param_init(param); if (ret) { - free(buf); + if (errno == ENODATA) { + if (!param->obduuid) + llapi_printf(LLAPI_MSG_NORMAL, + "%s has no stripe info\n", path); + goto out; + } else if (errno == ENOTTY) { + llapi_err(LLAPI_MSG_ERROR, + "%s: '%s' not on a Lustre fs?", + __func__, path); + } else if (errno == ENOENT) { + llapi_err(LLAPI_MSG_WARN, + "warning: %s: %s does not exist", + __func__, path); + goto out; + } else { + llapi_err(LLAPI_MSG_ERROR, + "error: %s: LL_IOC_GET_MDTIDX failed for %s", + __func__, path); + } return ret; } - param->depth = 0; + if (param->quiet) + llapi_printf(LLAPI_MSG_NORMAL, "%d\n", mdtidx); + else + llapi_printf(LLAPI_MSG_NORMAL, "%s MDT index: %d\n", + path, mdtidx); - strncpy(buf, path, PATH_MAX + 1); - ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_find_init, - cb_common_fini, param, NULL); +out: + /* Do not get down anymore? */ + if (param->depth == param->maxdepth) + return 1; - find_param_fini(param); - free(buf); - return ret < 0 ? ret : 0; + param->depth++; + return 0; } static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data, @@ -1331,8 +2052,8 @@ static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data, LASSERT(parent != NULL || d != NULL); - /* Prepare odb. */ - if (!param->got_uuids) { + if (param->obduuid) { + param->quiet = 1; ret = setup_obd_uuid(d ? d : parent, path, param); if (ret) return ret; @@ -1346,36 +2067,39 @@ static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data, fname = (fname == NULL ? path : fname + 1); strncpy((char *)¶m->lmd->lmd_lmm, fname, param->lumlen); + ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE, (void *)¶m->lmd->lmd_lmm); } if (ret) { if (errno == ENODATA) { - if (!param->obduuid && !param->quiet) - llapi_printf(LLAPI_MSG_NORMAL, + if (!param->obduuid) + llapi_printf(LLAPI_MSG_NORMAL, "%s has no stripe info\n", path); goto out; } else if (errno == ENOTTY) { - llapi_err(LLAPI_MSG_ERROR, + llapi_err(LLAPI_MSG_ERROR, "%s: '%s' not on a Lustre fs?", - __FUNCTION__, path); + __func__, path); } else if (errno == ENOENT) { - llapi_err(LLAPI_MSG_WARN, + llapi_err(LLAPI_MSG_WARN, "warning: %s: %s does not exist", - __FUNCTION__, path); + __func__, path); goto out; } else { - llapi_err(LLAPI_MSG_ERROR, + llapi_err(LLAPI_MSG_ERROR, "error: %s: %s failed for %s", - __FUNCTION__, d ? "LL_IOC_LOV_GETSTRIPE" : + __func__, d ? "LL_IOC_LOV_GETSTRIPE" : "IOC_MDC_GETFILESTRIPE", path); } return ret; } - llapi_lov_dump_user_lmm(param, path, d ? 1 : 0); + if (!param->get_mdt_index) + llapi_lov_dump_user_lmm(param, path, d ? 1 : 0); + out: /* Do not get down anymore? */ if (param->depth == param->maxdepth) @@ -1387,34 +2111,9 @@ out: int llapi_getstripe(char *path, struct find_param *param) { - char *buf; - int ret = 0, len = strlen(path); - - if (len > PATH_MAX) { - llapi_err(LLAPI_MSG_ERROR, - "%s: Path name '%s' is too long", - __FUNCTION__, path); - return -EINVAL; - } - - buf = (char *)malloc(PATH_MAX + 1); - if (!buf) - return -ENOMEM; - - ret = common_param_init(param); - if (ret) { - free(buf); - return ret; - } - - param->depth = 0; - - strncpy(buf, path, PATH_MAX + 1); - ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_getstripe, - cb_common_fini, param, NULL); - find_param_fini(param); - free(buf); - return ret < 0 ? ret : 0; + return param_callback(path, param->get_mdt_index ? + cb_get_mdt_index : cb_getstripe, + cb_common_fini, param); } int llapi_obd_statfs(char *path, __u32 type, __u32 index, @@ -1437,7 +2136,7 @@ int llapi_obd_statfs(char *path, __u32 type, __u32 index, data.ioc_plen2 = sizeof(struct obd_uuid); if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) { - llapi_err(LLAPI_MSG_ERROR, + llapi_err(LLAPI_MSG_ERROR, "llapi_obd_statfs: error packing ioctl data"); return rc; } @@ -1448,8 +2147,8 @@ int llapi_obd_statfs(char *path, __u32 type, __u32 index, if (fd < 0) { rc = errno ? -errno : -EBADF; - llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'", - __FUNCTION__, path); + llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'", + __func__, path); return rc; } rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf); @@ -1487,7 +2186,7 @@ int llapi_ping(char *obd_type, char *obd_name) return rc; } -int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t cb) +int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb) { char buf[MAX_STRING_SIZE]; FILE *fp = fopen(DEVICES_LIST, "r"); @@ -1503,8 +2202,6 @@ int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t c char *obd_type_name = NULL; char *obd_name = NULL; char *obd_uuid = NULL; - char rawbuf[OBD_MAX_IOCTL_BUFFER]; - char *bufl = rawbuf; char *bufp = buf; struct obd_ioctl_data datal = { 0, }; struct obd_statfs osfs_buffer; @@ -1520,7 +2217,6 @@ int llapi_target_iterate(int type_num, char **obd_type, void *args, llapi_cb_t c memset(&osfs_buffer, 0, sizeof (osfs_buffer)); - memset(bufl, 0, sizeof(rawbuf)); datal.ioc_pbuf1 = (char *)&osfs_buffer; datal.ioc_plen1 = sizeof(osfs_buffer); @@ -1697,13 +2393,13 @@ static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data, if (rc) { if (errno == ENODATA) { if (!param->obduuid && !param->quiet) - llapi_err(LLAPI_MSG_ERROR, + llapi_err(LLAPI_MSG_ERROR, "%s has no stripe info", path); rc = 0; } else if (errno == ENOENT) { - llapi_err(LLAPI_MSG_ERROR, + llapi_err(LLAPI_MSG_ERROR, "warning: %s: %s does not exist", - __FUNCTION__, path); + __func__, path); rc = 0; } else if (errno != EISDIR) { rc = errno; @@ -1725,7 +2421,8 @@ static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data, rc = chmod(path, st->st_mode); if (rc) - llapi_err(LLAPI_MSG_ERROR,"error: chmod %s (%hu)", path, st->st_mode); + llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)", + path, st->st_mode); return rc; } @@ -1733,35 +2430,13 @@ static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data, int llapi_quotachown(char *path, int flag) { struct find_param param; - char *buf; - int ret = 0, len = strlen(path); - - if (len > PATH_MAX) { - llapi_err(LLAPI_MSG_ERROR, "%s: Path name '%s' is too long", - __FUNCTION__, path); - return -EINVAL; - } - - buf = (char *)malloc(PATH_MAX + 1); - if (!buf) - return -ENOMEM; memset(¶m, 0, sizeof(param)); param.recursive = 1; param.verbose = 0; param.quiet = 1; - ret = common_param_init(¶m); - if (ret) - goto out; - - strncpy(buf, path, PATH_MAX + 1); - ret = llapi_semantic_traverse(buf, PATH_MAX + 1, NULL, cb_quotachown, - NULL, ¶m, NULL); -out: - find_param_fini(¶m); - free(buf); - return ret; + return param_callback(path, cb_quotachown, NULL, ¶m); } #include @@ -1801,7 +2476,7 @@ static int rmtacl_notify(int ops) if (rc < 0) { perror("ioctl"); return -1; - } + } found++; } @@ -2054,3 +2729,520 @@ int llapi_ls(int argc, char *argv[]) exit(execvp(argv[0], argv)); } + +/* Print mdtname 'name' into 'buf' using 'format'. Add -MDT0000 if needed. + * format must have %s%s, buf must be > 16 + * Eg: if name = "lustre-MDT0000", "lustre", or "lustre-MDT0000_UUID" + * then buf = "lustre-MDT0000" + */ +static int get_mdtname(char *name, char *format, char *buf) +{ + char suffix[]="-MDT0000"; + int len = strlen(name); + + if ((len > 5) && (strncmp(name + len - 5, "_UUID", 5) == 0)) { + name[len - 5] = '\0'; + len -= 5; + } + + if (len > 8) { + if ((len <= 16) && strncmp(name + len - 8, "-MDT", 4) == 0) { + suffix[0] = '\0'; + } else { + /* Not enough room to add suffix */ + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "MDT name too long |%s|", name); + return -EINVAL; + } + } + + return sprintf(buf, format, name, suffix); +} + +/** ioctl on filsystem root, with mdtindex sent as data + * \param mdtname path, fsname, or mdtname (lutre-MDT0004) + * \param mdtidxp pointer to integer within data to be filled in with the + * mdt index (0 if no mdt is specified). NULL won't be filled. + */ +static int root_ioctl(const char *mdtname, int opc, void *data, int *mdtidxp, + int want_error) +{ + char fsname[20]; + char *ptr; + int fd, index, rc; + + /* Take path, fsname, or MDTname. Assume MDT0000 in the former cases. + Open root and parse mdt index. */ + if (mdtname[0] == '/') { + index = 0; + rc = get_root_path(WANT_FD | want_error, NULL, &fd, + (char *)mdtname, -1); + } else { + if (get_mdtname((char *)mdtname, "%s%s", fsname) < 0) + return -EINVAL; + ptr = fsname + strlen(fsname) - 8; + *ptr = '\0'; + index = strtol(ptr + 4, NULL, 10); + rc = get_root_path(WANT_FD | want_error, fsname, &fd, NULL, -1); + } + if (rc < 0) { + if (want_error) + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "Can't open %s: %d\n", mdtname, rc); + return rc; + } + + if (mdtidxp) + *mdtidxp = index; + + rc = ioctl(fd, opc, data); + if (rc && want_error) + llapi_err(LLAPI_MSG_ERROR, "ioctl %d err %d", opc, rc); + + close(fd); + return rc; +} + +/****** Changelog API ********/ + +static int changelog_ioctl(const char *mdtname, int opc, int id, + long long recno, int flags) +{ + struct ioc_changelog data; + int *idx; + + data.icc_id = id; + data.icc_recno = recno; + data.icc_flags = flags; + idx = (int *)(&data.icc_mdtindex); + + return root_ioctl(mdtname, opc, &data, idx, WANT_ERROR); +} + +#define CHANGELOG_PRIV_MAGIC 0xCA8E1080 +struct changelog_private { + int magic; + int flags; + lustre_kernelcomm kuc; + char *buf; +}; + +/** Start reading from a changelog + * @param priv Opaque private control structure + * @param flags Start flags (e.g. CHANGELOG_FLAG_BLOCK) + * @param device Report changes recorded on this MDT + * @param startrec Report changes beginning with this record number + * (just call llapi_changelog_fini when done; don't need an endrec) + */ +int llapi_changelog_start(void **priv, int flags, const char *device, + long long startrec) +{ + struct changelog_private *cp; + int rc; + + /* Set up the receiver control struct */ + cp = malloc(sizeof(*cp)); + if (cp == NULL) + return -ENOMEM; + + cp->buf = malloc(CR_MAXSIZE); + if (cp->buf == NULL) { + rc = -ENOMEM; + goto out_free; + } + + cp->magic = CHANGELOG_PRIV_MAGIC; + cp->flags = flags; + + /* Set up the receiver */ + rc = libcfs_ukuc_start(&cp->kuc, 0 /* no group registration */); + if (rc < 0) + goto out_free; + + *priv = cp; + + /* Tell the kernel to start sending */ + rc = changelog_ioctl(device, OBD_IOC_CHANGELOG_SEND, cp->kuc.lk_wfd, + startrec, flags); + /* Only the kernel reference keeps the write side open */ + close(cp->kuc.lk_wfd); + cp->kuc.lk_wfd = 0; + if (rc < 0) { + /* frees and clears priv */ + llapi_changelog_fini(priv); + return rc; + } + + return 0; + +out_free: + if (cp->buf) + free(cp->buf); + free(cp); + return rc; +} + +/** Finish reading from a changelog */ +int llapi_changelog_fini(void **priv) +{ + struct changelog_private *cp = (struct changelog_private *)*priv; + + if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC)) + return -EINVAL; + + libcfs_ukuc_stop(&cp->kuc); + free(cp->buf); + free(cp); + *priv = NULL; + return 0; +} + +/** Read the next changelog entry + * @param priv Opaque private control structure + * @param rech Changelog record handle; record will be allocated here + * @return 0 valid message received; rec is set + * <0 error code + * 1 EOF + */ +int llapi_changelog_recv(void *priv, struct changelog_rec **rech) +{ + struct changelog_private *cp = (struct changelog_private *)priv; + struct kuc_hdr *kuch; + int rc = 0; + + if (!cp || (cp->magic != CHANGELOG_PRIV_MAGIC)) + return -EINVAL; + if (rech == NULL) + return -EINVAL; + +repeat: + rc = libcfs_ukuc_msg_get(&cp->kuc, cp->buf, CR_MAXSIZE, + KUC_TRANSPORT_CHANGELOG); + if (rc < 0) + return rc; + + kuch = (struct kuc_hdr *)cp->buf; + if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) || + ((kuch->kuc_msgtype != CL_RECORD) && + (kuch->kuc_msgtype != CL_EOF))) { + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "Unknown changelog message type %d:%d\n", + kuch->kuc_transport, kuch->kuc_msgtype); + rc = -EPROTO; + goto out_free; + } + + if (kuch->kuc_msgtype == CL_EOF) { + if (cp->flags & CHANGELOG_FLAG_FOLLOW) { + /* Ignore EOFs */ + goto repeat; + } else { + rc = 1; + goto out_free; + } + } + + /* Our message is a changelog_rec */ + *rech = (struct changelog_rec *)(kuch + 1); + + return 0; + +out_free: + *rech = NULL; + return rc; +} + +/** Release the changelog record when done with it. */ +int llapi_changelog_free(struct changelog_rec **rech) +{ + *rech = NULL; + return 0; +} + +int llapi_changelog_clear(const char *mdtname, const char *idstr, + long long endrec) +{ + int id; + + if (endrec < 0) { + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "can't purge negative records\n"); + return -EINVAL; + } + + id = strtol(idstr + strlen(CHANGELOG_USER_PREFIX), NULL, 10); + if ((id == 0) || (strncmp(idstr, CHANGELOG_USER_PREFIX, + strlen(CHANGELOG_USER_PREFIX)) != 0)) { + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "expecting id of the form '"CHANGELOG_USER_PREFIX + "'; got '%s'\n", idstr); + return -EINVAL; + } + + return changelog_ioctl(mdtname, OBD_IOC_CHANGELOG_CLEAR, id, endrec, 0); +} + +int llapi_fid2path(const char *device, const char *fidstr, char *buf, + int buflen, long long *recno, int *linkno) +{ + char path[PATH_MAX]; + struct lu_fid fid; + struct getinfo_fid2path *gf; + int fd, rc; + + while (*fidstr == '[') + fidstr++; + + sscanf(fidstr, SFID, RFID(&fid)); + if (!fid_is_sane(&fid)) { + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "bad FID format [%s], should be "DFID"\n", + fidstr, (__u64)1, 2, 0); + return -EINVAL; + } + + /* Take path or fsname */ + if (device[0] == '/') { + strcpy(path, device); + } else { + rc = get_root_path(WANT_PATH | WANT_ERROR, (char *)device, + NULL, path, -1); + if (rc < 0) + return rc; + } + sprintf(path, "%s/%s/fid/%s", path, dot_lustre_name, fidstr); + fd = open(path, O_RDONLY | O_NONBLOCK); + if (fd < 0) + return -errno; + + gf = malloc(sizeof(*gf) + buflen); + gf->gf_fid = fid; + gf->gf_recno = *recno; + gf->gf_linkno = *linkno; + gf->gf_pathlen = buflen; + rc = ioctl(fd, OBD_IOC_FID2PATH, gf); + if (rc) { + llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc); + } else { + memcpy(buf, gf->gf_path, gf->gf_pathlen); + *recno = gf->gf_recno; + *linkno = gf->gf_linkno; + } + + free(gf); + close(fd); + return rc; +} + +static int path2fid_from_lma(const char *path, lustre_fid *fid) +{ + char buf[512]; + struct lustre_mdt_attrs *lma; + int rc; + + rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf)); + if (rc < 0) + return -errno; + lma = (struct lustre_mdt_attrs *)buf; + fid_le_to_cpu(fid, &lma->lma_self_fid); + return 0; +} + +int llapi_path2fid(const char *path, lustre_fid *fid) +{ + int fd, rc; + + memset(fid, 0, sizeof(*fid)); + fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW); + if (fd < 0) { + if (errno == ELOOP) /* symbolic link */ + return path2fid_from_lma(path, fid); + return -errno; + } + + rc = ioctl(fd, LL_IOC_PATH2FID, fid) < 0 ? -errno : 0; + if (rc == -EINVAL) /* char special device */ + rc = path2fid_from_lma(path, fid); + + close(fd); + return rc; +} + +/****** HSM Copytool API ********/ +#define CT_PRIV_MAGIC 0xC0BE2001 +struct copytool_private { + int magic; + char *buf; + char *fsname; + lustre_kernelcomm kuc; + __u32 archives; +}; + +#include + +/** Register a copytool + * @param[out] priv Opaque private control structure + * @param fsname Lustre filesystem + * @param flags Open flags, currently unused (e.g. O_NONBLOCK) + * @param archive_count + * @param archives Which archive numbers this copytool is responsible for + */ +int llapi_copytool_start(void **priv, char *fsname, int flags, + int archive_count, int *archives) +{ + struct copytool_private *ct; + int rc; + + if (archive_count > 0 && archives == NULL) { + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "NULL archive numbers"); + return -EINVAL; + } + + ct = malloc(sizeof(*ct)); + if (ct == NULL) + return -ENOMEM; + + ct->buf = malloc(HAL_MAXSIZE); + ct->fsname = malloc(strlen(fsname) + 1); + if (ct->buf == NULL || ct->fsname == NULL) { + rc = -ENOMEM; + goto out_err; + } + strcpy(ct->fsname, fsname); + ct->magic = CT_PRIV_MAGIC; + ct->archives = 0; + for (rc = 0; rc < archive_count; rc++) { + if (archives[rc] > sizeof(ct->archives)) { + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "Maximum of %d archives supported", + sizeof(ct->archives)); + goto out_err; + } + ct->archives |= 1 << archives[rc]; + } + /* special case: if no archives specified, default to archive #0. */ + if (ct->archives == 0) + ct->archives = 1; + + rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM); + if (rc < 0) + goto out_err; + + /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */ + ct->kuc.lk_data = ct->archives; + rc = root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, + WANT_ERROR); + /* Only the kernel reference keeps the write side open */ + close(ct->kuc.lk_wfd); + ct->kuc.lk_wfd = 0; + if (rc < 0) + goto out_err; + + *priv = ct; + return 0; + +out_err: + if (ct->buf) + free(ct->buf); + if (ct->fsname) + free(ct->fsname); + free(ct); + return rc; +} + +/** Deregister a copytool */ +int llapi_copytool_fini(void **priv) +{ + struct copytool_private *ct = (struct copytool_private *)*priv; + + if (!ct || (ct->magic != CT_PRIV_MAGIC)) + return -EINVAL; + + /* Tell the kernel to stop sending us messages */ + ct->kuc.lk_flags = LK_FLG_STOP; + root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, 0); + + /* Shut down the kernelcomms */ + libcfs_ukuc_stop(&ct->kuc); + + free(ct->buf); + free(ct->fsname); + free(ct); + *priv = NULL; + return 0; +} + +/** Wait for the next hsm_action_list + * @param priv Opaque private control structure + * @param halh Action list handle, will be allocated here + * @param msgsize Number of bytes in the message, will be set here + * @return 0 valid message received; halh and msgsize are set + * <0 error code + */ +int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize) +{ + struct copytool_private *ct = (struct copytool_private *)priv; + struct kuc_hdr *kuch; + struct hsm_action_list *hal; + int rc = 0; + + if (!ct || (ct->magic != CT_PRIV_MAGIC)) + return -EINVAL; + if (halh == NULL || msgsize == NULL) + return -EINVAL; + + rc = libcfs_ukuc_msg_get(&ct->kuc, ct->buf, HAL_MAXSIZE, + KUC_TRANSPORT_HSM); + if (rc < 0) + return rc; + + /* Handle generic messages */ + kuch = (struct kuc_hdr *)ct->buf; + if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC && + kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) { + rc = -ESHUTDOWN; + goto out_free; + } + + if (kuch->kuc_transport != KUC_TRANSPORT_HSM || + kuch->kuc_msgtype != HMT_ACTION_LIST) { + llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO, + "Unknown HSM message type %d:%d\n", + kuch->kuc_transport, kuch->kuc_msgtype); + rc = -EPROTO; + goto out_free; + } + + /* Our message is an hsm_action_list */ + + hal = (struct hsm_action_list *)(kuch + 1); + + /* Check that we have registered for this archive # */ + if (((1 << hal->hal_archive_num) & ct->archives) == 0) { + llapi_err(LLAPI_MSG_INFO | LLAPI_MSG_NO_ERRNO, + "Ignoring request for archive #%d (bitmask %#x)\n", + hal->hal_archive_num, ct->archives); + rc = 0; + goto out_free; + } + + *halh = hal; + *msgsize = kuch->kuc_msglen - sizeof(*kuch); + return 0; + +out_free: + *halh = NULL; + *msgsize = 0; + return rc; +} + +/** Release the action list when done with it. */ +int llapi_copytool_free(struct hsm_action_list **hal) +{ + *hal = NULL; + return 0; +} + + +