Whamcloud - gitweb
LU-8066 sysfs: make ping sysfs file read and writable
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index 5f95d44..8f69ac6 100644 (file)
@@ -1344,26 +1344,36 @@ int llapi_search_rootpath(char *pathname, const char *fsname)
 
 int llapi_getname(const char *path, char *buf, size_t size)
 {
-        struct obd_uuid uuid_buf;
-        char *uuid = uuid_buf.uuid;
-        int rc, nr;
+       struct obd_uuid uuid_buf;
+       char *uuid = uuid_buf.uuid;
+       char *cfg_instance;
+       int rc, len, fsname_len;
 
-        memset(&uuid_buf, 0, sizeof(uuid_buf));
-        rc = llapi_file_get_lov_uuid(path, &uuid_buf);
-        if (rc)
-                return rc;
+       memset(&uuid_buf, 0, sizeof(uuid_buf));
+       rc = llapi_file_get_lov_uuid(path, &uuid_buf);
+       if (rc)
+               return rc;
 
-        /* We want to turn lustre-clilov-ffff88002738bc00 into
-         * lustre-ffff88002738bc00. */
+       /*
+        * We want to turn testfs-clilov-ffff88002738bc00 into
+        * testfs-ffff88002738bc00 in a portable way that doesn't depend
+        * on what is after "-clilov-" as it may change in the future.
+        * Unfortunately, the "fsname" part may contain a dash, so we
+        * can't just skip to the first dash, and the "instance" may be a
+        * UUID in the future, so we can't necessarily go to the last dash.
+        */
+       cfg_instance = strstr(uuid, "-clilov-");
+       if (!cfg_instance)
+               return -EINVAL;
 
-        nr = snprintf(buf, size, "%.*s-%s",
-                      (int) (strlen(uuid) - 24), uuid,
-                      uuid + strlen(uuid) - 16);
+       fsname_len = cfg_instance - uuid;
+       cfg_instance += strlen("-clilov-");
+       len = snprintf(buf, size, "%.*s-%s", fsname_len, uuid, cfg_instance);
 
-        if (nr >= size)
-                rc = -ENAMETOOLONG;
+       if (len >= size)
+               rc = -ENAMETOOLONG;
 
-        return rc;
+       return rc;
 }
 
 /**
@@ -2828,7 +2838,8 @@ void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
        }
 
        if (verbose & VERBOSE_HASH_TYPE) {
-               unsigned int type = lum->lum_hash_type;
+               unsigned int type = lum->lum_hash_type & LMV_HASH_TYPE_MASK;
+               unsigned int flags = lum->lum_hash_type & ~LMV_HASH_TYPE_MASK;
 
                llapi_printf(LLAPI_MSG_NORMAL, "%s", separator);
                if (verbose & ~VERBOSE_HASH_TYPE)
@@ -2837,9 +2848,18 @@ void lmv_dump_user_lmm(struct lmv_user_md *lum, char *pool_name,
                        llapi_printf(LLAPI_MSG_NORMAL, "%s",
                                     mdt_hash_name[type]);
                else
-                       llapi_printf(LLAPI_MSG_NORMAL, "%d",
-                                    (int)type);
+                       llapi_printf(LLAPI_MSG_NORMAL, "%#x", type);
+
+               if (flags & LMV_HASH_FLAG_MIGRATION)
+                       llapi_printf(LLAPI_MSG_NORMAL, ",migrating");
+               if (flags & LMV_HASH_FLAG_DEAD)
+                       llapi_printf(LLAPI_MSG_NORMAL, ",dead");
+               if (flags & LMV_HASH_FLAG_BAD_TYPE)
+                       llapi_printf(LLAPI_MSG_NORMAL, ",bad_type");
+               if (flags & LMV_HASH_FLAG_LOST_LMV)
+                       llapi_printf(LLAPI_MSG_NORMAL, ",lost_lmv");
                separator = "\n";
+
        }
 
        if (verbose & VERBOSE_OBJID && lum->lum_magic != LMV_USER_MAGIC) {
@@ -4801,26 +4821,32 @@ int llapi_obd_statfs(char *path, __u32 type, __u32 index,
 
 int llapi_ping(char *obd_type, char *obd_name)
 {
+       int flags = O_RDONLY;
+       char buf[1] = { 0 };
        glob_t path;
-       char buf[1];
        int rc, fd;
 
        rc = cfs_get_param_paths(&path, "%s/%s/ping",
                                obd_type, obd_name);
        if (rc != 0)
                return -errno;
-
-       fd = open(path.gl_pathv[0], O_WRONLY);
+retry_open:
+       fd = open(path.gl_pathv[0], flags);
        if (fd < 0) {
+               if (errno == EACCES && flags == O_RDONLY) {
+                       flags = O_WRONLY;
+                       goto retry_open;
+               }
                rc = -errno;
                llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s",
                            path.gl_pathv[0]);
                goto failed;
        }
 
-       /* The purpose is to send a byte as a ping, whatever this byte is. */
-       /* coverity[uninit_use_in_call] */
-       rc = write(fd, buf, 1);
+       if (flags == O_RDONLY)
+               rc = read(fd, buf, sizeof(buf));
+       else
+               rc = write(fd, buf, sizeof(buf));
        if (rc < 0)
                rc = -errno;
        close(fd);