Whamcloud - gitweb
LU-17025 llapi: Verify stripe pool name
[fs/lustre-release.git] / lustre / utils / liblustreapi_layout.c
index 374e9e1..3cabaf3 100644 (file)
@@ -1580,6 +1580,7 @@ int llapi_layout_file_open(const char *path, int open_flags, mode_t mode,
        int tmp;
        struct lov_user_md *lum;
        size_t lum_size;
+       char fsname[MAX_OBD_NAME + 1] = { 0 };
 
        if (path == NULL ||
            (layout != NULL && layout->llot_magic != LLAPI_LAYOUT_MAGIC)) {
@@ -1588,8 +1589,16 @@ int llapi_layout_file_open(const char *path, int open_flags, mode_t mode,
        }
 
        if (layout) {
-               rc = llapi_layout_sanity((struct llapi_layout *)layout, false,
-                               !!(layout->llot_mirror_count > 1));
+               /* Make sure we are on a Lustre file system */
+               rc = llapi_search_fsname(path, fsname);
+               if (rc) {
+                       errno = ENOTTY;
+                       return -1;
+               }
+               rc = llapi_layout_v2_sanity((struct llapi_layout *)layout,
+                                           false,
+                                           !!(layout->llot_mirror_count > 1),
+                                           fsname);
                if (rc) {
                        llapi_layout_sanity_perror(rc);
                        return -1;
@@ -2195,6 +2204,7 @@ int llapi_layout_file_comp_add(const char *path,
        int rc, fd = -1, lum_size, tmp_errno = 0;
        struct llapi_layout *existing_layout = NULL;
        struct lov_user_md *lum = NULL;
+       char fsname[MAX_OBD_NAME + 1] = { 0 };
 
        if (path == NULL || layout == NULL ||
            layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
@@ -2223,7 +2233,14 @@ int llapi_layout_file_comp_add(const char *path,
                goto out;
        }
 
-       rc = llapi_layout_sanity(existing_layout, false, false);
+       rc = llapi_search_fsname(path, fsname);
+       if (rc) {
+               tmp_errno = -rc;
+               rc = -1;
+               goto out;
+       }
+
+       rc = llapi_layout_v2_sanity(existing_layout, false, false, fsname);
        if (rc) {
                tmp_errno = errno;
                llapi_layout_sanity_perror(rc);
@@ -2471,6 +2488,7 @@ int llapi_layout_file_comp_set(const char *path, uint32_t *ids, uint32_t *flags,
        struct llapi_layout *layout = NULL;
        struct llapi_layout_comp *comp;
        struct lov_user_md *lum = NULL;
+       char fsname[MAX_OBD_NAME + 1] = { 0 };
 
        if (path == NULL) {
                errno = EINVAL;
@@ -2518,7 +2536,14 @@ int llapi_layout_file_comp_set(const char *path, uint32_t *ids, uint32_t *flags,
                goto out;
        }
 
-       rc = llapi_layout_sanity(existing_layout, false, false);
+       rc = llapi_search_fsname(path, fsname);
+       if (rc) {
+               tmp_errno = -rc;
+               rc = -1;
+               goto out;
+       }
+
+       rc = llapi_layout_v2_sanity(existing_layout, false, false, fsname);
        if (rc) {
                tmp_errno = errno;
                llapi_layout_sanity_perror(rc);
@@ -3343,12 +3368,32 @@ struct llapi_layout_sanity_args {
        bool lsa_flr;
        bool lsa_ondisk;
        int lsa_rc;
+       char *fsname;
 };
 
 /* The component flags can be set by users at creation/modification time. */
 #define LCME_USER_COMP_FLAGS   (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
                                 LCME_FL_EXTENSION)
 
+/* Inline function to verify the pool name */
+static inline int verify_pool_name(char *fsname, struct llapi_layout *layout)
+{
+       struct llapi_layout_comp *comp;
+
+       if (!fsname || fsname[0] == '\0')
+               return 0;
+
+       comp = __llapi_layout_cur_comp(layout);
+       if (!comp)
+               return 0;
+       if (comp->llc_pool_name[0] == '\0')
+               return 0;
+       /* check if the pool name exist */
+       if (llapi_search_ost(fsname, comp->llc_pool_name, NULL) < 0)
+               return -1;
+       return 0;
+}
+
 /**
  * When modified, adjust llapi_stripe_param_verify() if needed as well.
  */
@@ -3365,6 +3410,11 @@ static int llapi_layout_sanity_cb(struct llapi_layout *layout,
                goto out_err;
        }
 
+       if (verify_pool_name(args->fsname, layout) != 0) {
+               args->lsa_rc = -1;
+               goto out_err;
+       }
+
        if (comp->llc_list.prev != &layout->llot_comp_list)
                prev = list_last_entry(&comp->llc_list, typeof(*prev),
                                       llc_list);
@@ -3560,9 +3610,31 @@ void llapi_layout_sanity_perror(int error)
  * \retval                      0, success, positive: various errors, see
  *                              llapi_layout_sanity_perror, -1, failure
  */
+
 int llapi_layout_sanity(struct llapi_layout *layout,
-                       bool incomplete,
-                       bool flr)
+                       bool incomplete, bool flr)
+{
+       return llapi_layout_v2_sanity(layout, incomplete, flr, NULL);
+}
+
+/* This function has been introduced to do pool name checking
+ * on top of llapi_layout_sanity, the file name passed in this
+ * function is used later to verify if pool exist. The older version
+ * of the sanity function is passing NULL for the filename
+ * Input arguments ---
+ * \param[in] layout            component layout list.
+ * \param[in] fname            file the layout to be checked for
+ * \param[in] incomplete        if layout is complete or not - some checks can
+ *                              only be done on complete layouts.
+ * \param[in] flr              set when this is called from FLR mirror create
+ * \param[in] fsname           filesystem name is used to check pool name, if
+ *                             NULL no pool name check is performed
+ *
+ * \retval                      0, success, positive: various errors, see
+ */
+
+int llapi_layout_v2_sanity(struct llapi_layout *layout,
+                       bool incomplete, bool flr, char *fsname)
 {
        struct llapi_layout_sanity_args args = { 0 };
        struct llapi_layout_comp *curr;
@@ -3579,6 +3651,7 @@ int llapi_layout_sanity(struct llapi_layout *layout,
        args.lsa_rc = 0;
        args.lsa_flr = flr;
        args.lsa_incomplete = incomplete;
+       args.fsname = fsname;
 
        /* When we modify an existing layout, this tells us if it's FLR */
        if (mirror_id_of(curr->llc_id) > 0)