Whamcloud - gitweb
LU-8998 tools: support negative flags 90/26490/7
authorNiu Yawei <yawei.niu@intel.com>
Fri, 31 Mar 2017 03:01:46 +0000 (23:01 -0400)
committerOleg Drokin <oleg.drokin@intel.com>
Tue, 16 May 2017 05:44:08 +0000 (05:44 +0000)
- Make 'lfs setstripe --component-flags' support negative flags
  "^init".
- Change llapi_layout_file_comp_del() to accept both 'id' and
  'flags', 'id' must be real ID, 'flags' can be negative flags.
- Fix swab defect in lod_declare_layout_del().
- Update man pages and test scripts accordingly.

Signed-off-by: Niu Yawei <yawei.niu@intel.com>
Change-Id: I9b66492133f56eabc928dbbb41bb8eb5627be095
Reviewed-on: https://review.whamcloud.com/26490
Tested-by: Jenkins
Reviewed-by: Andreas Dilger <andreas.dilger@intel.com>
Tested-by: Maloo <hpdd-maloo@intel.com>
Reviewed-by: Jinshan Xiong <jinshan.xiong@intel.com>
lustre/doc/lfs-setstripe.1
lustre/doc/llapi_layout_file_comp_del.3
lustre/include/lustre/lustre_user.h
lustre/include/lustre/lustreapi.h
lustre/lod/lod_lov.c
lustre/lod/lod_object.c
lustre/tests/llapi_layout_test.c
lustre/tests/sanity-pfl.sh
lustre/utils/lfs.c
lustre/utils/liblustreapi_layout.c

index 9c9f68a..52dc756 100644 (file)
@@ -135,7 +135,8 @@ such as 256M. -1 means the end of file.
 The numerical unique component id.
 .TP
 .B --component-flags <\fIflags\fR>
-Component flags. Available flags: \fBinit\fR: instatiated component.
+Component flags. Available flags: \fBinit\fR: instantiated component.
+\fB^init\fR: uninstantiated component.
 .TP
 .B --component-add
 Add specified components to an existing composite file.
index 5a726df..3e91d07 100644 (file)
@@ -7,25 +7,30 @@ from an existing file.
 .B #include <lustre/lustreapi.h>
 .PP
 .BI "int llapi_layout_file_comp_del(const char *" path ",
-.BI "                               uint32_t "id );
+.BI "                               uint32_t "id ", uint32_t "flags );
 .fi
 .SH DESCRIPTION
 .PP
 Deletes the layout component(s) specified by
 .I id
+or
+.I flags
 from an existing file. The
 .I id
-can be a real unique component ID or a
-.IR lcme_id .
+must be a real unique component ID, when
+.I flags
+is specified,
+.I id
+must be set to zero.
 Available
-.IR lcme_id
+.IR flags
 are:
 .PP
-.BR LCME_ID_ALL:
-All components in the layout.
+.BR LCME_FL_INIT:
+Instantiated components in the layout.
 .PP
-.BR LCME_ID_NONE|LCME_FL_INIT:
-All the instantiated components in the layout.
+.BR LCME_FL_NEG|LCME_FL_INIT:
+Uninstantiated components in the layout.
 .SH RETURN VALUES
 Returns 0 on success, or -1 if an error occurred (in which case, errno is
 set appropriately).
@@ -37,9 +42,6 @@ An invalid argument was specified.
 .SM ENOENT
 .I path
 doesn't exist or the specified component isn't found in file.
-.SH TODO
-Add description for the
-.IR lcme_id .
 .SH "SEE ALSO"
 .BR llapi_layout_alloc (3),
 .BR llapi_layout_file_open (3),
index 6dfa137..5db4dc1 100644 (file)
@@ -522,9 +522,11 @@ enum lov_comp_md_entry_flags {
        LCME_FL_OFFLINE = 0x00000004,   /* Not used */
        LCME_FL_PREFERRED = 0x00000008, /* Not used */
        LCME_FL_INIT    = 0x00000010,   /* instantiated */
+       LCME_FL_NEG     = 0x80000000    /* used to indicate a negative flag,
+                                          won't be stored on disk */
 };
 
-#define LCME_KNOWN_FLAGS       LCME_FL_INIT
+#define LCME_KNOWN_FLAGS       (LCME_FL_NEG | LCME_FL_INIT)
 
 /* lcme_id can be specified as certain flags, and the the first
  * bit of lcme_id is used to indicate that the ID is representing
@@ -534,7 +536,7 @@ enum lcme_id {
        LCME_ID_INVAL   = 0x0,
        LCME_ID_MAX     = 0x7FFFFFFF,
        LCME_ID_ALL     = 0xFFFFFFFF,
-       LCME_ID_NONE    = 0x80000000
+       LCME_ID_NOT_ID  = LCME_FL_NEG
 };
 
 #define LCME_ID_MASK   LCME_ID_MAX
index d701f52..e38956e 100644 (file)
@@ -784,10 +784,9 @@ int llapi_layout_comp_use(struct llapi_layout *layout, uint32_t pos);
 int llapi_layout_file_comp_add(const char *path,
                               const struct llapi_layout *layout);
 /**
- * Delete component(s) by the specified component id (accepting lcme_id
- * wildcards also) from an existing file.
+ * Delete component(s) by the specified component id or flags.
  */
-int llapi_layout_file_comp_del(const char *path, uint32_t id);
+int llapi_layout_file_comp_del(const char *path, uint32_t id, uint32_t flags);
 /**
  * Change flags or other parameters of the component(s) by component ID of an
  * existing file. The component to be modified is specified by the
index 50b3998..6b01a87 100644 (file)
@@ -881,7 +881,7 @@ again:
                if (i == lo->ldo_comp_cnt)
                        return id;
        }
-       if (end > LCME_ID_MAX) {
+       if (end == LCME_ID_MAX) {
                start = 1;
                end = min(lo->ldo_layout_gen & LCME_ID_MASK,
                          (__u32)(LCME_ID_MAX - 1));
index d1ab06c..7b02db6 100644 (file)
@@ -2431,23 +2431,50 @@ static int lod_declare_layout_del(const struct lu_env *env,
                                  struct thandle *th)
 {
        struct lod_thread_info  *info = lod_env_info(env);
-       struct dt_object        *next = dt_object_child(dt);
-       struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
-       struct lod_object       *lo = lod_dt_obj(dt);
-       struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
-       __u32   id;
-       int     rc, i, j, left;
+       struct dt_object *next = dt_object_child(dt);
+       struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
+       struct lod_object *lo = lod_dt_obj(dt);
+       struct lu_attr *attr = &lod_env_info(env)->lti_attr;
+       struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
+       __u32 magic, id, flags, neg_flags = 0;
+       int rc, i, j, left;
        ENTRY;
 
        LASSERT(lo->ldo_is_composite);
 
-       id = *(__u32 *)buf->lb_buf;
-       if (id == 0 || id == LCME_ID_NONE) {
-               CDEBUG(D_LAYOUT, "%s: invalid component id %#x\n",
-                      lod2obd(d)->obd_name, id);
+       rc = lod_verify_striping(d, buf, false, 0);
+       if (rc != 0)
+               RETURN(rc);
+
+       magic = comp_v1->lcm_magic;
+       if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
+               lustre_swab_lov_comp_md_v1(comp_v1);
+               magic = comp_v1->lcm_magic;
+       }
+
+       if (magic != LOV_USER_MAGIC_COMP_V1)
+               RETURN(-EINVAL);
+
+       id = comp_v1->lcm_entries[0].lcme_id;
+       flags = comp_v1->lcm_entries[0].lcme_flags;
+
+       if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
+               CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
+                      lod2obd(d)->obd_name, id, flags);
                RETURN(-EINVAL);
        }
 
+       if (id != LCME_ID_INVAL && flags != 0) {
+               CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
+                      lod2obd(d)->obd_name);
+               RETURN(-EINVAL);
+       }
+
+       if (flags & LCME_FL_NEG) {
+               neg_flags = flags & ~LCME_FL_NEG;
+               flags = 0;
+       }
+
        left = lo->ldo_comp_cnt;
        if (left <= 0)
                RETURN(-EINVAL);
@@ -2457,10 +2484,11 @@ static int lod_declare_layout_del(const struct lu_env *env,
 
                lod_comp = &lo->ldo_comp_entries[i];
 
-               if (id <= LCME_ID_MAX && id != lod_comp->llc_id)
+               if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
+                       continue;
+               else if (flags && !(flags & lod_comp->llc_flags))
                        continue;
-               else if (id > LCME_ID_MAX && id < LCME_ID_ALL &&
-                        !(id & lod_comp->llc_flags))
+               else if (neg_flags && (neg_flags & lod_comp->llc_flags))
                        continue;
 
                if (left != (i + 1)) {
index a86dc5b..939067d 100644 (file)
@@ -1501,10 +1501,10 @@ void test31(void)
        llapi_layout_free(layout);
 
        /* delete non-tail component will fail */
-       rc = llapi_layout_file_comp_del(path, id[0]);
+       rc = llapi_layout_file_comp_del(path, id[0], 0);
        ASSERTF(rc < 0 && errno == EINVAL, "rc %d, errno %d", rc, errno);
 
-       rc = llapi_layout_file_comp_del(path, id[1]);
+       rc = llapi_layout_file_comp_del(path, id[1], 0);
        ASSERTF(rc == 0, "rc %d, errno %d", rc, errno);
 
        /* verify the composite layout after deleting */
index cbd34dd..e49c7cb 100644 (file)
@@ -133,12 +133,12 @@ del_comp_and_verify() {
        local left=$3
        local size=$4
 
-       local opt="-I"
-       if [ $id == "init" ]; then
-               opt="--component-flags"
+       local opt="-I "
+       if [ $id == "init" -o $id == "^init" ]; then
+               opt="--component-flags="
        fi
 
-       $LFS setstripe --component-del $opt $id $comp_file ||
+       $LFS setstripe --component-del $opt$id $comp_file ||
                error "Delete component $id from $comp_file failed"
 
        local comp_cnt=$($LFS getstripe --component-count $comp_file)
@@ -176,11 +176,10 @@ test_3() {
        $LFS setstripe -E 1M -E 16M -E -1 $comp_file ||
                error "Create second $comp_file failed"
 
-       #instantiate all components, so that objs are allocted
-       dd if=/dev/zero of=$comp_file bs=1k count=1 seek=17k
-
+       del_comp_and_verify $comp_file "^init" 1 0
        del_comp_and_verify $comp_file "init" 0 0
        rm -f $comp_file || error "Delete second $comp_file failed"
+
 }
 run_test 3 "Delete component from existing file"
 
index c9b10e8..aa46f2d 100644 (file)
@@ -187,6 +187,7 @@ command_t cmdlist[] = {
         "                               <filename>\n"
         "\tcomp_id:     Unique component ID\n"
         "\tcomp_flags:  'init' indicating all instantiated components\n"
+        "\t             '^init' indicating all uninstantiated components\n"
         "\t-I and -F can't be specified at the same time\n"
         " or\n"
         "To add component(s) to an existing composite file:\n"
@@ -684,13 +685,12 @@ static int lfs_component_del(char *fname, __u32 comp_id, __u32 flags)
                        fprintf(stderr, "Invalid component flags %#x\n", flags);
                        return -EINVAL;
                }
-               comp_id = LCME_ID_NONE | flags;
        } else if (comp_id > LCME_ID_MAX) {
                fprintf(stderr, "Invalid component id %u\n", comp_id);
                return -EINVAL;
        }
 
-       rc = llapi_layout_file_comp_del(fname, comp_id);
+       rc = llapi_layout_file_comp_del(fname, comp_id, flags);
        if (rc)
                fprintf(stderr, "Delete component %#x from %s failed. %s\n",
                        comp_id, fname, strerror(errno));
@@ -1246,9 +1246,25 @@ static int adjust_first_extent(char *fname, struct llapi_layout *layout)
        return 0;
 }
 
+static inline bool comp_flags_is_neg(__u32 flags)
+{
+       return flags & LCME_FL_NEG;
+}
+
+static inline void comp_flags_set_neg(__u32 *flags)
+{
+       *flags |= LCME_FL_NEG;
+}
+
+static inline void comp_flags_clear_neg(__u32 *flags)
+{
+       *flags &= ~LCME_FL_NEG;
+}
+
 static int comp_name2flags(__u32 *flags, char *name)
 {
        char *ptr;
+       __u32 neg_flags = 0;
 
        if (name == NULL)
                return -EINVAL;
@@ -1260,10 +1276,24 @@ static int comp_name2flags(__u32 *flags, char *name)
                        break;
                if (strcmp(flg, "init") == 0)
                        *flags |= LCME_FL_INIT;
+               else if (strcmp(flg, "^init") == 0)
+                       neg_flags |= LCME_FL_INIT;
                else
                        return -EINVAL;
        }
-       return (*flags == 0) ? -EINVAL : 0;
+
+       if (*flags == 0 && neg_flags == 0)
+               return -EINVAL;
+       /* don't support mixed flags for now */
+       if (*flags && neg_flags)
+               return -EINVAL;
+
+       if (neg_flags) {
+               *flags = neg_flags;
+               comp_flags_set_neg(flags);
+       }
+
+       return 0;
 }
 
 enum {
index 0ab8565..015adf5 100644 (file)
@@ -1928,35 +1928,67 @@ out:
 }
 
 /**
- * Delete component(s) by the specified component id (accepting lcme_id
- * wildcards also) from an existing file.
+ * Delete component(s) by the specified component id or component flags
+ * from an existing file.
  *
  * \param[in] path     path name of the file
- * \param[in] id       unique component ID or an lcme_id
- *                     (LCME_ID_NONE | LCME_FL_* )
+ * \param[in] id       unique component ID
+ * \param[in] flags    flags: LCME_FL_* or;
+ *                     negative flags: (LCME_FL_NEG|LCME_FL_*)
  */
-int llapi_layout_file_comp_del(const char *path, uint32_t id)
+int llapi_layout_file_comp_del(const char *path, uint32_t id, uint32_t flags)
 {
-       int rc, fd;
+       int rc, fd, lum_size;
+       struct llapi_layout *layout;
+       struct llapi_layout_comp *comp;
+       struct lov_user_md *lum;
 
-       if (path == NULL || id == 0) {
+       if (path == NULL || id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
                errno = EINVAL;
                return -1;
        }
 
-       fd = open(path, O_RDWR);
-       if (fd < 0)
+       /* Can only specify ID or flags, not both. */
+       if (id != 0 && flags != 0) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       layout = llapi_layout_alloc();
+       if (layout == NULL)
+               return -1;
+
+       llapi_layout_comp_extent_set(layout, 0, LUSTRE_EOF);
+       comp = __llapi_layout_cur_comp(layout);
+       comp->llc_id = id;
+       comp->llc_flags = flags;
+
+       lum = llapi_layout_to_lum(layout);
+       if (lum == NULL) {
+               llapi_layout_free(layout);
                return -1;
+       }
+       lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
+
+       fd = open(path, O_RDWR);
+       if (fd < 0) {
+               rc = -1;
+               goto out;
+       }
 
-       rc = fsetxattr(fd, XATTR_LUSTRE_LOV".del", &id, sizeof(id), 0);
+       rc = fsetxattr(fd, XATTR_LUSTRE_LOV".del", lum, lum_size, 0);
        if (rc < 0) {
                int tmp_errno = errno;
                close(fd);
                errno = tmp_errno;
-               return -1;
+               rc = -1;
+               goto out;
        }
        close(fd);
-       return 0;
+out:
+       free(lum);
+       llapi_layout_free(layout);
+       return rc;
 }
 
 /**