axmap: fix bug in axmap_find_first_free()
authorJens Axboe <axboe@kernel.dk>
Wed, 23 Jan 2013 17:15:05 +0000 (10:15 -0700)
committerJens Axboe <axboe@kernel.dk>
Wed, 23 Jan 2013 17:15:05 +0000 (10:15 -0700)
If the map is completely full, we could return an invalid value
for the first free bit. So check if we actually found a match,
return failure if we didn't.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
lib/axmap.c

index d9ad30bf5b9d679070b6e1767b4b978dba7c3940..7351e29725f4189b8178d9e6895efdbc276f61c0 100644 (file)
@@ -309,7 +309,7 @@ static uint64_t axmap_find_first_free(struct axmap *axmap, unsigned int level,
                                       uint64_t index)
 {
        unsigned long j;
-       int i;
+       int i, found = 0;
 
        /*
         * Start at the bottom, then converge towards first free bit at the top
@@ -331,11 +331,12 @@ static uint64_t axmap_find_first_free(struct axmap *axmap, unsigned int level,
                         * free bit at the next higher level
                         */
                        index = (j << UNIT_SHIFT) + ffz(al->map[j]);
+                       found = 1;
                        break;
                }
        }
 
-       return index;
+       return found ? index : (uint64_t) -1ULL;
 }
 
 uint64_t axmap_first_free(struct axmap *axmap)