Whamcloud - gitweb
LU-11264 llapi: clean up llapi_file_create_foreign() 93/35093/5
authorAndreas Dilger <adilger@whamcloud.com>
Fri, 7 Jun 2019 03:45:37 +0000 (21:45 -0600)
committerOleg Drokin <green@whamcloud.com>
Fri, 9 Aug 2019 04:39:34 +0000 (04:39 +0000)
Clean up the error handling in llapi_file_create_foreign() to always
set errno, and avoid printing a duplicate error message when run on
a non-Lustre filesystem.

Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Change-Id: I610748f5d07566fb81c2f0b6f59507e97e03158f
Reviewed-on: https://review.whamcloud.com/35093
Reviewed-by: Faccini Bruno <bruno.faccini@intel.com>
Reviewed-by: Olaf Faaland-LLNL <faaland1@llnl.gov>
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/utils/liblustreapi.c

index 4e04559..c25456d 100644 (file)
@@ -741,32 +741,35 @@ int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
        int fd, rc;
 
        if (foreign_lov == NULL) {
-               llapi_error(LLAPI_MSG_ERROR, -EINVAL,
+               rc = -EINVAL;
+               llapi_error(LLAPI_MSG_ERROR, rc,
                            "foreign LOV EA content must be provided");
-               return -EINVAL;
+               goto out_err;
        }
 
        len = strlen(foreign_lov);
        if (len > XATTR_SIZE_MAX - offsetof(struct lov_foreign_md, lfm_value) ||
            len <= 0) {
-               llapi_error(LLAPI_MSG_ERROR, -EINVAL,
+               rc = -EINVAL;
+               llapi_error(LLAPI_MSG_ERROR, rc,
                            "foreign LOV EA size %zu (must be 0 < len < %zu)",
                            len, XATTR_SIZE_MAX -
                            offsetof(struct lov_foreign_md, lfm_value));
-               return -EINVAL;
+               goto out_err;
        }
 
        lfm = malloc(len + offsetof(struct lov_foreign_md, lfm_value));
        if (lfm == NULL) {
-               llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
+               rc = -ENOMEM;
+               llapi_error(LLAPI_MSG_ERROR, rc,
                            "failed to allocate lov_foreign_md");
-               return -ENOMEM;
+               goto out_err;
        }
 
        fd = open(name, O_WRONLY|O_CREAT|O_LOV_DELAY_CREATE, mode);
        if (fd == -1) {
-               perror("open()");
-               rc = -errno;
+               fd = -errno;
+               llapi_error(LLAPI_MSG_ERROR, fd, "open '%s' failed", name);
                goto out_free;
        }
 
@@ -778,21 +781,18 @@ int llapi_file_create_foreign(const char *name, mode_t mode, __u32 type,
 
        if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, lfm) != 0) {
                char *errmsg = "stripe already set";
-               char fsname[MAX_OBD_NAME + 1] = { 0 };
 
                rc = -errno;
-               if (errno != EEXIST && errno != EALREADY)
+               if (errno == ENOTTY)
+                       errmsg = "not on a Lustre filesystem";
+               else if (errno == EEXIST || errno == EALREADY)
+                       errmsg = "stripe already set";
+               else
                        errmsg = strerror(errno);
 
                llapi_err_noerrno(LLAPI_MSG_ERROR,
                                  "setstripe error for '%s': %s", name, errmsg);
 
-               /* Make sure we are on a Lustre file system */
-               if (rc == -ENOTTY && llapi_search_fsname(name, fsname))
-                       llapi_error(LLAPI_MSG_ERROR, rc,
-                                   "'%s' is not on a Lustre filesystem",
-                                   name);
-
                close(fd);
                fd = rc;
        }
@@ -801,6 +801,10 @@ out_free:
        free(lfm);
 
        return fd;
+
+out_err:
+       errno = -rc;
+       return rc;
 }
 
 int llapi_file_create(const char *name, unsigned long long stripe_size,