Whamcloud - gitweb
LU-1187 utils: add lfs setdirstripe/getdirstripe
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index 48a1453..f23f11f 100644 (file)
@@ -61,6 +61,7 @@
 #include <sys/xattr.h>
 #include <fnmatch.h>
 #include <glob.h>
+#include <libgen.h> /* for dirname() */
 #ifdef HAVE_LINUX_UNISTD_H
 #include <linux/unistd.h>
 #else
@@ -647,6 +648,124 @@ int llapi_file_create_pool(const char *name, unsigned long long stripe_size,
         return 0;
 }
 
+/**
+ * In DNE phase I, only stripe_offset will be used in this function.
+ * stripe_count, stripe_pattern and pool_name will be supported later.
+ */
+int llapi_dir_create_pool(const char *name, int flags, int stripe_offset,
+                         int stripe_count, int stripe_pattern, char *pool_name)
+{
+       struct lmv_user_md lmu = { 0 };
+       struct obd_ioctl_data data = { 0 };
+       char rawbuf[8192];
+       char *buf = rawbuf;
+       char *dirpath = NULL;
+       char *namepath = NULL;
+       char *dir;
+       char *filename;
+       int fd = -1;
+       int rc;
+
+       dirpath = strdup(name);
+       namepath = strdup(name);
+       if (!dirpath || !namepath)
+               return -ENOMEM;
+
+       lmu.lum_magic = LMV_USER_MAGIC;
+       lmu.lum_stripe_offset = stripe_offset;
+       lmu.lum_stripe_count = stripe_count;
+       lmu.lum_hash_type = stripe_pattern;
+       if (pool_name != NULL) {
+               if (strlen(pool_name) >= LOV_MAXPOOLNAME) {
+                       llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                 "error LL_IOC_LMV_SETSTRIPE '%s' : too large"
+                                 "pool name: %s", name, pool_name);
+                       GOTO(out, rc = -E2BIG);
+               }
+               memcpy(lmu.lum_pool_name, pool_name, strlen(pool_name));
+       }
+
+       filename = basename(namepath);
+       lmu.lum_type = LMV_STRIPE_TYPE;
+       dir = dirname(dirpath);
+
+       data.ioc_inlbuf1 = (char *)filename;
+       data.ioc_inllen1 = strlen(filename) + 1;
+       data.ioc_inlbuf2 = (char *)&lmu;
+       data.ioc_inllen2 = sizeof(struct lmv_user_md);
+       rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
+       if (rc) {
+               llapi_error(LLAPI_MSG_ERROR, rc,
+                           "error: LL_IOC_LMV_SETSTRIPE pack failed '%s'.",
+                           name);
+               GOTO(out, rc);
+       }
+
+       fd = open(dir, O_DIRECTORY | O_RDONLY);
+       if (fd < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
+               GOTO(out, rc);
+       }
+
+       if (ioctl(fd, LL_IOC_LMV_SETSTRIPE, buf)) {
+               char *errmsg = "stripe already set";
+               rc = -errno;
+               if (errno != EEXIST && errno != EALREADY)
+                       errmsg = strerror(errno);
+
+               llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                 "error on LL_IOC_LMV_SETSTRIPE '%s' (%d): %s",
+                                 name, fd, errmsg);
+       }
+       close(fd);
+out:
+       free(dirpath);
+       free(namepath);
+       return rc;
+}
+
+int llapi_direntry_remove(char *dname)
+{
+       char *dirpath = NULL;
+       char *namepath = NULL;
+       char *dir;
+       char *filename;
+       int fd = -1;
+       int rc = 0;
+
+       dirpath = strdup(dname);
+       namepath = strdup(dname);
+       if (!dirpath || !namepath)
+               return -ENOMEM;
+
+       filename = basename(namepath);
+
+       dir = dirname(dirpath);
+
+       fd = open(dir, O_DIRECTORY | O_RDONLY);
+       if (fd < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'",
+                           filename);
+               GOTO(out, rc);
+       }
+
+       if (ioctl(fd, LL_IOC_REMOVE_ENTRY, filename)) {
+               char *errmsg = strerror(errno);
+               llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                 "error on ioctl "LPX64" for '%s' (%d): %s",
+                                 (__u64)LL_IOC_LMV_SETSTRIPE, filename,
+                                 fd, errmsg);
+       }
+out:
+       free(dirpath);
+       free(namepath);
+       if (fd != -1)
+               close(fd);
+       return rc;
+}
+
 /*
  * Find the fsname, the full path, and/or an open fd.
  * Either the fsname or path must not be NULL
@@ -1121,14 +1240,23 @@ typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
 
 static int common_param_init(struct find_param *param, char *path)
 {
-        param->lumlen = get_mds_md_size(path);
-        param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
-        if (param->lmd == NULL) {
-                llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
-                            "error: allocation of %d bytes for ioctl",
-                            sizeof(lstat_t) + param->lumlen);
-                return -ENOMEM;
-        }
+       param->lumlen = get_mds_md_size(path);
+       param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
+       if (param->lmd == NULL) {
+               llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
+                           "error: allocation of %d bytes for ioctl",
+                           sizeof(lstat_t) + param->lumlen);
+               return -ENOMEM;
+       }
+
+       param->fp_lmv_count = 256;
+       param->fp_lmv_md = malloc(lmv_user_md_size(256, LMV_MAGIC_V1));
+       if (param->fp_lmv_md == NULL) {
+               llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
+                           "error: allocation of %d bytes for ioctl",
+                           lmv_user_md_size(256, LMV_MAGIC_V1));
+               return -ENOMEM;
+       }
 
        param->got_uuids = 0;
        param->obdindexes = NULL;
@@ -1139,11 +1267,14 @@ static int common_param_init(struct find_param *param, char *path)
 
 static void find_param_fini(struct find_param *param)
 {
-        if (param->obdindexes)
-                free(param->obdindexes);
+       if (param->obdindexes)
+               free(param->obdindexes);
+
+       if (param->lmd)
+               free(param->lmd);
 
-        if (param->lmd)
-                free(param->lmd);
+       if (param->fp_lmv_md)
+               free(param->fp_lmv_md);
 }
 
 static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
@@ -1172,6 +1303,17 @@ static DIR *opendir_parent(char *path)
         return parent;
 }
 
+static int cb_get_dirstripe(char *path, DIR *d, struct find_param *param)
+{
+       struct lmv_user_md *lmv = (struct lmv_user_md *)param->fp_lmv_md;
+       int ret = 0;
+
+       lmv->lum_stripe_count = param->fp_lmv_count;
+       lmv->lum_magic = LMV_MAGIC_V1;
+       ret = ioctl(dirfd(d), LL_IOC_LMV_GETSTRIPE, lmv);
+       return ret;
+}
+
 static int get_lmd_info(char *path, DIR *parent, DIR *dir,
                  struct lov_user_mds_data *lmd, int lumlen)
 {
@@ -1216,14 +1358,14 @@ static int get_lmd_info(char *path, DIR *parent, DIR *dir,
                                     "%s ioctl failed for %s.",
                                     dir ? "LL_IOC_MDC_GETINFO" :
                                     "IOC_MDC_GETFILEINFO", path);
-                } else {
-                        ret = -errno;
-                        llapi_error(LLAPI_MSG_ERROR, ret,
-                                   "error: %s: IOC_MDC_GETFILEINFO failed for %s",
-                                   __func__, path);
-                }
-        }
-        return ret;
+               } else {
+                       ret = -errno;
+                       llapi_error(LLAPI_MSG_ERROR, ret,
+                                "error: %s: IOC_MDC_GETFILEINFO failed for %s",
+                                  __func__, path);
+               }
+       }
+       return ret;
 }
 
 int llapi_mds_getfileinfo(char *path, DIR *parent,
@@ -1235,13 +1377,14 @@ int llapi_mds_getfileinfo(char *path, DIR *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)
+                                  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;
+       struct find_param *param = (struct find_param *)data;
+       cfs_dirent_t *dent;
+       int len, ret;
+       DIR *d, *p = NULL;
 
         ret = 0;
         len = strlen(path);
@@ -1262,11 +1405,11 @@ static int llapi_semantic_traverse(char *path, int size, DIR *parent,
         if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
                 goto err;
 
-        if (!d)
-                GOTO(out, ret = 0);
+       if (!d || (param->get_lmv && !param->recursive))
+               GOTO(out, ret = 0);
 
-        while ((dent = readdir64(d)) != NULL) {
-                ((struct find_param *)data)->have_fileinfo = 0;
+       while ((dent = readdir64(d)) != NULL) {
+               param->have_fileinfo = 0;
 
                 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
                         continue;
@@ -1286,20 +1429,17 @@ static int llapi_semantic_traverse(char *path, int size, DIR *parent,
                 strcat(path, dent->d_name);
 
                 if (dent->d_type == DT_UNKNOWN) {
-                        lstat_t *st = &((struct find_param *)data)->lmd->lmd_st;
+                       lstat_t *st = &param->lmd->lmd_st;
 
-                        ret = llapi_mds_getfileinfo(path, d,
-                                             ((struct find_param *)data)->lmd);
+                       ret = llapi_mds_getfileinfo(path, d, param->lmd);
                         if (ret == 0) {
-                                ((struct find_param *)data)->have_fileinfo = 1;
-                                dent->d_type =
-                                        llapi_filetype_dir_table[st->st_mode &
-                                                                 S_IFMT];
-                        }
+                               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_noerrno(LLAPI_MSG_ERROR,
@@ -2033,10 +2173,89 @@ void lov_dump_user_lmm_v1v3(struct lov_user_md *lum, char *pool_name,
         }
 }
 
-void llapi_lov_dump_user_lmm(struct find_param *param,
-                             char *path, int is_dir)
+void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
+                      char *path, int obdindex, int depth, int verbose)
 {
-        switch(*(__u32 *)&param->lmd->lmd_lmm) { /* lum->lmm_magic */
+       struct lmv_user_mds_data *objects = lum->lum_objects;
+       char *prefix = lum->lum_magic == LMV_USER_MAGIC ? "(Default)" : "";
+       int i, obdstripe = 0;
+
+       if (obdindex != OBD_NOT_FOUND) {
+               for (i = 0; i < lum->lum_stripe_count; i++) {
+                       if (obdindex == objects[i].lum_mds) {
+                               llapi_printf(LLAPI_MSG_NORMAL, "%s%s\n", prefix,
+                                            path);
+                               obdstripe = 1;
+                               break;
+                       }
+               }
+       } else {
+               obdstripe = 1;
+       }
+
+       /* show all information default */
+       if (!verbose) {
+               if (lum->lum_magic == LMV_USER_MAGIC)
+                       verbose = VERBOSE_POOL | VERBOSE_COUNT | VERBOSE_OFFSET;
+               else
+                       verbose = VERBOSE_OBJID;
+       }
+
+       if (lum->lum_magic == LMV_USER_MAGIC)
+               verbose &= ~VERBOSE_OBJID;
+
+       if (depth && path && ((verbose != VERBOSE_OBJID)))
+               llapi_printf(LLAPI_MSG_NORMAL, "%s\n", path);
+
+       if (verbose & VERBOSE_COUNT) {
+               if (verbose & ~VERBOSE_COUNT)
+                       llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_count: ");
+               llapi_printf(LLAPI_MSG_NORMAL, "%u\n",
+                            (int)lum->lum_stripe_count);
+       }
+
+       if (verbose & VERBOSE_OFFSET) {
+               if (verbose & ~VERBOSE_OFFSET)
+                       llapi_printf(LLAPI_MSG_NORMAL, "lmv_stripe_offset: ");
+               llapi_printf(LLAPI_MSG_NORMAL, "%d\n",
+                            (int)lum->lum_stripe_offset);
+       }
+
+       if (verbose & VERBOSE_OBJID) {
+               if ((obdstripe == 1))
+                       llapi_printf(LLAPI_MSG_NORMAL,
+                                    "\tmdtidx\t\t FID[seq:oid:ver]\n");
+               for (i = 0; i < lum->lum_stripe_count; i++) {
+                       int idx = objects[i].lum_mds;
+                       struct lu_fid *fid = &objects[i].lum_fid;
+                       if ((obdindex == OBD_NOT_FOUND) || (obdindex == idx))
+                               llapi_printf(LLAPI_MSG_NORMAL,
+                                            "\t%6u\t\t "DFID"\t\t%s\n",
+                                           idx, PFID(fid),
+                                           obdindex == idx ? " *" : "");
+               }
+
+       }
+
+       if ((verbose & VERBOSE_POOL) && (pool_name[0] != '\0')) {
+               if (verbose & ~VERBOSE_POOL)
+                       llapi_printf(LLAPI_MSG_NORMAL, "%slmv_pool:           ",
+                                    prefix);
+               llapi_printf(LLAPI_MSG_NORMAL, "%s%c ", pool_name, ' ');
+       }
+       llapi_printf(LLAPI_MSG_NORMAL, "\n");
+}
+
+void llapi_lov_dump_user_lmm(struct find_param *param, char *path, int is_dir)
+{
+       __u32 magic;
+
+       if (param->get_lmv)
+               magic = (__u32)param->fp_lmv_md->lum_magic;
+       else
+               magic = *(__u32 *)&param->lmd->lmd_lmm; /* lum->lmm_magic */
+
+       switch (magic) {
         case LOV_USER_MAGIC_V1:
                 lov_dump_user_lmm_v1v3(&param->lmd->lmd_lmm, NULL,
                                        param->lmd->lmd_lmm.lmm_objects,
@@ -2058,13 +2277,26 @@ void llapi_lov_dump_user_lmm(struct find_param *param,
                                        param->verbose, param->raw);
                 break;
         }
-        default:
-                llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic:  %#x "
-                             "(expecting one of %#x %#x %#x)\n",
-                             *(__u32 *)&param->lmd->lmd_lmm,
-                             LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3);
-                return;
-        }
+       case LMV_MAGIC_V1:
+       case LMV_USER_MAGIC: {
+               char pool_name[LOV_MAXPOOLNAME + 1];
+               struct lmv_user_md *lum;
+
+               lum = (struct lmv_user_md *)param->fp_lmv_md;
+               strncpy(pool_name, lum->lum_pool_name, LOV_MAXPOOLNAME);
+               lmv_dump_user_lmm(lum, pool_name, path,
+                                 param->obdindex, param->maxdepth,
+                                 param->verbose);
+               break;
+       }
+       default:
+               llapi_printf(LLAPI_MSG_NORMAL, "unknown lmm_magic:  %#x "
+                            "(expecting one of %#x %#x %#x %#x)\n",
+                            *(__u32 *)&param->lmd->lmd_lmm,
+                            LOV_USER_MAGIC_V1, LOV_USER_MAGIC_V3,
+                            LMV_USER_MAGIC, LMV_MAGIC_V1);
+               return;
+       }
 }
 
 int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
@@ -2734,18 +2966,30 @@ static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
                         return ret;
         }
 
-        if (d) {
-                ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
-                            (void *)&param->lmd->lmd_lmm);
-        } else if (parent) {
-                char *fname = strrchr(path, '/');
-                fname = (fname == NULL ? path : fname + 1);
+       if (d) {
+               if (param->get_lmv) {
+                       ret = cb_get_dirstripe(path, d, param);
+               } else {
+                       ret = ioctl(dirfd(d), LL_IOC_LOV_GETSTRIPE,
+                                    (void *)&param->lmd->lmd_lmm);
+               }
 
-                strncpy((char *)&param->lmd->lmd_lmm, fname, param->lumlen);
+       } else if (parent) {
+               char *fname = strrchr(path, '/');
+               fname = (fname == NULL ? path : fname + 1);
 
-                ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
-                            (void *)&param->lmd->lmd_lmm);
-        }
+               if (param->get_lmv) {
+                       llapi_printf(LLAPI_MSG_NORMAL,
+                                    "%s get dirstripe information for file\n",
+                                    path);
+                       goto out;
+               }
+
+               strncpy((char *)&param->lmd->lmd_lmm, fname, param->lumlen);
+
+               ret = ioctl(dirfd(parent), IOC_MDC_GETFILESTRIPE,
+                           (void *)&param->lmd->lmd_lmm);
+       }
 
         if (ret) {
                 if (errno == ENODATA && d != NULL) {
@@ -2765,7 +3009,7 @@ static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
                         goto dump;
 
                 } else if (errno == ENODATA && parent != NULL) {
-                        if (!param->obduuid)
+                       if (!param->obduuid && !param->mdtuuid)
                                 llapi_printf(LLAPI_MSG_NORMAL,
                                              "%s has no stripe info\n", path);
                         goto out;