Whamcloud - gitweb
LU-541 Fix getstripe interop with 2.0 server
authorBobi Jam <bobijam@whamcloud.com>
Thu, 28 Jul 2011 09:28:11 +0000 (17:28 +0800)
committerJohann Lombardi <johann@whamcloud.com>
Mon, 29 Aug 2011 11:14:20 +0000 (07:14 -0400)
* 2.0 server has changed dir getstripe rule as follows:
          If the directory in question DOES NOT have it's EA set, AND it
          IS NOT the root directory of the filesystem, then no EA values
          are returned to userspace. Rather, an error is returned from
          the ioctl and errno is set to ENODATA.
  1.8 client needs adapt to this rule
* fix liblustreapi.c usage of errno and rc, llapi_xxx() functions should
  return negative value on error.

Change-Id: If81a456209d5985aae7f20569eba1ac45ef576b1
Signed-off-by: Bobi Jam <bobijam@whamcloud.com>
Reviewed-on: http://review.whamcloud.com/1154
Tested-by: Hudson
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Johann Lombardi <johann@whamcloud.com>
Tested-by: Maloo <whamcloud.maloo@gmail.com>
lustre/include/lustre/liblustreapi.h
lustre/tests/ll_dirstripe_verify.c
lustre/utils/lfs.c
lustre/utils/liblustreapi.c

index 17c6a00..7830bcb 100644 (file)
@@ -30,6 +30,9 @@
  * Use is subject to license terms.
  */
 /*
+ * Copyright (c) 2011 Whamcloud, Inc.
+ */
+/*
  * This file is part of Lustre, http://www.lustre.org/
  * Lustre is a trademark of Sun Microsystems, Inc.
  */
@@ -61,11 +64,14 @@ enum llapi_message_level {
 
 /* the bottom three bits reserved for llapi_message_level */
 #define LLAPI_MSG_MASK          0x00000007
-#define LLAPI_MSG_NO_ERRNO      0x00000010 
+#define LLAPI_MSG_NO_ERRNO      0x00000010
 
 /* liblustreapi.c */
 extern void llapi_msg_set_level(int level);
-extern void llapi_err(int level, char *fmt, ...);
+extern void llapi_error(int level, int rc, char *fmt, ...);
+#define llapi_err(level, fmt, a...) llpai_error(level, -errno, fmt, ##a)
+#define llapi_err_noerrno(level, fmt, a...)                             \
+       llapi_error((level) | LLAPI_MSG_NO_ERRNO, 0, fmt, ## a)
 extern void llapi_printf(int level, char *fmt, ...);
 extern int llapi_file_create(const char *name, unsigned long long stripe_size,
                              int stripe_offset, int stripe_count,
@@ -88,10 +94,10 @@ extern int llapi_file_lookup(int dirfd, const char *name);
 
 #define VERBOSE_COUNT   0x1
 #define VERBOSE_SIZE    0x2
-#define VERBOSE_OFFSET  0x4 
+#define VERBOSE_OFFSET  0x4
 #define VERBOSE_POOL    0x8
 #define VERBOSE_OBJID   0x10
-#define VERBOSE_DETAIL  0x20 
+#define VERBOSE_DETAIL  0x20
 #define VERBOSE_ALL     (VERBOSE_COUNT | VERBOSE_SIZE | VERBOSE_OFFSET | \
                          VERBOSE_POOL | VERBOSE_OBJID)
 
@@ -169,7 +175,7 @@ extern int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_uuid);
 extern int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count);
 extern int llapi_is_lustre_mnttype(const char *type);
 extern int llapi_search_ost(char *fsname, char *poolname, char *ostname);
-extern int llapi_search_mounts(const char *pathname, int index, 
+extern int llapi_search_mounts(const char *pathname, int index,
                                char *mntdir, char *fsname);
 extern int llapi_search_fsname(const char *pathname, char *fsname);
 extern int parse_size(char *optarg, unsigned long long *size,
index 77c5512..5110010 100644 (file)
@@ -30,6 +30,9 @@
  * Use is subject to license terms.
  */
 /*
+ * Copyright (c) 2011 Whamcloud, Inc.
+ */
+/*
  * This file is part of Lustre, http://www.lustre.org/
  * Lustre is a trademark of Sun Microsystems, Inc.
  *
@@ -80,18 +83,19 @@ int read_proc_entry(char *proc_path, char *buf, int len)
 
         fd = open(proc_path, O_RDONLY);
         if (fd == -1) {
-                llapi_err(LLAPI_MSG_ERROR, "open('%s') failed: %s\n",
-                          proc_path);
+                llapi_error(LLAPI_MSG_ERROR, -errno, "open('%s') failed: %s\n",
+                            proc_path);
                 return -2;
         }
 
         rc = read(fd, buf, len - 1);
         if (rc < 0) {
-                llapi_err(LLAPI_MSG_ERROR, "read('%s') failed: %s\n",proc_path);
+                llapi_error(LLAPI_MSG_ERROR, -errno,
+                            "read('%s') failed: %s\n", proc_path);
                 rc = -3;
         } else if (rc == 0) {
-                llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
-                          "read('%s') zero bytes\n", proc_path);
+                llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                  "read('%s') zero bytes\n", proc_path);
                 rc = -4;
         } else if (/* rc > 0 && */ buf[rc - 1] == '\n') {
                 buf[rc - 1] = '\0'; /* Remove trailing newline */
@@ -116,12 +120,13 @@ int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
 
         fp = popen("\\ls -d  /proc/fs/lustre/lov/*lov* | head -1", "r");
         if (!fp) {
-                llapi_err(LLAPI_MSG_ERROR,
-                          "open(lustre/lov/*lov*) failed: %s\n");
+                llapi_error(LLAPI_MSG_ERROR, -errno,
+                            "open(lustre/lov/*lov*) failed: %s\n");
                 return 2;
         }
         if (fscanf(fp, "%s", lov_path) < 1) {
-                llapi_err(LLAPI_MSG_ERROR,"read lustre/lov/*lov* failed: %s\n");
+                llapi_error(LLAPI_MSG_ERROR, -EINVAL,
+                            "read lustre/lov/*lov* failed: %s\n");
                 pclose(fp);
                 return 3;
         }
@@ -160,15 +165,17 @@ int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
 
         if (lum_file1->lmm_stripe_count != stripe_count ||
             lum_file1->lmm_stripe_count < min_stripe_count) {
-                llapi_err(LLAPI_MSG_ERROR, "file1 stripe count %d != dir %d\n",
-                          lum_file1->lmm_stripe_count, stripe_count);
+                llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                  "file1 stripe count %d != dir %d\n",
+                                  lum_file1->lmm_stripe_count, stripe_count);
                 return 7;
         }
 
         if (lum_file1->lmm_stripe_count < stripe_count)
-                llapi_err(LLAPI_MSG_WARN, "warning: file1 used fewer stripes "
-                          "%d < dir %d (likely due to bug 4900)\n",
-                          lum_file1->lmm_stripe_count, stripe_count);
+                llapi_err_noerrno(LLAPI_MSG_WARN,
+                                  "warning: file1 used fewer stripes "
+                                  "%d < dir %d (likely due to bug 4900)\n",
+                                  lum_file1->lmm_stripe_count, stripe_count);
 
         if (lum_dir != NULL)
                 stripe_size = (int)lum_dir->lmm_stripe_size;
@@ -182,8 +189,9 @@ int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
         }
 
         if (lum_file1->lmm_stripe_size != stripe_size) {
-                llapi_err(LLAPI_MSG_ERROR, "file1 stripe size %d != dir %d\n",
-                          lum_file1->lmm_stripe_size, stripe_size);
+                llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                  "file1 stripe size %d != dir %d\n",
+                                  lum_file1->lmm_stripe_size, stripe_size);
                 return 8;
         }
 
@@ -193,7 +201,7 @@ int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
                 for (i = 0; i < stripe_count; i++)
                         if (lum_file1->lmm_objects[i].l_ost_idx !=
                             (stripe_offset + i) % ost_count) {
-                                llapi_err(LLAPI_MSG_WARN,
+                                llapi_err_noerrno(LLAPI_MSG_WARN,
                                           "warning: file1 non-sequential "
                                           "stripe[%d] %d != %d\n", i,
                                           lum_file1->lmm_objects[i].l_ost_idx,
@@ -205,7 +213,8 @@ int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
                        ost_count;
                 idx = lum_file2->lmm_objects[0].l_ost_idx;
                 if (idx != next) {
-                        llapi_err(LLAPI_MSG_WARN, "warning: non-sequential "
+                        llapi_err_noerrno(LLAPI_MSG_WARN,
+                                  "warning: non-sequential "
                                   "file1 stripe[%d] %d != file2 stripe[0] %d\n",
                                   stripe,
                                   lum_file1->lmm_objects[stripe].l_ost_idx,
@@ -224,37 +233,37 @@ int main(int argc, char **argv)
         int lum_size;
 
         if (argc < 3) {
-                llapi_err(LLAPI_MSG_ERROR,
-                          "Usage: %s <dirname> <filename1> [filename2]\n",
-                          argv[0]);
+                llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                "Usage: %s <dirname> <filename1> [filename2]\n",
+                                argv[0]);
                 return 1;
         }
 
         dir = opendir(argv[1]);
         if (dir == NULL) {
-                rc = errno;
-                llapi_err(LLAPI_MSG_ERROR,
-                          "error: %s opendir failed\n", argv[1]);
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "error: %s opendir failed\n", argv[1]);
                 return rc;
         }
 
         lum_size = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC);
         if ((lum_dir = (struct lov_user_md *)malloc(lum_size)) == NULL) {
-                rc = ENOMEM;
-                llapi_err(LLAPI_MSG_ERROR, "error: can't allocate %d bytes "
-                          "for dir EA", lum_size);
+                rc = -ENOMEM;
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "error: can't allocate %d bytes "
+                            "for dir EA", lum_size);
                 goto cleanup;
         }
 
         rc = llapi_file_get_stripe(argv[1], lum_dir);
         if (rc) {
-                if (errno == ENODATA) {
+                if (rc == -ENODATA) {
                         free(lum_dir);
                         lum_dir = NULL;
                 } else {
-                        rc = errno;
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "error: can't get EA for %s\n", argv[1]);
+                        llapi_error(LLAPI_MSG_ERROR, rc,
+                                    "error: can't get EA for %s\n", argv[1]);
                         goto cleanup;
                 }
         }
@@ -262,41 +271,41 @@ int main(int argc, char **argv)
         /* XXX should be llapi_lov_getname() */
         rc = llapi_file_get_lov_uuid(argv[1], &lov_uuid);
         if (rc) {
-                rc = errno;
-                llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name for %s\n",
-                          argv[1]);
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "error: can't get lov name for %s\n",
+                            argv[1]);
                 return rc;
         }
 
         if ((lum_file1 = (struct lov_user_md *)malloc(lum_size)) == NULL) {
-                rc = ENOMEM;
-                llapi_err(LLAPI_MSG_ERROR,
-                          "error: can't allocate %d bytes for EA\n", lum_size);
+                rc = -ENOMEM;
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "error: can't allocate %d bytes for EA\n",
+                            lum_size);
                 goto cleanup;
         }
 
         rc = llapi_file_get_stripe(argv[2], lum_file1);
         if (rc) {
-                rc = errno;
-                llapi_err(LLAPI_MSG_ERROR,
-                          "error: unable to get EA for %s\n", argv[2]);
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "error: unable to get EA for %s\n", argv[2]);
                 goto cleanup;
         }
 
         if (argc == 4) {
                 lum_file2 = (struct lov_user_md *)malloc(lum_size);
                 if (lum_file2 == NULL) {
-                        rc = ENOMEM;
-                        llapi_err(LLAPI_MSG_ERROR, "error: can't allocate %d "
-                                  "bytes for file2 EA\n", lum_size);
+                        rc = -ENOMEM;
+                        llapi_error(LLAPI_MSG_ERROR, rc,
+                                    "error: can't allocate %d "
+                                    "bytes for file2 EA\n", lum_size);
                         goto cleanup;
                 }
 
                 rc = llapi_file_get_stripe(argv[3], lum_file2);
                 if (rc) {
-                        rc = errno;
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "error: can't get EA for %s\n", argv[3]);
+                        llapi_error(LLAPI_MSG_ERROR, rc,
+                                    "error: can't get EA for %s\n", argv[3]);
                         goto cleanup;
                 }
         }
index 700f09b..0c903b4 100644 (file)
@@ -1331,14 +1331,14 @@ static int lfs_quotacheck(int argc, char **argv)
                                             * take this parameter into account */
                 rc = llapi_quotactl(mnt, &qctl);
                 if (rc) {
-                        fprintf(stderr, "quota off failed: %s\n", strerror(errno));
+                        fprintf(stderr, "quota off failed: %s\n", strerror(-rc));
                         return rc;
                 }
         }
 
         rc = llapi_quotacheck(mnt, check_type);
         if (rc) {
-                fprintf(stderr, "quotacheck failed: %s\n", strerror(errno));
+                fprintf(stderr, "quotacheck failed: %s\n", strerror(-rc));
                 return rc;
         }
 
@@ -1348,7 +1348,7 @@ static int lfs_quotacheck(int argc, char **argv)
                 if (*obd_type)
                         fprintf(stderr, "%s %s ", obd_type,
                                 obd_uuid2str(&qchk.obd_uuid));
-                fprintf(stderr, "quota check failed: %s\n", strerror(errno));
+                fprintf(stderr, "quota check failed: %s\n", strerror(-rc));
                 return rc;
         }
 
@@ -1360,7 +1360,7 @@ static int lfs_quotacheck(int argc, char **argv)
                                             * take this parameter into account */
         rc = llapi_quotactl(mnt, &qctl);
         if (rc) {
-                if (v2 > 0 && errno == EALREADY) {
+                if (v2 > 0 && rc == -EALREADY) {
                         /* This is for 2.0 server. */
                         rc = 0;
                 } else {
@@ -1368,7 +1368,7 @@ static int lfs_quotacheck(int argc, char **argv)
                                 fprintf(stderr, "%s %s ", (char *)qctl.obd_type,
                                         obd_uuid2str(&qctl.obd_uuid));
                         fprintf(stderr, "%s turn on quota failed: %s\n",
-                                argv[0], strerror(errno));
+                                argv[0], strerror(-rc));
                 }
         }
 
@@ -1425,10 +1425,10 @@ static int lfs_quotaon(int argc, char **argv)
 
         rc = llapi_quotactl(mnt, &qctl);
         if (rc) {
-                if (v2 > 0 && errno == EALREADY) {
+                if (v2 > 0 && rc == -EALREADY) {
                         /* This is for 2.0 server. */
                         rc = 0;
-                } else if (errno == ENOENT) {
+                } else if (rc == -ENOENT) {
                         fprintf(stderr, "error: cannot find quota database, "
                                         "make sure you have run quotacheck\n");
                 } else {
@@ -1436,7 +1436,7 @@ static int lfs_quotaon(int argc, char **argv)
                                 fprintf(stderr, "%s %s ", obd_type,
                                         obd_uuid2str(&qctl.obd_uuid));
                         fprintf(stderr, "%s failed: %s\n", argv[0],
-                                strerror(errno));
+                                strerror(-rc));
                 }
         }
 
@@ -1486,15 +1486,15 @@ static int lfs_quotaoff(int argc, char **argv)
 
         rc = llapi_quotactl(mnt, &qctl);
         if (rc) {
-                if ((v2 > 0 && errno == EALREADY) ||
-                    (v2 == 0 && errno == ESRCH)) {
+                if ((v2 > 0 && rc == -EALREADY) ||
+                    (v2 == 0 && rc == -ESRCH)) {
                         rc = 0;
                 } else {
                         if (*obd_type)
                                 fprintf(stderr, "%s %s ", obd_type,
                                         obd_uuid2str(&qctl.obd_uuid));
                         fprintf(stderr, "quotaoff failed: %s\n",
-                                strerror(errno));
+                                strerror(-rc));
                 }
         }
 
@@ -1542,7 +1542,7 @@ static int lfs_quotainv(int argc, char **argv)
 
         rc = llapi_quotactl(mnt, &qctl);
         if (rc) {
-                fprintf(stderr, "quotainv failed: %s\n", strerror(errno));
+                fprintf(stderr, "quotainv failed: %s\n", strerror(-rc));
                 return rc;
         }
 
@@ -1741,7 +1741,7 @@ quotactl:
                 if (*obd_type)
                         fprintf(stderr, "%s %s ", obd_type,
                                 obd_uuid2str(&qctl.obd_uuid));
-                fprintf(stderr, "setquota failed: %s\n", strerror(errno));
+                fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
                 return rc;
         }
 
@@ -1883,7 +1883,7 @@ int lfs_setquota(int argc, char **argv)
                 if (rc < 0) {
                         fprintf(stderr, "error: setquota failed while retrieving"
                                         " current quota settings (%s)\n",
-                                        strerror(errno));
+                                        strerror(-rc));
                         return rc;
                 }
 
@@ -1919,7 +1919,7 @@ quotactl:
                 if (*obd_type)
                         fprintf(stderr, "%s %s ", obd_type,
                                 obd_uuid2str(&qctl.obd_uuid));
-                fprintf(stderr, "setquota failed: %s\n", strerror(errno));
+                fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
                 return rc;
         }
 
@@ -2094,7 +2094,7 @@ static int print_mds_quota(char *mnt, struct if_quotactl *qctl)
         qctl->qc_dqblk.dqb_valid = 1;
         rc = llapi_quotactl(mnt, qctl);
         if (rc) {
-                fprintf(stderr, "quotactl failed: %s\n", strerror(errno));
+                fprintf(stderr, "quotactl failed: %s\n", strerror(-rc));
                 return rc;
         }
 
@@ -2148,7 +2148,7 @@ retry_get_uuids:
                         if (!rc1)
                                 rc1 = rc;
                         fprintf(stderr, "%s quotactl failed: %s\n",
-                                uuidp->uuid, strerror(errno));
+                                uuidp->uuid, strerror(-rc));
                         continue;
                 }
 
@@ -2254,21 +2254,21 @@ ug_output:
         mnt = argv[optind];
 
         rc1 = llapi_quotactl(mnt, &qctl);
-        if (rc1 == -1) {
-                switch (errno) {
-                case EPERM:
+        if (rc1 < 0) {
+                switch (rc1) {
+                case -EPERM:
                         fprintf(stderr, "Permission denied.\n");
-                case ENOENT:
+                case -ENOENT:
                         /* We already got a "No such file..." message. */
                         goto out;
-                case ESRCH: {
+                case -ESRCH: {
                         fprintf(stderr, "%s quotas are not enabled.\n",
                                 qctl.qc_type == USRQUOTA?"user":"group");
                         goto out;
                 }
                 default:
                         fprintf(stderr, "Unexpected quotactl error: %s\n",
-                                strerror(errno));
+                                strerror(-rc1));
                 }
         }
 
index 7ada71b..f27f4c3 100644 (file)
@@ -30,6 +30,9 @@
  * Use is subject to license terms.
  */
 /*
+ * Copyright (c) 2011 Whamcloud, Inc.
+ */
+/*
  * This file is part of Lustre, http://www.lustre.org/
  * Lustre is a trademark of Sun Microsystems, Inc.
  *
@@ -132,10 +135,13 @@ void llapi_msg_set_level(int level)
                 llapi_msg_level = level;
 }
 
-void llapi_err(int level, char *fmt, ...)
+/* llapi_error will preserve errno */
+void llapi_error(int level, int _rc, char *fmt, ...)
 {
         va_list args;
-        int tmp_errno = abs(errno);
+        int tmp_errno = errno;
+        /* to protect using errno as _rc argument */
+        int rc = abs(_rc);
 
         if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
                 return;
@@ -147,15 +153,15 @@ void llapi_err(int level, char *fmt, ...)
         if (level & LLAPI_MSG_NO_ERRNO)
                 fprintf(stderr, "\n");
         else
-                fprintf(stderr, ": %s (%d)\n", strerror(tmp_errno), tmp_errno);
+                fprintf(stderr, ": %s (%d)\n", strerror(rc), rc);
+        errno = tmp_errno;
 }
 
-#define llapi_err_noerrno(level, fmt, a...)                             \
-        llapi_err((level) | LLAPI_MSG_NO_ERRNO, fmt, ## a)
-
+/* llapi_printf will preserve errno */
 void llapi_printf(int level, char *fmt, ...)
 {
         va_list args;
+        int tmp_errno = errno;
 
         if ((level & LLAPI_MSG_MASK) > llapi_msg_level)
                 return;
@@ -163,6 +169,7 @@ void llapi_printf(int level, char *fmt, ...)
         va_start(args, fmt);
         vfprintf(stdout, fmt, args);
         va_end(args);
+        errno = tmp_errno;
 }
 
 /* size_units is unchanged if no specifier used */
@@ -223,7 +230,7 @@ int parse_size(char *optarg, unsigned long long *size,
 int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
                              int stripe_count, int stripe_pattern)
 {
-        int page_size;
+        int page_size, rc;
 
         /* 64 KB is the largest common page size I'm aware of (on ia64), but
          * check the local page size just in case. */
@@ -236,29 +243,30 @@ int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
                                   LOV_MIN_STRIPE_SIZE);
         }
         if (stripe_size < 0 || (stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
-                errno = -EINVAL;
-                llapi_err(LLAPI_MSG_ERROR, "error: bad stripe_size %lu, "
-                          "must be an even multiple of %d bytes",
-                          stripe_size, page_size);
-                return errno;
+                rc = -EINVAL;
+                llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %lu, "
+                            "must be an even multiple of %d bytes",
+                            stripe_size, page_size);
+                return rc;
         }
         if (stripe_offset < -1 || stripe_offset > MAX_OBD_DEVICES) {
-                errno = -EINVAL;
-                llapi_err(LLAPI_MSG_ERROR, "error: bad stripe offset %d",
-                          stripe_offset);
-                return errno;
+                rc = -EINVAL;
+                llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe offset %d",
+                            stripe_offset);
+                return rc;
         }
         if (stripe_count < -1 || stripe_count > LOV_MAX_STRIPE_COUNT) {
-                errno = -EINVAL;
-                llapi_err(LLAPI_MSG_ERROR, "error: bad stripe count %d",
-                          stripe_count);
-                return errno;
+                rc = -EINVAL;
+                llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe count %d",
+                            stripe_count);
+                return rc;
         }
         if (stripe_size >= (1ULL << 32)) {
-                errno = -EINVAL;
-                llapi_err(LLAPI_MSG_ERROR, "warning: stripe size larger than 4G"
-                          " is not currently supported and would wrap");
-                return errno;
+                rc = -EINVAL;
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "warning: stripe size larger than 4G "
+                            "is not currently supported and would wrap");
+                return rc;
         }
         return 0;
 }
@@ -329,7 +337,7 @@ int llapi_search_ost(char *fsname, char *poolname, char *ostname)
                 return rc;
 
         if ((fd = fopen(buffer, "r")) == NULL)
-                return -EINVAL;
+                return -errno;
 
         while (fgets(buffer, sizeof(buffer), fd) != NULL) {
                 if (poolname == NULL) {
@@ -368,10 +376,12 @@ int llapi_file_open_pool(const char *name, int flags, int mode,
         if (pool_name != NULL) {
                 char fsname[MAX_OBD_NAME + 1], *ptr;
 
-                if (llapi_search_fsname(name, fsname)) {
-                    llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
-                              "'%s' is not on a Lustre filesystem", name);
-                    return -EINVAL;
+                rc = llapi_search_fsname(name, fsname);
+                if (rc) {
+                        llapi_error(LLAPI_MSG_ERROR, rc,
+                                    "'%s' is not on a Lustre filesystem",
+                                    name);
+                        return rc;
                 }
 
                 /* in case user gives the full pool name <fsname>.<poolname>,
@@ -381,19 +391,21 @@ int llapi_file_open_pool(const char *name, int flags, int mode,
                         *ptr = '\0';
                         if (strcmp(pool_name, fsname) != 0) {
                                 *ptr = '.';
-                                llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
-                                   "Pool '%s' is not on filesystem '%s'",
-                                   pool_name, fsname);
+                                llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                          "Pool '%s' is not on filesystem '%s'",
+                                          pool_name, fsname);
                                 return -EINVAL;
                         }
                         pool_name = ptr + 1;
                 }
 
                 /* Make sure the pool exists and is non-empty */
-                if ((rc = llapi_search_ost(fsname, pool_name, NULL)) < 1) {
-                        llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
-                                  "pool '%s.%s' %s", fsname, pool_name,
-                                  rc == 0 ? "has no OSTs" : "does not exist");
+                rc = llapi_search_ost(fsname, pool_name, NULL);
+                if (rc < 1) {
+                        llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                          "pool '%s.%s' %s", fsname, pool_name,
+                                          rc == 0 ? "has no OSTs" :
+                                                    "does not exist");
                         return -EINVAL;
                 }
         }
@@ -406,15 +418,14 @@ int llapi_file_open_pool(const char *name, int flags, int mode,
 
         if (fd < 0) {
                 rc = -errno;
-                llapi_err(LLAPI_MSG_ERROR, "unable to open '%s'", name);
+                llapi_error(LLAPI_MSG_ERROR, rc, "unable to open '%s'", name);
                 return rc;
         }
 
-        if ((rc = llapi_stripe_limit_check(stripe_size, stripe_offset,
-                                           stripe_count, stripe_pattern)) != 0){
-                errno = rc;
+        rc = llapi_stripe_limit_check(stripe_size, stripe_offset, stripe_count,
+                                      stripe_pattern);
+        if (rc != 0)
                 goto out;
-        }
 
         /*  Initialize IOCTL striping pattern structure */
         lum.lmm_magic = LOV_USER_MAGIC_V3;
@@ -510,10 +521,10 @@ static int get_root_path(int want, char *fsname, int *outfd, char *path,
         /* get the mount point */
         fp = setmntent(MOUNTED, "r");
         if (fp == NULL) {
-                 llapi_err(LLAPI_MSG_ERROR,
-                           "setmntent(%s) failed: %s:", MOUNTED,
-                           strerror (errno));
-                 return -EIO;
+                rc = -EIO;
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "setmntent(%s) failed", MOUNTED);
+                return rc;
         }
         while (1) {
                 if (getmntent_r(fp, &mnt, buf, sizeof(buf)) == NULL)
@@ -564,16 +575,17 @@ static int get_root_path(int want, char *fsname, int *outfd, char *path,
                 if (want & WANT_FD) {
                         fd = open(mntdir, O_RDONLY | O_DIRECTORY | O_NONBLOCK);
                         if (fd < 0) {
-                                perror("open");
                                 rc = -errno;
+                                llapi_error(LLAPI_MSG_ERROR, rc,
+                                            "error opening '%s'\n", mntdir);
                         } else {
                                 *outfd = fd;
                         }
                 }
         } else if (want & WANT_ERROR)
-                llapi_err(LLAPI_MSG_ERROR | LLAPI_MSG_NO_ERRNO,
-                          "can't find fs root for '%s': %d",
-                          (want & WANT_PATH) ? fsname : path, rc);
+                llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                  "can't find fs root for '%s': %d",
+                                  (want & WANT_PATH) ? fsname : path, rc);
         return rc;
 }
 
@@ -633,10 +645,11 @@ int llapi_search_fsname(const char *pathname, char *fsname)
                         *ptr = '\0';
                         path = realpath(buf, NULL);
                         if (path == NULL) {
-                                llapi_err(LLAPI_MSG_ERROR,
-                                          "pathname '%s' cannot expand",
-                                          pathname);
-                                return -errno;
+                                rc = -errno;
+                                llapi_error(LLAPI_MSG_ERROR, rc,
+                                            "pathname '%s' cannot expand",
+                                            pathname);
+                                return rc;
                         }
                 }
         }
@@ -731,17 +744,18 @@ int llapi_get_poolmembers(const char *poolname, char **members,
 
         rc = poolpath(fsname, NULL, pathname);
         if (rc != 0) {
-                errno = -rc;
-                llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
-                          fsname);
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "Lustre filesystem '%s' not found",
+                            fsname);
                 return rc;
         }
 
         llapi_printf(LLAPI_MSG_NORMAL, "Pool: %s.%s\n", fsname, pool);
         sprintf(path, "%s/%s", pathname, pool);
         if ((fd = fopen(path, "r")) == NULL) {
-                llapi_err(LLAPI_MSG_ERROR, "Cannot open %s", path);
-                return -EINVAL;
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "Cannot open %s", path);
+                return rc;
         }
 
         rc = 0;
@@ -803,17 +817,18 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size,
                 /* only absolute pathname is supported */
                 if (*name != '/')
                         return -EINVAL;
+
                 if (!realpath(name, rname)) {
                         rc = -errno;
-                        llapi_err(LLAPI_MSG_ERROR, "invalid path '%s'", name);
+                        llapi_error(LLAPI_MSG_ERROR, rc, "invalid path '%s'",
+                                    name);
                         return rc;
                 }
 
                 rc = poolpath(NULL, rname, pathname);
                 if (rc != 0) {
-                        errno = -rc;
-                        llapi_err(LLAPI_MSG_ERROR, "'%s' is not"
-                                  " a Lustre filesystem", name);
+                        llapi_error(LLAPI_MSG_ERROR, rc, "'%s' is not"
+                                    " a Lustre filesystem", name);
                         return rc;
                 }
                 strcpy(fsname, rname);
@@ -823,29 +838,32 @@ int llapi_get_poollist(const char *name, char **poollist, int list_size,
                 rc = poolpath(fsname, NULL, pathname);
         }
         if (rc != 0) {
-                errno = -rc;
-                llapi_err(LLAPI_MSG_ERROR, "Lustre filesystem '%s' not found",
-                          name);
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "Lustre filesystem '%s' not found", name);
                 return rc;
         }
 
         llapi_printf(LLAPI_MSG_NORMAL, "Pools from %s:\n", fsname);
         if ((dir = opendir(pathname)) == NULL) {
-                llapi_err(LLAPI_MSG_ERROR, "Could not open pool list for '%s'",
-                          name);
-                return -errno;
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "Could not open pool list for '%s'",
+                            name);
+                return rc;
         }
 
         while(1) {
                 rc = readdir_r(dir, &pool, &cookie);
 
                 if (rc != 0) {
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "Error reading pool list for '%s'", name);
-                        return -errno;
-                } else if ((rc == 0) && (cookie == NULL))
+                        rc = -errno;
+                        llapi_error(LLAPI_MSG_ERROR, rc,
+                                    "Error reading pool list for '%s'", name);
+                        return rc;
+                } else if ((rc == 0) && (cookie == NULL)) {
                         /* end of directory */
                         break;
+                }
 
                 /* ignore . and .. */
                 if (!strcmp(pool.d_name, ".") || !strcmp(pool.d_name, ".."))
@@ -910,10 +928,11 @@ typedef int (semantic_func_t)(char *path, DIR *parent, DIR *d,
 static int common_param_init(struct find_param *param)
 {
         param->lumlen = lov_mds_md_size(MAX_LOV_UUID_COUNT, LOV_MAGIC_V3);
-        if ((param->lmd = malloc(sizeof(lstat_t) + param->lumlen)) == NULL) {
-                llapi_err(LLAPI_MSG_ERROR,
-                          "error: allocation of %d bytes for ioctl",
-                          sizeof(lstat_t) + param->lumlen);
+        param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
+        if (param->lmd == NULL) {
+                llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
+                            "error: allocation of %d bytes for ioctl",
+                            sizeof(lstat_t) + param->lumlen);
                 return -ENOMEM;
         }
 
@@ -940,6 +959,7 @@ static int cb_common_fini(char *path, DIR *parent, DIR *d, void *data,
         return 0;
 }
 
+/* set errno upon failure */
 static DIR *opendir_parent(char *path)
 {
         DIR *parent;
@@ -979,20 +999,23 @@ int llapi_mds_getfileinfo(char *path, DIR *parent,
                          * Do the regular lstat(2) instead. */
                         ret = lstat_f(path, st);
                         if (ret) {
-                                llapi_err(LLAPI_MSG_ERROR,
-                                          "error: %s: lstat failed for %s",
-                                          __func__, path);
+                                ret = -errno;
+                                llapi_error(LLAPI_MSG_ERROR, ret,
+                                            "error: %s: lstat failed for %s",
+                                            __func__, path);
                                 return ret;
                         }
                 } else if (errno == ENOENT) {
-                        llapi_err(LLAPI_MSG_WARN,
-                                  "warning: %s: %s does not exist",
-                                  __func__, path);
-                        return -ENOENT;
+                        ret = -errno;
+                        llapi_error(LLAPI_MSG_WARN, ret,
+                                    "warning: %s: %s does not exist",
+                                    __func__, path);
+                        return ret;
                 } else {
-                        llapi_err(LLAPI_MSG_ERROR,
-                                 "error: %s: IOC_MDC_GETFILEINFO failed for %s",
-                                 __func__, path);
+                        ret = -errno;
+                        llapi_error(LLAPI_MSG_ERROR, ret,
+                                   "error: %s: IOC_MDC_GETFILEINFO failed for %s",
+                                   __func__, path);
                         return ret;
                 }
         }
@@ -1014,14 +1037,15 @@ static int llapi_semantic_traverse(char *path, int size, DIR *parent,
 
         d = opendir(path);
         if (!d && errno != ENOTDIR) {
-                llapi_err(LLAPI_MSG_ERROR, "%s: Failed to open '%s'",
-                          __func__, path);
-                return -EINVAL;
+                ret = -errno;
+                llapi_error(LLAPI_MSG_ERROR, ret, "%s: Failed to open '%s'",
+                            __func__, path);
+                return ret;
         } else if (!d && !parent) {
                 /* ENOTDIR. Open the parent dir. */
                 p = opendir_parent(path);
                 if (!p)
-                        GOTO(out, ret = -EINVAL);
+                        GOTO(out, ret = -errno);
         }
 
         if (sem_init && (ret = sem_init(path, parent ?: p, d, data, de)))
@@ -1038,9 +1062,9 @@ static int llapi_semantic_traverse(char *path, int size, DIR *parent,
 
                 path[len] = 0;
                 if ((len + dent->d_reclen + 2) > size) {
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "error: %s: string buffer is too small",
-                                  __func__);
+                        llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                          "error: %s: string buffer is too small",
+                                          __func__);
                         break;
                 }
 
@@ -1065,9 +1089,9 @@ static int llapi_semantic_traverse(char *path, int size, DIR *parent,
 
                 switch (dent->d_type) {
                 case DT_UNKNOWN:
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "error: %s: '%s' is UNKNOWN type %d",
-                                  __func__, dent->d_name, dent->d_type);
+                        llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                          "error: %s: '%s' is UNKNOWN type %d",
+                                          __func__, dent->d_name, dent->d_type);
                         break;
                 case DT_DIR:
                         ret = llapi_semantic_traverse(path, size, d, sem_init,
@@ -1107,8 +1131,10 @@ static int param_callback(char *path, semantic_func_t sem_init,
         char *buf;
 
         if (len > PATH_MAX) {
-                llapi_err(LLAPI_MSG_ERROR, "Path name '%s' is too long", path);
-                return -EINVAL;
+                ret = -EINVAL;
+                llapi_error(LLAPI_MSG_ERROR, ret,
+                            "Path name '%s' is too long", path);
+                return ret;
         }
 
         buf = (char *)malloc(PATH_MAX + 1);
@@ -1133,8 +1159,8 @@ int llapi_file_fget_lov_uuid(int fd, struct obd_uuid *lov_name)
 {
         int rc = ioctl(fd, OBD_IOC_GETNAME, lov_name);
         if (rc) {
-                rc = errno;
-                llapi_err(LLAPI_MSG_ERROR, "error: can't get lov name.");
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "error: can't get lov name.");
         }
         return rc;
 }
@@ -1145,15 +1171,14 @@ int llapi_file_get_lov_uuid(const char *path, struct obd_uuid *lov_uuid)
 
         fd = open(path, O_RDONLY);
         if (fd < 0) {
-                rc = errno;
-                llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
                 return rc;
         }
 
         rc = llapi_file_fget_lov_uuid(fd, lov_uuid);
 
         close(fd);
-
         return rc;
 }
 
@@ -1180,8 +1205,8 @@ int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
                  lov_name.uuid);
         fp = fopen(buf, "r");
         if (fp == NULL) {
-                rc = errno;
-                llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
                 return rc;
         }
 
@@ -1199,7 +1224,7 @@ int llapi_lov_get_uuids(int fd, struct obd_uuid *uuidp, int *ost_count)
                 return -EOVERFLOW;
 
         *ost_count = index;
-        return rc;
+        return 0;
 }
 
 /* Check if user specified value matches a real uuid.  Ignore _UUID,
@@ -1241,10 +1266,9 @@ static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
         /* Get the lov name */
         rc = llapi_file_fget_lov_uuid(dirfd(dir), &lov_uuid);
         if (rc) {
-                if (errno != ENOTTY) {
-                        rc = errno;
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "error: can't get lov name: %s", dname);
+                if (rc != -ENOTTY) {
+                        llapi_error(LLAPI_MSG_ERROR, rc,
+                                    "error: can't get lov name: %s", dname);
                 } else {
                         rc = 0;
                 }
@@ -1258,8 +1282,8 @@ static int setup_obd_uuid(DIR *dir, char *dname, struct find_param *param)
                  lov_uuid.uuid);
         fp = fopen(buf, "r");
         if (fp == NULL) {
-                rc = errno;
-                llapi_err(LLAPI_MSG_ERROR, "error: opening '%s'", buf);
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'", buf);
                 return rc;
         }
 
@@ -1323,7 +1347,7 @@ retry_get_uuids:
                                 ret = -ENOMEM;
                 }
 
-                llapi_err(LLAPI_MSG_ERROR, "get ost uuid failed");
+                llapi_error(LLAPI_MSG_ERROR, ret, "get ost uuid failed");
                 return ret;
         }
 
@@ -1408,7 +1432,7 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                                      prefix);
                         sp = is_dir ? " " : "";
                 }
-                if (is_dir) 
+                if (is_dir)
                         llapi_printf(LLAPI_MSG_NORMAL, "%d%s",
                                      lum->lmm_stripe_count ==
                                      (typeof(lum->lmm_stripe_count))(-1) ? -1 :
@@ -1439,7 +1463,7 @@ static void lov_dump_user_lmm_header(struct lov_user_md *lum, char *path,
                                      sp, prefix);
                         sp = is_dir ? " " : "";
                 }
-                if (is_dir) 
+                if (is_dir)
                         llapi_printf(LLAPI_MSG_NORMAL, "%d%s",
                                      lum->lmm_stripe_offset ==
                                      (typeof(lum->lmm_stripe_offset))(-1) ? -1 :
@@ -1620,20 +1644,19 @@ int llapi_file_get_stripe(const char *path, struct lov_user_md *lum)
         }
 
         if ((fd = open(dname, O_RDONLY)) == -1) {
-                rc = errno;
+                rc = -errno;
                 free(dname);
                 return rc;
         }
 
         strcpy((char *)lum, fname);
         if (ioctl(fd, IOC_MDC_GETFILESTRIPE, (void *)lum) == -1)
-                rc = errno;
+                rc = -errno;
 
         if (close(fd) == -1 && rc == 0)
-                rc = errno;
+                rc = -errno;
 
         free(dname);
-
         return rc;
 }
 
@@ -1654,13 +1677,16 @@ int llapi_file_lookup(int dirfd, const char *name)
 
         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
         if (rc) {
-                llapi_err(LLAPI_MSG_ERROR,
-                          "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
-                          name, rc);
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "error: IOC_MDC_LOOKUP pack failed for '%s': rc %d",
+                            name, rc);
                 return rc;
         }
 
-        return ioctl(dirfd, IOC_MDC_LOOKUP, buf);
+        rc = ioctl(dirfd, IOC_MDC_LOOKUP, buf);
+        if (rc < 0)
+                rc = -errno;
+        return rc;
 }
 
 /* Check if the value matches 1 of the given criteria (e.g. --atime +/-N).
@@ -1825,19 +1851,22 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir,
                         lustre_fs = 0;
                         ret = lstat_f(path, st);
                         if (ret) {
-                                llapi_err(LLAPI_MSG_ERROR,
-                                          "error: %s: lstat failed for %s",
-                                          __func__, path);
+                                ret = -errno;
+                                llapi_error(LLAPI_MSG_ERROR, ret,
+                                            "error: %s: lstat failed for %s",
+                                            __func__, path);
                                 return ret;
                         }
                 } else if (errno == ENOENT) {
-                        llapi_err(LLAPI_MSG_WARN,
+                        llapi_error(LLAPI_MSG_WARN, -ENOENT,
                                   "warning: %s: %s does not exist",
                                   __func__, path);
                         goto decided;
                 } else {
-                        llapi_err(LLAPI_MSG_ERROR,"error: %s: %s failed for %s",
-                                  __func__, dir ? "LL_IOC_MDC_GETINFO" :
+                        ret = -errno;
+                        llapi_error(LLAPI_MSG_ERROR, ret,
+                                    "error: %s: %s failed for %s",
+                                    __func__, dir ? "LL_IOC_MDC_GETINFO" :
                                   "IOC_MDC_GETFILEINFO", path);
                         return ret;
                 }
@@ -1984,14 +2013,15 @@ obd_matches:
 
                 if (ret) {
                         if (errno == ENOENT) {
-                                llapi_err(LLAPI_MSG_ERROR,
-                                          "warning: %s: %s does not exist",
-                                          __func__, path);
+                                llapi_error(LLAPI_MSG_ERROR, -ENOENT,
+                                            "warning: %s: %s does not exist",
+                                            __func__, path);
                                 goto decided;
                         } else {
-                                llapi_err(LLAPI_MSG_ERROR,
-                                          "%s: IOC_LOV_GETINFO on %s failed",
-                                          __func__, path);
+                                ret = -errno;
+                                llapi_error(LLAPI_MSG_ERROR, ret,
+                                            "%s: IOC_LOV_GETINFO on %s failed",
+                                            __func__, path);
                                 return ret;
                         }
                 }
@@ -2058,30 +2088,48 @@ static int cb_getstripe(char *path, DIR *parent, DIR *d, void *data,
         }
 
         if (ret) {
-                if (errno == ENODATA) {
+                if (errno == ENODATA && d != NULL) {
+                        /* We need to "fake" the "use the default" values
+                         * since the lmm struct is zeroed out at this point.
+                         * The magic needs to be set in order to satisfy
+                         * a check later on in the code path.
+                         * The object_seq needs to be set for the "(Default)"
+                         * prefix to be displayed. */
+                        struct lov_user_md *lmm = &param->lmd->lmd_lmm;
+                        lmm->lmm_magic = LOV_MAGIC_V1;
+                        lmm->lmm_object_gr = LOV_OBJECT_GROUP_DEFAULT;
+                        lmm->lmm_stripe_count = 0;
+                        lmm->lmm_stripe_size = 0;
+                        lmm->lmm_stripe_offset = -1;
+                        goto dump;
+
+                } else if (errno == ENODATA && parent != NULL) {
                         if (!param->obduuid)
                                 llapi_printf(LLAPI_MSG_NORMAL,
                                              "%s has no stripe info\n", path);
                         goto out;
-                } else if (errno == ENOTTY) {
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "%s: '%s' not on a Lustre fs?",
-                                  __func__, path);
                 } else if (errno == ENOENT) {
-                        llapi_err(LLAPI_MSG_WARN,
-                                  "warning: %s: %s does not exist",
-                                  __func__, path);
+                        llapi_error(LLAPI_MSG_WARN, -ENOENT,
+                                    "warning: %s: %s does not exist",
+                                    __func__, path);
                         goto out;
+                } else if (errno == ENOTTY) {
+                        ret = -errno;
+                        llapi_error(LLAPI_MSG_ERROR, ret,
+                                    "%s: '%s' not on a Lustre fs?",
+                                    __func__, path);
                 } else {
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "error: %s: %s failed for %s",
-                                   __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
-                                  "IOC_MDC_GETFILESTRIPE", path);
+                        ret = -errno;
+                        llapi_error(LLAPI_MSG_ERROR, ret,
+                                    "error: %s: %s failed for %s",
+                                     __func__, d ? "LL_IOC_LOV_GETSTRIPE" :
+                                    "IOC_MDC_GETFILESTRIPE", path);
                 }
 
                 return ret;
         }
 
+dump:
         llapi_lov_dump_user_lmm(param, path, d ? 1 : 0);
 out:
         /* Do not get down anymore? */
@@ -2116,9 +2164,10 @@ int llapi_obd_statfs(char *path, __u32 type, __u32 index,
         data.ioc_pbuf2 = (char *)uuid_buf;
         data.ioc_plen2 = sizeof(struct obd_uuid);
 
-        if ((rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw))) != 0) {
-                llapi_err(LLAPI_MSG_ERROR,
-                          "llapi_obd_statfs: error packing ioctl data");
+        rc = obd_ioctl_pack(&data, &rawbuf, sizeof(raw));
+        if (rc != 0) {
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "llapi_obd_statfs: error packing ioctl data");
                 return rc;
         }
 
@@ -2128,8 +2177,8 @@ int llapi_obd_statfs(char *path, __u32 type, __u32 index,
 
         if (fd < 0) {
                 rc = errno ? -errno : -EBADF;
-                llapi_err(LLAPI_MSG_ERROR, "error: %s: opening '%s'",
-                          __func__, path);
+                llapi_error(LLAPI_MSG_ERROR, rc, "error: %s: opening '%s'",
+                            __func__, path);
                 return rc;
         }
         rc = ioctl(fd, IOC_OBD_STATFS, (void *)rawbuf);
@@ -2154,12 +2203,14 @@ int llapi_ping(char *obd_type, char *obd_name)
 
         fd = open(path, O_WRONLY);
         if (fd < 0) {
-                rc = errno;
-                llapi_err(LLAPI_MSG_ERROR, "error opening %s", path);
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "error opening %s", path);
                 return rc;
         }
 
         rc = write(fd, buf, 1);
+        if (rc < 0)
+                rc = -errno;
         close(fd);
 
         if (rc == 1)
@@ -2167,15 +2218,16 @@ int llapi_ping(char *obd_type, char *obd_name)
         return rc;
 }
 
-int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
+int llapi_target_iterate(int type_num, char **obd_type,
+                         void *args, llapi_cb_t cb)
 {
         char buf[MAX_STRING_SIZE];
         FILE *fp = fopen(DEVICES_LIST, "r");
         int i, rc = 0;
 
         if (fp == NULL) {
-                rc = errno;
-                llapi_err(LLAPI_MSG_ERROR, "error: opening "DEVICES_LIST);
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "error: opening "DEVICES_LIST);
                 return rc;
         }
 
@@ -2212,7 +2264,7 @@ int llapi_target_iterate(int type_num, char **obd_type,void *args,llapi_cb_t cb)
                 }
         }
         fclose(fp);
-        return rc;
+        return 0;
 }
 
 static void do_target_check(char *obd_type_name, char *obd_name,
@@ -2224,7 +2276,7 @@ static void do_target_check(char *obd_type_name, char *obd_name,
         if (rc == ENOTCONN) {
                 llapi_printf(LLAPI_MSG_NORMAL, "%s inactive.\n", obd_name);
         } else if (rc) {
-                llapi_err(LLAPI_MSG_ERROR, "error: check '%s'", obd_name);
+                llapi_error(LLAPI_MSG_ERROR, rc, "error: check '%s'", obd_name);
         } else {
                 llapi_printf(LLAPI_MSG_NORMAL, "%s active.\n", obd_name);
         }
@@ -2264,16 +2316,19 @@ int llapi_catinfo(char *dir, char *keyword, char *node_name)
 
         root = opendir(dir);
         if (root == NULL) {
-                rc = errno;
-                llapi_err(LLAPI_MSG_ERROR, "open %s failed", dir);
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", dir);
                 return rc;
         }
 
         rc = ioctl(dirfd(root), OBD_IOC_LLOG_CATINFO, buf);
-        if (rc)
-                llapi_err(LLAPI_MSG_ERROR, "ioctl OBD_IOC_CATINFO failed");
-        else
+        if (rc) {
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "ioctl OBD_IOC_CATINFO failed");
+        } else {
                 llapi_printf(LLAPI_MSG_NORMAL, "%s", data.ioc_pbuf1);
+        }
 
         closedir(root);
         return rc;
@@ -2299,11 +2354,14 @@ int llapi_quotacheck(char *mnt, int check_type)
 
         root = opendir(mnt);
         if (!root) {
-                llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
-                return -1;
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
+                return rc;
         }
 
         rc = ioctl(dirfd(root), LL_IOC_QUOTACHECK, check_type);
+        if (rc < 0)
+                rc = -errno;
 
         closedir(root);
         return rc;
@@ -2317,8 +2375,9 @@ int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
 
         root = opendir(mnt);
         if (!root) {
-                llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
-                return -1;
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
+                return rc;
         }
 
         while (1) {
@@ -2331,7 +2390,7 @@ int llapi_poll_quotacheck(char *mnt, struct if_quotacheck *qchk)
         }
 
         closedir(root);
-        return rc;
+        return 0;
 }
 
 int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
@@ -2341,11 +2400,14 @@ int llapi_quotactl(char *mnt, struct if_quotactl *qctl)
 
         root = opendir(mnt);
         if (!root) {
-                llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
-                return -1;
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
+                return rc;
         }
 
         rc = ioctl(dirfd(root), LL_IOC_QUOTACTL, qctl);
+        if (rc < 0)
+                rc = -errno;
 
         closedir(root);
         return rc;
@@ -2377,19 +2439,19 @@ static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
         if (rc) {
                 if (errno == ENODATA) {
                         if (!param->obduuid && !param->quiet)
-                                llapi_err(LLAPI_MSG_ERROR,
-                                          "%s has no stripe info", path);
+                                llapi_error(LLAPI_MSG_ERROR, -ENODATA,
+                                            "%s has no stripe info", path);
                         rc = 0;
                 } else if (errno == ENOENT) {
-                        llapi_err(LLAPI_MSG_ERROR,
-                                  "warning: %s: %s does not exist",
-                                  __func__, path);
+                        llapi_error(LLAPI_MSG_ERROR, -ENOENT,
+                                    "warning: %s: %s does not exist",
+                                    __func__, path);
                         rc = 0;
                 } else if (errno != EISDIR) {
-                        rc = errno;
-                        llapi_err(LLAPI_MSG_ERROR, "%s ioctl failed for %s.",
-                                  d ? "LL_IOC_MDC_GETINFO" :
-                                  "IOC_MDC_GETFILEINFO", path);
+                        rc = -errno;
+                        llapi_error(LLAPI_MSG_ERROR, rc, "%s ioctl failed for %s.",
+                                    d ? "LL_IOC_MDC_GETINFO" :
+                                    "IOC_MDC_GETFILEINFO", path);
                 }
                 return rc;
         }
@@ -2401,12 +2463,15 @@ static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
          * invoke syscall directly. */
         rc = syscall(SYS_chown, path, -1, -1);
         if (rc)
-                llapi_err(LLAPI_MSG_ERROR, "error: chown %s", path);
+                llapi_error(LLAPI_MSG_ERROR, errno,
+                            "error: chown %s", path);
 
         rc = chmod(path, st->st_mode);
-        if (rc)
-                llapi_err(LLAPI_MSG_ERROR, "error: chmod %s (%hu)",
-                          path, st->st_mode);
+        if (rc) {
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "error: chmod %s (%hu)",
+                            path, st->st_mode);
+        }
 
         return rc;
 }
@@ -2414,7 +2479,7 @@ static int cb_quotachown(char *path, DIR *parent, DIR *d, void *data,
 int llapi_quotachown(char *path, int flag)
 {
         struct find_param param;
-        
+
         memset(&param, 0, sizeof(param));
         param.recursive = 1;
         param.verbose = 0;
@@ -2432,7 +2497,8 @@ int llapi_path2fid(const char *path, lustre_fid *fid)
                 return -errno;
 
         rc = ioctl(fd, LL_IOC_PATH2FID, fid);
-
+        if (rc)
+                rc = -errno;
         close(fd);
         return rc;
 }
@@ -2444,15 +2510,18 @@ int llapi_get_connect_flags(const char *mnt, __u64 *flags)
 
         root = opendir(mnt);
         if (!root) {
-                llapi_err(LLAPI_MSG_ERROR, "open %s failed", mnt);
-                return -1;
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc, "open %s failed", mnt);
+                return rc;
         }
 
         rc = ioctl(dirfd(root), LL_IOC_GET_CONNECT_FLAGS, flags);
+        if (rc < 0) {
+                rc = -errno;
+                llapi_error(LLAPI_MSG_ERROR, rc,
+                            "ioctl on %s for getting connect flags failed", mnt);
+        }
         closedir(root);
-        if (rc < 0)
-                llapi_err(LLAPI_MSG_ERROR,
-                          "ioctl on %s for getting connect flags failed", mnt);
         return rc;
 }
 
@@ -2475,9 +2544,9 @@ int llapi_get_version(char *buffer, int buffer_size,
 
         rc = ioctl(fd, OBD_GET_VERSION, buffer);
         if (rc == -1) {
-                rc = errno;
+                rc = -errno;
                 close(fd);
-                return -rc;
+                return rc;
         }
         close(fd);
         *version = data->ioc_bulk;