Hi all,
Here is a code snippet from e4defrag.c, can be located @
http://git.kernel.org/?p=fs/ext2/e2fsprogs.git;a=blob_plain;f=misc/e4defrag.c;hb=next
/*
* get_file_extents() - Get file's extent list.
*
* @fd: defrag target file's descriptor.
* @ext_list_head: the head of the extent list.
*/
static int get_file_extents(int fd, struct fiemap_extent_list **ext_list_head)
{
__u32 i;
int ret;
int ext_buf_size, fie_buf_size;
__u64 pos = 0;
struct fiemap *fiemap_buf = NULL;
struct fiemap_extent *ext_buf = NULL;
struct fiemap_extent_list *ext_list = NULL;
/* Convert units, in bytes.
* Be careful : now, physical block number in extent is 48bit,
* and the maximum blocksize for ext4 is 4K(12bit),
* so there is no overflow, but in future it may be changed.
*/
/* Alloc space for fiemap */
ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
fiemap_buf = malloc(fie_buf_size);
if (fiemap_buf == NULL)
return -1;
ext_buf = fiemap_buf->fm_extents;
memset(fiemap_buf, 0, fie_buf_size);
fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
do {
fiemap_buf->fm_start = pos;
memset(ext_buf, 0, ext_buf_size);
ret = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
if (ret < 0)
goto out;
for (i = 0; i < fiemap_buf->fm_mapped_extents; i++) {
ext_list = NULL;
ext_list = malloc(sizeof(struct fiemap_extent_list));
if (ext_list == NULL)
goto out;
ext_list->data.physical = ext_buf[i].fe_physical
/ block_size;
ext_list->data.logical = ext_buf[i].fe_logical
/ block_size;
ext_list->data.len = ext_buf[i].fe_length
/ block_size;
ret = insert_extent_by_physical(
ext_list_head, ext_list);
if (ret < 0) {
FREE(ext_list);
goto out;
}
}
/* Record file's logical offset this time */
pos = ext_buf[EXTENT_MAX_COUNT-1].fe_logical +
ext_buf[EXTENT_MAX_COUNT-1].fe_length;
/*
* If fm_extents array has been filled and
* there are extents left, continue to cycle.
*/
} while (fiemap_buf->fm_mapped_extents
== EXTENT_MAX_COUNT &&
!(ext_buf[EXTENT_MAX_COUNT-1].fe_flags
& FIEMAP_EXTENT_LAST));
FREE(fiemap_buf);
return 0;
out:
FREE(fiemap_buf);
return -1;
}
Why do we have a while loop here, we have already made sure that we
are initially itself allocating memory for EXTENT_MAX_COUNT?
This is the maximum number of extents supported by the file system. Is
this loop not redundant ?
--
Regards,
Sandeep.
“To learn is to change. Education is a process that changes the learner.”
--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to [email protected]
Please read the FAQ at http://kernelnewbies.org/FAQ