Whamcloud - gitweb
b=20101 lfs ost enhancements
[fs/lustre-release.git] / lustre / utils / liblustreapi.c
index a4c8cf6..a425b1a 100644 (file)
@@ -592,7 +592,7 @@ int llapi_search_mounts(const char *pathname, int index, char *mntdir,
 {
         int want = WANT_PATH, idx = -1;
 
-        if (!pathname) {
+        if (!pathname || pathname[0] == '\0') {
                 want |= WANT_INDEX;
                 idx = index;
         } else
@@ -1603,28 +1603,26 @@ int llapi_file_lookup(int dirfd, const char *name)
  * Note: 5th actually means that the value is within the interval
  * (limit - margin, limit]. */
 static int find_value_cmp(unsigned int file, unsigned int limit, int sign,
-                          unsigned long long margin, int mds)
+                          int negopt, unsigned long long margin, int mds)
 {
+        int ret = -1;
+        
         if (sign > 0) {
-                if (file < limit)
-                        return mds ? 0 : 1;
-        }
-
-        if (sign == 0) {
-                if (file <= limit && file + margin > limit)
-                        return mds ? 0 : 1;
-                if (file + margin <= limit)
-                        return mds ? 0 : -1;
-        }
-
-        if (sign < 0) {
-                if (file > limit)
-                        return 1;
-                if (mds)
-                        return 0;
+                if (file <= limit)
+                        ret = mds ? 0 : 1;
+        } else if (sign == 0) {
+                if (file <= limit && file + margin >= limit)
+                        ret = mds ? 0 : 1;
+                else if (file + margin <= limit)
+                        ret = mds ? 0 : -1;
+        } else if (sign < 0) {
+                if (file >= limit)
+                        ret = 1;
+                else if (mds)
+                        ret = 0;
         }
 
-        return -1;
+        return negopt ? ~ret + 1 : ret;
 }
 
 /* Check if the file time matches all the given criteria (e.g. --atime +/-N).
@@ -1642,7 +1640,8 @@ static int find_time_check(lstat_t *st, struct find_param *param, int mds)
         /* Check if file is accepted. */
         if (param->atime) {
                 ret = find_value_cmp(st->st_atime, param->atime,
-                                     param->asign, 24 * 60 * 60, mds);
+                                     param->asign, param->exclude_atime, 
+                                     24 * 60 * 60, mds);
                 if (ret < 0)
                         return ret;
                 rc = ret;
@@ -1650,7 +1649,8 @@ static int find_time_check(lstat_t *st, struct find_param *param, int mds)
 
         if (param->mtime) {
                 ret = find_value_cmp(st->st_mtime, param->mtime,
-                                     param->msign, 24 * 60 * 60, mds);
+                                     param->msign, param->exclude_mtime, 
+                                     24 * 60 * 60, mds);
                 if (ret < 0)
                         return ret;
 
@@ -1662,7 +1662,8 @@ static int find_time_check(lstat_t *st, struct find_param *param, int mds)
 
         if (param->ctime) {
                 ret = find_value_cmp(st->st_ctime, param->ctime,
-                                     param->csign, 24 * 60 * 60, mds);
+                                     param->csign, param->exclude_ctime,
+                                     24 * 60 * 60, mds);
                 if (ret < 0)
                         return ret;
 
@@ -1715,7 +1716,7 @@ static int cb_find_init(char *path, DIR *parent, DIR *dir,
 
         /* If a time or OST should be checked, the decision is not taken yet. */
         if (param->atime || param->ctime || param->mtime || param->obduuid ||
-            param->size)
+            param->check_size)
                 decision = 0;
 
         ret = 0;
@@ -1896,7 +1897,7 @@ obd_matches:
            'glimpse-size-ioctl'. */
         if (!decision && S_ISREG(st->st_mode) &&
             param->lmd->lmd_lmm.lmm_stripe_count &&
-            (param->size ||param->atime || param->mtime || param->ctime)) {
+            (param->check_size ||param->atime || param->mtime || param->ctime)) {
                 if (param->obdindex != OBD_NOT_FOUND) {
                         /* Check whether the obd is active or not, if it is
                          * not active, just print the object affected by this
@@ -1949,10 +1950,10 @@ obd_matches:
                         goto decided;
         }
 
-        if (param->size)
+        if (param->check_size)
                 decision = find_value_cmp(st->st_size, param->size,
-                                          param->size_sign, param->size_units,
-                                          0);
+                                          param->size_sign, param->exclude_size,
+                                          param->size_units, 0);
 
 print_path:
         if (decision != -1) {
@@ -2830,7 +2831,6 @@ struct changelog_private {
         int magic;
         int flags;
         lustre_kernelcomm kuc;
-        char *buf;
 };
 
 /** Start reading from a changelog
@@ -2847,16 +2847,10 @@ int llapi_changelog_start(void **priv, int flags, const char *device,
         int rc;
 
         /* Set up the receiver control struct */
-        cp = malloc(sizeof(*cp));
+        cp = calloc(1, sizeof(*cp));
         if (cp == NULL)
                 return -ENOMEM;
 
-        cp->buf = malloc(CR_MAXSIZE);
-        if (cp->buf == NULL) {
-                rc = -ENOMEM;
-                goto out_free;
-        }
-
         cp->magic = CHANGELOG_PRIV_MAGIC;
         cp->flags = flags;
 
@@ -2882,8 +2876,6 @@ int llapi_changelog_start(void **priv, int flags, const char *device,
         return 0;
 
 out_free:
-        if (cp->buf)
-                free(cp->buf);
         free(cp);
         return rc;
 }
@@ -2897,7 +2889,6 @@ int llapi_changelog_fini(void **priv)
                 return -EINVAL;
 
         libcfs_ukuc_stop(&cp->kuc);
-        free(cp->buf);
         free(cp);
         *priv = NULL;
         return 0;
@@ -2920,14 +2911,17 @@ int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
                 return -EINVAL;
         if (rech == NULL)
                 return -EINVAL;
+        kuch = malloc(CR_MAXSIZE + sizeof(*kuch));
+        if (kuch == NULL)
+                return -ENOMEM;
 
 repeat:
-        rc = libcfs_ukuc_msg_get(&cp->kuc, cp->buf, CR_MAXSIZE,
+        rc = libcfs_ukuc_msg_get(&cp->kuc, (char *)kuch,
+                                 CR_MAXSIZE + sizeof(*kuch),
                                  KUC_TRANSPORT_CHANGELOG);
         if (rc < 0)
-                return rc;
+                goto out_free;
 
-        kuch = (struct kuc_hdr *)cp->buf;
         if ((kuch->kuc_transport != KUC_TRANSPORT_CHANGELOG) ||
             ((kuch->kuc_msgtype != CL_RECORD) &&
              (kuch->kuc_msgtype != CL_EOF))) {
@@ -2948,19 +2942,30 @@ repeat:
                 }
         }
 
-        /* Our message is a changelog_rec */
+        /* Our message is a changelog_rec.  Use pointer math to skip
+         * kuch_hdr and point directly to the message payload.
+         */
         *rech = (struct changelog_rec *)(kuch + 1);
 
         return 0;
 
 out_free:
         *rech = NULL;
+        free(kuch);
         return rc;
 }
 
 /** Release the changelog record when done with it. */
 int llapi_changelog_free(struct changelog_rec **rech)
 {
+        if (*rech) {
+                /* We allocated memory starting at the kuc_hdr, but passed
+                 * the consumer a pointer to the payload.
+                 * Use pointer math to get back to the header.
+                 */
+                struct kuc_hdr *kuch = (struct kuc_hdr *)*rech - 1;
+                free(kuch);
+        }
         *rech = NULL;
         return 0;
 }
@@ -3078,7 +3083,6 @@ int llapi_path2fid(const char *path, lustre_fid *fid)
 #define CT_PRIV_MAGIC 0xC0BE2001
 struct copytool_private {
         int magic;
-        char *buf;
         char *fsname;
         lustre_kernelcomm kuc;
         __u32 archives;
@@ -3105,13 +3109,12 @@ int llapi_copytool_start(void **priv, char *fsname, int flags,
                 return -EINVAL;
         }
 
-        ct = malloc(sizeof(*ct));
+        ct = calloc(1, sizeof(*ct));
         if (ct == NULL)
                 return -ENOMEM;
 
-        ct->buf = malloc(HAL_MAXSIZE);
         ct->fsname = malloc(strlen(fsname) + 1);
-        if (ct->buf == NULL || ct->fsname == NULL) {
+        if (ct->fsname == NULL) {
                 rc = -ENOMEM;
                 goto out_err;
         }
@@ -3149,8 +3152,6 @@ int llapi_copytool_start(void **priv, char *fsname, int flags,
         return 0;
 
 out_err:
-        if (ct->buf)
-                free(ct->buf);
         if (ct->fsname)
                 free(ct->fsname);
         free(ct);
@@ -3172,7 +3173,6 @@ int llapi_copytool_fini(void **priv)
         /* Shut down the kernelcomms */
         libcfs_ukuc_stop(&ct->kuc);
 
-        free(ct->buf);
         free(ct->fsname);
         free(ct);
         *priv = NULL;
@@ -3198,13 +3198,17 @@ int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
         if (halh == NULL || msgsize == NULL)
                 return -EINVAL;
 
-        rc = libcfs_ukuc_msg_get(&ct->kuc, ct->buf, HAL_MAXSIZE,
+        kuch = malloc(HAL_MAXSIZE + sizeof(*kuch));
+        if (kuch == NULL)
+                return -ENOMEM;
+
+        rc = libcfs_ukuc_msg_get(&ct->kuc, (char *)kuch,
+                                 HAL_MAXSIZE + sizeof(*kuch),
                                  KUC_TRANSPORT_HSM);
         if (rc < 0)
-                return rc;
+                goto out_free;
 
         /* Handle generic messages */
-        kuch = (struct kuc_hdr *)ct->buf;
         if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
             kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
                 rc = -ESHUTDOWN;
@@ -3220,8 +3224,9 @@ int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
                 goto out_free;
         }
 
-        /* Our message is an hsm_action_list */
-
+        /* Our message is a hsm_action_list.  Use pointer math to skip
+         * kuch_hdr and point directly to the message payload.
+         */
         hal = (struct hsm_action_list *)(kuch + 1);
 
         /* Check that we have registered for this archive # */
@@ -3240,14 +3245,15 @@ int llapi_copytool_recv(void *priv, struct hsm_action_list **halh, int *msgsize)
 out_free:
         *halh = NULL;
         *msgsize = 0;
+        free(kuch);
         return rc;
 }
 
 /** Release the action list when done with it. */
 int llapi_copytool_free(struct hsm_action_list **hal)
 {
-        *hal = NULL;
-        return 0;
+        /* Reuse the llapi_changelog_free function */
+        return llapi_changelog_free((struct changelog_rec **)hal);
 }
 
 int llapi_get_connect_flags(const char *mnt, __u64 *flags)