Whamcloud - gitweb
LU-9771 util: rename LCM_FL_NOT_FLR to LCM_FL_NONE
[fs/lustre-release.git] / lustre / utils / liblustreapi_layout.c
index ae2826e..f206c96 100644 (file)
@@ -23,7 +23,7 @@
  * Lustre files while hiding details of the internal data structures
  * from the user.
  *
- * Copyright (c) 2016, Intel Corporation.
+ * Copyright (c) 2016, 2017, Intel Corporation.
  *
  * Author: Ned Bass <bass6@llnl.gov>
  */
@@ -34,7 +34,9 @@
 #include <unistd.h>
 #include <errno.h>
 #include <limits.h>
+#include <assert.h>
 #include <sys/xattr.h>
+#include <sys/param.h>
 
 #include <libcfs/util/list.h>
 #include <lustre/lustreapi.h>
@@ -1015,7 +1017,7 @@ struct llapi_layout *llapi_layout_get_by_path(const char *path, uint32_t flags)
  * \retval     NULL if an error occurs
  */
 struct llapi_layout *llapi_layout_get_by_fid(const char *lustre_dir,
-                                            const lustre_fid *fid,
+                                            const struct lu_fid *fid,
                                             uint32_t flags)
 {
        int fd;
@@ -1537,6 +1539,20 @@ int llapi_layout_flags_set(struct llapi_layout *layout, uint32_t flags)
        return 0;
 }
 
+const char *llapi_layout_flags_string(uint32_t flags)
+{
+       switch (flags & LCM_FL_FLR_MASK) {
+       case LCM_FL_RDONLY:
+               return "ro";
+       case LCM_FL_WRITE_PENDING:
+               return "wp";
+       case LCM_FL_SYNC_PENDING:
+               return "sp";
+       }
+
+       return "0";
+}
+
 /**
  * llapi_layout_mirror_count_is_valid() - Check the validity of mirror count.
  * @count: Mirror count value to be checked.
@@ -2118,13 +2134,100 @@ out:
  * comp->lcme_id value, which must be an unique component ID. The new
  * attributes are passed in by @comp and @valid is used to specify which
  * attributes in the component are going to be changed.
+ *
+ * \param[in] path     path name of the file
+ * \param[in] ids      An array of component IDs
+ * \param[in] flags    flags: LCME_FL_* or;
+ *                     negative flags: (LCME_FL_NEG|LCME_FL_*)
+ * \param[in] count    Number of elements in ids and flags array
  */
-int llapi_layout_file_comp_set(const char *path,
-                              const struct llapi_layout *comp,
-                              uint32_t valid)
+int llapi_layout_file_comp_set(const char *path, uint32_t *ids, uint32_t *flags,
+                              size_t count)
 {
-       errno = EOPNOTSUPP;
-       return -1;
+       int rc = -1, fd = -1, i;
+       size_t lum_size;
+       struct llapi_layout *layout;
+       struct llapi_layout_comp *comp;
+       struct lov_user_md *lum = NULL;
+
+       if (path == NULL) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       if (!count)
+               return 0;
+
+       for (i = 0; i < count; i++) {
+               if (!ids[i] || !flags[i]) {
+                       errno = EINVAL;
+                       return -1;
+               }
+
+               if (ids[i] > LCME_ID_MAX || (flags[i] & ~LCME_KNOWN_FLAGS)) {
+                       errno = EINVAL;
+                       return -1;
+               }
+
+               /* do not allow to set or clear INIT flag */
+               if (flags[i] & LCME_FL_INIT) {
+                       errno = EINVAL;
+                       return -1;
+               }
+       }
+
+       layout = __llapi_layout_alloc();
+       if (layout == NULL)
+               return -1;
+
+       layout->llot_is_composite = true;
+       for (i = 0; i < count; i++) {
+               comp = __llapi_comp_alloc(0);
+               if (comp == NULL)
+                       goto out;
+
+               comp->llc_id = ids[i];
+               comp->llc_flags = flags[i];
+
+               list_add_tail(&comp->llc_list, &layout->llot_comp_list);
+               layout->llot_cur_comp = comp;
+       }
+
+       lum = llapi_layout_to_lum(layout);
+       if (lum == NULL)
+               goto out;
+
+       lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
+
+       fd = open(path, O_RDWR);
+       if (fd < 0)
+               goto out;
+
+       /* flush cached pages from clients */
+       rc = llapi_file_flush(fd);
+       if (rc) {
+               errno = -rc;
+               rc = -1;
+               goto out_close;
+       }
+
+       rc = fsetxattr(fd, XATTR_LUSTRE_LOV".set.flags", lum, lum_size, 0);
+       if (rc < 0)
+               goto out_close;
+
+       rc = 0;
+
+out_close:
+       if (fd >= 0) {
+               int tmp_errno = errno;
+               close(fd);
+               errno = tmp_errno;
+       }
+out:
+       if (lum)
+               free(lum);
+       llapi_layout_free(layout);
+       return rc;
 }
 
 /**
@@ -2141,6 +2244,47 @@ bool llapi_layout_is_composite(struct llapi_layout *layout)
 }
 
 /**
+ * Iterate every components in the @layout and call callback function @cb.
+ *
+ * \param[in] layout   component layout list.
+ * \param[in] cb       callback for each component
+ * \param[in] cbdata   callback data
+ *
+ * \retval < 0                         error happens during the iteration
+ * \retval LLAPI_LAYOUT_ITER_CONT      finished the iteration w/o error
+ * \retval LLAPI_LAYOUT_ITER_STOP      got something, stop the iteration
+ */
+int llapi_layout_comp_iterate(struct llapi_layout *layout,
+                             llapi_layout_iter_cb cb, void *cbdata)
+{
+       int rc;
+
+       rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
+       if (rc < 0)
+               return rc;
+
+       /**
+        * make sure on success llapi_layout_comp_use() API returns 0 with
+        * USE_FIRST.
+        */
+       assert(rc == 0);
+
+       while (1) {
+               rc = cb(layout, cbdata);
+               if (rc != LLAPI_LAYOUT_ITER_CONT)
+                       break;
+
+               rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
+               if (rc < 0)
+                       return rc;
+               else if (rc == 1)       /* reached the last comp */
+                       return LLAPI_LAYOUT_ITER_CONT;
+       }
+
+       return rc;
+}
+
+/**
  * llapi_layout_merge() - Merge a composite layout into another one.
  * @dst_layout: Destination composite layout.
  * @src_layout: Source composite layout.
@@ -2214,3 +2358,188 @@ error:
        llapi_layout_free(new_layout);
        return -1;
 }
+
+/**
+ * Find all stale components.
+ *
+ * \param[in] layout           component layout list.
+ * \param[out] comp            array of stale component info.
+ * \param[in] comp_size                array size of @comp.
+ * \param[in] mirror_ids       array of mirror id that only components
+ *                             belonging to these mirror will be collected.
+ * \param[in] ids_nr           number of mirror ids array.
+ *
+ * \retval             number of component info collected on sucess or
+ *                     an error code on failure.
+ */
+int llapi_mirror_find_stale(struct llapi_layout *layout,
+               struct llapi_resync_comp *comp, size_t comp_size,
+               __u16 *mirror_ids, int ids_nr)
+{
+       int idx = 0;
+       int rc;
+
+       rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
+       if (rc < 0)
+               goto error;
+
+       while (rc == 0) {
+               uint32_t id;
+               uint32_t mirror_id;
+               uint32_t flags;
+               uint64_t start, end;
+
+               rc = llapi_layout_comp_flags_get(layout, &flags);
+               if (rc < 0)
+                       goto error;
+
+               if (!(flags & LCME_FL_STALE))
+                       goto next;
+
+               rc = llapi_layout_mirror_id_get(layout, &mirror_id);
+               if (rc < 0)
+                       goto error;
+
+               /* the caller only wants stale components from specific
+                * mirrors */
+               if (ids_nr > 0) {
+                       int j;
+
+                       for (j = 0; j < ids_nr; j++) {
+                               if (mirror_ids[j] == mirror_id)
+                                       break;
+                       }
+
+                       /* not in the specified mirror */
+                       if (j == ids_nr)
+                               goto next;
+               }
+
+               rc = llapi_layout_comp_id_get(layout, &id);
+               if (rc < 0)
+                       goto error;
+
+               rc = llapi_layout_comp_extent_get(layout, &start, &end);
+               if (rc < 0)
+                       goto error;
+
+               /* pack this component into @comp array */
+               comp[idx].lrc_id = id;
+               comp[idx].lrc_mirror_id = mirror_id;
+               comp[idx].lrc_start = start;
+               comp[idx].lrc_end = end;
+               idx++;
+
+               if (idx >= comp_size) {
+                       rc = -EINVAL;
+                       goto error;
+               }
+
+       next:
+               rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
+               if (rc < 0) {
+                       rc = -EINVAL;
+                       goto error;
+               }
+       }
+error:
+       return rc < 0 ? rc : idx;
+}
+
+/* locate @layout to a valid component covering file [file_start, file_end) */
+static uint32_t llapi_mirror_find(struct llapi_layout *layout,
+                                 uint64_t file_start, uint64_t file_end,
+                                 uint64_t *endp)
+{
+       uint32_t mirror_id = 0;
+       int rc;
+
+       rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
+       if (rc < 0)
+               return rc;
+
+       *endp = 0;
+       while (rc == 0) {
+               uint64_t start, end;
+               uint32_t flags, id, rid;
+
+               rc = llapi_layout_comp_flags_get(layout, &flags);
+               if (rc < 0)
+                       return rc;
+
+               if (flags & LCME_FL_STALE)
+                       goto next;
+
+               rc = llapi_layout_mirror_id_get(layout, &rid);
+               if (rc < 0)
+                       return rc;
+
+               rc = llapi_layout_comp_id_get(layout, &id);
+               if (rc < 0)
+                       return rc;
+
+               rc = llapi_layout_comp_extent_get(layout, &start, &end);
+               if (rc < 0)
+                       return rc;
+
+               if (file_start >= start && file_start < end) {
+                       if (!mirror_id)
+                               mirror_id = rid;
+                       else if (mirror_id != rid || *endp != start)
+                               break;
+
+                       file_start = *endp = end;
+                       if (end >= file_end)
+                               break;
+               }
+
+       next:
+               rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
+               if (rc < 0)
+                       return rc;
+       }
+
+       return mirror_id;
+}
+
+ssize_t llapi_mirror_resync_one(int fd, struct llapi_layout *layout,
+                               uint32_t dst, uint64_t start, uint64_t end)
+{
+       uint64_t mirror_end = 0;
+       ssize_t result = 0;
+       size_t count;
+
+       if (end == OBD_OBJECT_EOF)
+               count = OBD_OBJECT_EOF;
+       else
+               count = end - start;
+
+       while (count > 0) {
+               uint32_t src;
+               size_t to_copy;
+               ssize_t copied;
+
+               src = llapi_mirror_find(layout, start, end, &mirror_end);
+               if (src == 0)
+                       return -ENOENT;
+
+               if (mirror_end == OBD_OBJECT_EOF)
+                       to_copy = count;
+               else
+                       to_copy = MIN(count, mirror_end - start);
+
+               copied = llapi_mirror_copy(fd, src, dst, start, to_copy);
+               if (copied < 0)
+                       return copied;
+
+               result += copied;
+               if (copied < to_copy) /* end of file */
+                       break;
+
+               if (count != OBD_OBJECT_EOF)
+                       count -= copied;
+               start += copied;
+       }
+
+       return result;
+}