eal: introduce internal wrappers for file operations
[dpdk.git] / lib / librte_eal / common / eal_common_fbarray.c
index 0e7366e..c52ddb9 100644 (file)
@@ -8,8 +8,8 @@
 #include <sys/mman.h>
 #include <stdint.h>
 #include <errno.h>
-#include <sys/file.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <rte_common.h>
 #include <rte_log.h>
@@ -85,10 +85,8 @@ resize_and_map(int fd, void *addr, size_t len)
        char path[PATH_MAX];
        void *map_addr;
 
-       if (ftruncate(fd, len)) {
+       if (eal_file_truncate(fd, len)) {
                RTE_LOG(ERR, EAL, "Cannot truncate %s\n", path);
-               /* pass errno up the chain */
-               rte_errno = errno;
                return -1;
        }
 
@@ -709,7 +707,7 @@ fully_validate(const char *name, unsigned int elt_sz, unsigned int len)
        return 0;
 }
 
-int __rte_experimental
+int
 rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len,
                unsigned int elt_sz)
 {
@@ -736,15 +734,19 @@ rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len,
        }
 
        page_sz = sysconf(_SC_PAGESIZE);
-       if (page_sz == (size_t)-1)
-               goto fail;
+       if (page_sz == (size_t)-1) {
+               free(ma);
+               return -1;
+       }
 
        /* calculate our memory limits */
        mmap_len = calc_data_size(page_sz, elt_sz, len);
 
        data = eal_get_virtual_area(NULL, &mmap_len, page_sz, 0, 0);
-       if (data == NULL)
-               goto fail;
+       if (data == NULL) {
+               free(ma);
+               return -1;
+       }
 
        rte_spinlock_lock(&mem_area_lock);
 
@@ -768,15 +770,15 @@ rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len,
                 * and see if we succeed. If we don't, someone else is using it
                 * already.
                 */
-               fd = open(path, O_CREAT | O_RDWR, 0600);
+               fd = eal_file_open(path, EAL_OPEN_CREATE | EAL_OPEN_READWRITE);
                if (fd < 0) {
                        RTE_LOG(DEBUG, EAL, "%s(): couldn't open %s: %s\n",
-                                       __func__, path, strerror(errno));
-                       rte_errno = errno;
+                               __func__, path, rte_strerror(rte_errno));
                        goto fail;
-               } else if (flock(fd, LOCK_EX | LOCK_NB)) {
+               } else if (eal_file_lock(
+                               fd, EAL_FLOCK_EXCLUSIVE, EAL_FLOCK_RETURN)) {
                        RTE_LOG(DEBUG, EAL, "%s(): couldn't lock %s: %s\n",
-                                       __func__, path, strerror(errno));
+                               __func__, path, rte_strerror(rte_errno));
                        rte_errno = EBUSY;
                        goto fail;
                }
@@ -785,10 +787,8 @@ rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len,
                 * still attach to it, but no other process could reinitialize
                 * it.
                 */
-               if (flock(fd, LOCK_SH | LOCK_NB)) {
-                       rte_errno = errno;
+               if (eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN))
                        goto fail;
-               }
 
                if (resize_and_map(fd, data, mmap_len))
                        goto fail;
@@ -829,7 +829,7 @@ fail:
        return -1;
 }
 
-int __rte_experimental
+int
 rte_fbarray_attach(struct rte_fbarray *arr)
 {
        struct mem_area *ma = NULL, *tmp = NULL;
@@ -859,8 +859,10 @@ rte_fbarray_attach(struct rte_fbarray *arr)
        }
 
        page_sz = sysconf(_SC_PAGESIZE);
-       if (page_sz == (size_t)-1)
-               goto fail;
+       if (page_sz == (size_t)-1) {
+               free(ma);
+               return -1;
+       }
 
        mmap_len = calc_data_size(page_sz, arr->elt_sz, arr->len);
 
@@ -882,17 +884,14 @@ rte_fbarray_attach(struct rte_fbarray *arr)
 
        eal_get_fbarray_path(path, sizeof(path), arr->name);
 
-       fd = open(path, O_RDWR);
+       fd = eal_file_open(path, EAL_OPEN_READWRITE);
        if (fd < 0) {
-               rte_errno = errno;
                goto fail;
        }
 
        /* lock the file, to let others know we're using it */
-       if (flock(fd, LOCK_SH | LOCK_NB)) {
-               rte_errno = errno;
+       if (eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN))
                goto fail;
-       }
 
        if (resize_and_map(fd, data, mmap_len))
                goto fail;
@@ -906,6 +905,7 @@ rte_fbarray_attach(struct rte_fbarray *arr)
 
        /* we're done */
 
+       rte_spinlock_unlock(&mem_area_lock);
        return 0;
 fail:
        if (data)
@@ -913,10 +913,11 @@ fail:
        if (fd >= 0)
                close(fd);
        free(ma);
+       rte_spinlock_unlock(&mem_area_lock);
        return -1;
 }
 
-int __rte_experimental
+int
 rte_fbarray_detach(struct rte_fbarray *arr)
 {
        struct mem_area *tmp = NULL;
@@ -970,7 +971,7 @@ out:
        return ret;
 }
 
-int __rte_experimental
+int
 rte_fbarray_destroy(struct rte_fbarray *arr)
 {
        struct mem_area *tmp = NULL;
@@ -1017,7 +1018,7 @@ rte_fbarray_destroy(struct rte_fbarray *arr)
                 * has been detached by all other processes
                 */
                fd = tmp->fd;
-               if (flock(fd, LOCK_EX | LOCK_NB)) {
+               if (eal_file_lock(fd, EAL_FLOCK_EXCLUSIVE, EAL_FLOCK_RETURN)) {
                        RTE_LOG(DEBUG, EAL, "Cannot destroy fbarray - another process is using it\n");
                        rte_errno = EBUSY;
                        ret = -1;
@@ -1034,7 +1035,7 @@ rte_fbarray_destroy(struct rte_fbarray *arr)
                         * we're still holding an exclusive lock, so drop it to
                         * shared.
                         */
-                       flock(fd, LOCK_SH | LOCK_NB);
+                       eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN);
 
                        ret = -1;
                        goto out;
@@ -1047,12 +1048,15 @@ rte_fbarray_destroy(struct rte_fbarray *arr)
        TAILQ_REMOVE(&mem_area_tailq, tmp, next);
        free(tmp);
        ret = 0;
+
+       /* reset the fbarray structure */
+       memset(arr, 0, sizeof(*arr));
 out:
        rte_spinlock_unlock(&mem_area_lock);
        return ret;
 }
 
-void * __rte_experimental
+void *
 rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
 {
        void *ret = NULL;
@@ -1071,19 +1075,19 @@ rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
        return ret;
 }
 
-int __rte_experimental
+int
 rte_fbarray_set_used(struct rte_fbarray *arr, unsigned int idx)
 {
        return set_used(arr, idx, true);
 }
 
-int __rte_experimental
+int
 rte_fbarray_set_free(struct rte_fbarray *arr, unsigned int idx)
 {
        return set_used(arr, idx, false);
 }
 
-int __rte_experimental
+int
 rte_fbarray_is_used(struct rte_fbarray *arr, unsigned int idx)
 {
        struct used_mask *msk;
@@ -1152,25 +1156,25 @@ out:
        return ret;
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_next_free(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find(arr, start, true, false);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_next_used(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find(arr, start, true, true);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_prev_free(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find(arr, start, false, false);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_prev_used(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find(arr, start, false, true);
@@ -1228,28 +1232,28 @@ out:
        return ret;
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_next_n_free(struct rte_fbarray *arr, unsigned int start,
                unsigned int n)
 {
        return fbarray_find_n(arr, start, n, true, false);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_next_n_used(struct rte_fbarray *arr, unsigned int start,
                unsigned int n)
 {
        return fbarray_find_n(arr, start, n, true, true);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_prev_n_free(struct rte_fbarray *arr, unsigned int start,
                unsigned int n)
 {
        return fbarray_find_n(arr, start, n, false, false);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_prev_n_used(struct rte_fbarray *arr, unsigned int start,
                unsigned int n)
 {
@@ -1326,7 +1330,7 @@ fbarray_find_biggest(struct rte_fbarray *arr, unsigned int start, bool used,
         */
 
        /* the API's called are thread-safe, but something may still happen
-        * inbetween the API calls, so lock the fbarray. all other API's are
+        * between the API calls, so lock the fbarray. all other API's are
         * read-locking the fbarray, so read lock here is OK.
         */
        rte_rwlock_read_lock(&arr->rwlock);
@@ -1392,56 +1396,56 @@ fbarray_find_biggest(struct rte_fbarray *arr, unsigned int start, bool used,
        return biggest_idx;
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_biggest_free(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find_biggest(arr, start, false, false);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_biggest_used(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find_biggest(arr, start, true, false);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_rev_biggest_free(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find_biggest(arr, start, false, true);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_rev_biggest_used(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find_biggest(arr, start, true, true);
 }
 
 
-int __rte_experimental
+int
 rte_fbarray_find_contig_free(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find_contig(arr, start, true, false);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_contig_used(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find_contig(arr, start, true, true);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_rev_contig_free(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find_contig(arr, start, false, false);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_rev_contig_used(struct rte_fbarray *arr, unsigned int start)
 {
        return fbarray_find_contig(arr, start, false, true);
 }
 
-int __rte_experimental
+int
 rte_fbarray_find_idx(const struct rte_fbarray *arr, const void *elt)
 {
        void *end;
@@ -1467,7 +1471,7 @@ rte_fbarray_find_idx(const struct rte_fbarray *arr, const void *elt)
        return ret;
 }
 
-void __rte_experimental
+void
 rte_fbarray_dump_metadata(struct rte_fbarray *arr, FILE *f)
 {
        struct used_mask *msk;