--- IO-AIO/AIO.xs 2012/07/27 18:43:25 1.219 +++ IO-AIO/AIO.xs 2012/07/30 16:44:12 1.220 @@ -167,56 +167,86 @@ req->result = -1; #if HAVE_FIEMAP + /* assume some c99 */ + struct fiemap *fiemap = 0; + size_t end_offset; int count = req->int3; - /* heuristic: first try with 72 extents if we don't know how many, */ - /* as most files have (hopefully) fewer than this many extents */ - /* in fact, most should have <= 2, so the 72 below is probably overkill */ + req->flags |= EIO_FLAG_PTR1_FREE; + + /* heuristic: start with 512 bytes (8 extents), and if that isn't enough, */ + /* increase in 3.5kb steps */ if (count < 0) - count = 72; /* for what it's worth, 72 extents fit nicely into 4kb */ + count = 8; + + fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); + errno = ENOMEM; + if (!fiemap) + return; + + req->ptr1 = fiemap; + + fiemap->fm_start = req->offs; + fiemap->fm_length = req->size; + fiemap->fm_flags = req->int2; + fiemap->fm_extent_count = count; + + if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) + return; + + if (req->int3 >= 0 /* not autosizing */ + || !fiemap->fm_mapped_extents /* no more extents */ + || fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST /* hit eof */) + goto done; + + /* else we have to loop - + * it would be tempting (atcually I tried that first) to just query the + * number of extents needed, but linux often feels like not returning all + * extents, without telling us it left any out. this complicates + * this quite a bit. + */ + + end_offset = fiemap->fm_length + (fiemap->fm_length == FIEMAP_MAX_OFFSET ? 0 : fiemap->fm_start); for (;;) { - struct fiemap *fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); + /* we go in 54 extent steps - 3kb, in the hope that this fits nicely on the eio stack (normally 16+ kb) */ + char scratch[3072]; + struct fiemap *incmap = (struct fiemap *)scratch; + + incmap->fm_start = fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_logical + + fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_length; + incmap->fm_length = fiemap->fm_length - (incmap->fm_start - fiemap->fm_start); + incmap->fm_flags = fiemap->fm_flags; + incmap->fm_extent_count = (sizeof (scratch) - sizeof (struct fiemap)) / sizeof (struct fiemap_extent); + + if (ioctl (req->int1, FS_IOC_FIEMAP, incmap)) + return; + + count = fiemap->fm_mapped_extents + incmap->fm_mapped_extents; + fiemap = realloc (fiemap, sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); errno = ENOMEM; if (!fiemap) return; req->ptr1 = fiemap; - req->flags |= EIO_FLAG_PTR1_FREE; - - fiemap->fm_start = req->offs; - fiemap->fm_length = req->size; - fiemap->fm_flags = req->int2; - fiemap->fm_extent_count = count; - if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) - return; + for (count = 0; count < incmap->fm_mapped_extents; ++count) + { + struct fiemap_extent *e = incmap->fm_extents + count; - if (req->int3 >= 0) - break; /* when not autosizing we are done */ + if (e->fe_logical + e->fe_length >= end_offset) + goto done; - if (fiemap->fm_mapped_extents < count) - /* either autosizing succeeded, - * or the file had no extents - * or we luckily had enough space */ - break; - - /* some kernels overwrite fm_length, so just reset everything */ - fiemap->fm_start = req->offs; - fiemap->fm_length = req->size; - fiemap->fm_flags = req->int2; - fiemap->fm_extent_count = 0; - - if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) - return; + fiemap->fm_extents [fiemap->fm_mapped_extents++] = *e; - /* to work around a kernel bug, we allocate one more */ - count = fiemap->fm_mapped_extents + 1; + if (e->fe_flags & FIEMAP_EXTENT_LAST) + goto done; - free (fiemap); + } } +done: req->result = 0; #else