Whamcloud - gitweb
New release 2.12.7
[fs/lustre-release.git] / lustre / utils / liblustreapi_layout.c
index c5a268a..d69f6c8 100644 (file)
@@ -545,8 +545,10 @@ struct llapi_layout *llapi_layout_get_by_xattr(void *lov_xattr,
 
                if (v1->lmm_pattern == LOV_PATTERN_RAID0)
                        comp->llc_pattern = LLAPI_LAYOUT_RAID0;
+               else if (v1->lmm_pattern == LOV_PATTERN_MDT)
+                       comp->llc_pattern = LLAPI_LAYOUT_MDT;
                else
-                       /* Lustre only supports RAID0 for now. */
+                       /* Lustre only supports RAID0 and DoM for now. */
                        comp->llc_pattern = v1->lmm_pattern;
 
                if (v1->lmm_stripe_size == 0)
@@ -775,7 +777,7 @@ static void get_parent_dir(const char *path, char *buf, size_t size)
 {
        char *p;
 
-       strncpy(buf, path, size);
+       strncpy(buf, path, size - 1);
        p = strrchr(buf, '/');
 
        if (p != NULL) {
@@ -2595,13 +2597,14 @@ int llapi_mirror_resync_many(int fd, struct llapi_layout *layout,
                             struct llapi_resync_comp *comp_array,
                             int comp_size,  uint64_t start, uint64_t end)
 {
-       size_t count;
+       uint64_t count;
        size_t page_size = sysconf(_SC_PAGESIZE);
        const size_t buflen = 4 << 20; /* 4M */
        void *buf;
        uint64_t pos = start;
        int i;
        int rc;
+       int rc2 = 0;
 
        rc = posix_memalign(&buf, page_size, buflen);
        if (rc)
@@ -2615,6 +2618,7 @@ int llapi_mirror_resync_many(int fd, struct llapi_layout *layout,
        while (count > 0) {
                uint32_t src;
                uint64_t mirror_end = 0;
+               uint64_t bytes_left;
                ssize_t bytes_read;
                size_t to_read;
                size_t to_write;
@@ -2624,12 +2628,12 @@ int llapi_mirror_resync_many(int fd, struct llapi_layout *layout,
                        return -ENOENT;
 
                if (mirror_end == OBD_OBJECT_EOF) {
-                       to_read = count;
+                       bytes_left = count;
                } else {
-                       to_read = MIN(count, mirror_end - pos);
-                       to_read = (to_read + page_size - 1) & ~(page_size - 1);
+                       bytes_left = MIN(count, mirror_end - pos);
+                       bytes_left = ((bytes_left - 1) | (page_size - 1)) + 1;
                }
-               to_read = MIN(buflen, to_read);
+               to_read = MIN(buflen, bytes_left);
 
                bytes_read = llapi_mirror_read(fd, src, buf, to_read, pos);
                if (bytes_read == 0) {
@@ -2642,7 +2646,7 @@ int llapi_mirror_resync_many(int fd, struct llapi_layout *layout,
                }
 
                /* round up to page align to make direct IO happy. */
-               to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
+               to_write = ((bytes_read - 1) | (page_size - 1)) + 1;
 
                for (i = 0; i < comp_size; i++) {
                        ssize_t written;
@@ -2679,6 +2683,11 @@ int llapi_mirror_resync_many(int fd, struct llapi_layout *layout,
                                 * meanings.
                                 */
                                comp_array[i].lrc_synced = true;
+                               llapi_error(LLAPI_MSG_ERROR, written,
+                                           "component %u not synced\n",
+                                           comp_array[i].lrc_id);
+                               if (rc2 == 0)
+                                       rc2 = (int)written;
                                continue;
                        }
                        assert(written == to_write2);
@@ -2691,11 +2700,17 @@ int llapi_mirror_resync_many(int fd, struct llapi_layout *layout,
        free(buf);
 
        if (rc < 0) {
+               /* fatal error happens */
                for (i = 0; i < comp_size; i++)
                        comp_array[i].lrc_synced = false;
                return rc;
        }
 
+       /**
+        * no fatal error happens, each lrc_synced tells whether the component
+        * has been resync successfully (note: we'd reverse the value to
+        * reflect its true meaning.
+        */
        for (i = 0; i < comp_size; i++) {
                comp_array[i].lrc_synced = !comp_array[i].lrc_synced;
                if (comp_array[i].lrc_synced && pos & (page_size - 1)) {
@@ -2706,6 +2721,120 @@ int llapi_mirror_resync_many(int fd, struct llapi_layout *layout,
                }
        }
 
-       /* partially successful is successful */
+       /**
+        * returns the first error code for partially successful resync if
+        * possible.
+        */
+       return rc2;
+}
+
+int lov_comp_md_size(struct lov_comp_md_v1 *lcm)
+{
+       if (lcm->lcm_magic == LOV_MAGIC_V1 || lcm->lcm_magic == LOV_MAGIC_V3) {
+               struct lov_user_md *lum = (void *)lcm;
+
+               return lov_user_md_size(lum->lmm_stripe_count, lum->lmm_magic);
+       }
+
+       if (lcm->lcm_magic != LOV_MAGIC_COMP_V1)
+               return -EOPNOTSUPP;
+
+       return lcm->lcm_size;
+}
+
+int llapi_get_lum_file_fd(int dir_fd, const char *fname, __u64 *valid,
+                         lstatx_t *statx, struct lov_user_md *lum,
+                         size_t lumsize)
+{
+       struct lov_user_mds_data *lmd;
+       char buf[65536 + offsetof(typeof(*lmd), lmd_lmm)];
+       int parent_fd = -1;
+       int rc;
+
+       if (lum && lumsize < sizeof(*lum))
+               return -EINVAL;
+
+       /* If a file name is provided, it is relative to the parent directory */
+       if (fname) {
+               parent_fd = dir_fd;
+               dir_fd = -1;
+       }
+
+       lmd = (struct lov_user_mds_data *)buf;
+       rc = get_lmd_info_fd(fname, parent_fd, dir_fd, buf, sizeof(buf),
+                            GET_LMD_INFO);
+       if (rc)
+               return rc;
+
+       *valid = lmd->lmd_flags;
+       if (statx)
+               memcpy(statx, &lmd->lmd_stx, sizeof(*statx));
+
+       if (lum) {
+               if (lmd->lmd_lmmsize > lumsize)
+                       return -EOVERFLOW;
+               memcpy(lum, &lmd->lmd_lmm, lmd->lmd_lmmsize);
+       }
+
        return 0;
 }
+
+int llapi_get_lum_dir_fd(int dir_fd, __u64 *valid, lstatx_t *statx,
+                        struct lov_user_md *lum, size_t lumsize)
+{
+       return llapi_get_lum_file_fd(dir_fd, NULL, valid, statx, lum, lumsize);
+}
+
+int llapi_get_lum_file(const char *path, __u64 *valid, lstatx_t *statx,
+                      struct lov_user_md *lum, size_t lumsize)
+{
+       char parent[PATH_MAX];
+       const char *fname;
+       char *tmp;
+       int offset;
+       int dir_fd;
+       int rc;
+
+       tmp = strrchr(path, '/');
+       if (!tmp) {
+               strncpy(parent, ".", sizeof(parent) - 1);
+               offset = -1;
+       } else {
+               strncpy(parent, path, tmp - path);
+               offset = tmp - path - 1;
+               parent[tmp - path] = 0;
+       }
+
+       fname = path;
+       if (offset >= 0)
+               fname += offset + 2;
+
+       dir_fd = open(parent, O_RDONLY);
+       if (dir_fd < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
+               return rc;
+       }
+
+       rc = llapi_get_lum_file_fd(dir_fd, fname, valid, statx, lum, lumsize);
+       close(dir_fd);
+       return rc;
+}
+
+int llapi_get_lum_dir(const char *path, __u64 *valid, lstatx_t *statx,
+                     struct lov_user_md *lum, size_t lumsize)
+{
+       int dir_fd;
+       int rc;
+
+       dir_fd = open(path, O_RDONLY);
+       if (dir_fd < 0) {
+               rc = -errno;
+               llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
+               return rc;
+       }
+
+       rc = llapi_get_lum_dir_fd(dir_fd, valid, statx, lum, lumsize);
+       close(dir_fd);
+       return rc;
+}