* grub-core/fs/btrfs.c: Some code style fixes.

This commit is contained in:
Szymon Janc 2011-08-14 11:42:53 +02:00
parent b2d7e331e2
commit b6085f3236
2 changed files with 94 additions and 97 deletions

View file

@ -1,3 +1,7 @@
2011-08-14 Szymon Janc <szymon@janc.net.pl>
* grub-core/fs/btrfs.c: Some code style fixes.
2001-08-14 Szymon Janc <szymon@janc.net.pl>
Add helper functions for easier unaligned memory access.

View file

@ -41,7 +41,7 @@ struct grub_btrfs_device
} __attribute__ ((packed));
struct grub_btrfs_superblock
{
{
grub_btrfs_checksum_t checksum;
grub_btrfs_uuid_t uuid;
grub_uint8_t dummy[0x10];
@ -160,7 +160,8 @@ struct grub_btrfs_leaf_descriptor
{
unsigned depth;
unsigned allocated;
struct {
struct
{
grub_disk_addr_t addr;
unsigned iter;
unsigned maxiter;
@ -179,7 +180,7 @@ struct grub_btrfs_time
{
grub_int64_t sec;
grub_uint32_t nanosec;
} __attribute__ ((aligned(4)));
} __attribute__ ((aligned (4)));
struct grub_btrfs_inode
{
@ -219,8 +220,8 @@ struct grub_btrfs_extent_data
#define GRUB_BTRFS_OBJECT_ID_CHUNK 0x100
static grub_disk_addr_t superblock_sectors[] = { 64 * 2, 64 * 1024 * 2,
256 * 1048576 * 2,
1048576ULL * 1048576ULL * 2 };
256 * 1048576 * 2, 1048576ULL * 1048576ULL * 2
};
static grub_err_t
grub_btrfs_read_logical (struct grub_btrfs_data *data,
@ -283,7 +284,7 @@ free_iterator (struct grub_btrfs_leaf_descriptor *desc)
}
static grub_err_t
save_ref (struct grub_btrfs_leaf_descriptor *desc,
save_ref (struct grub_btrfs_leaf_descriptor *desc,
grub_disk_addr_t addr, unsigned i, unsigned m, int l)
{
desc->depth++;
@ -307,7 +308,7 @@ save_ref (struct grub_btrfs_leaf_descriptor *desc,
static int
next (struct grub_btrfs_data *data,
struct grub_btrfs_leaf_descriptor *desc,
grub_disk_addr_t *outaddr, grub_size_t *outsize,
grub_disk_addr_t * outaddr, grub_size_t * outsize,
struct grub_btrfs_key *key_out)
{
grub_err_t err;
@ -316,8 +317,8 @@ next (struct grub_btrfs_data *data,
for (; desc->depth > 0; desc->depth--)
{
desc->data[desc->depth - 1].iter++;
if (desc->data[desc->depth - 1].iter
< desc->data[desc->depth - 1].maxiter)
if (desc->data[desc->depth - 1].iter <
desc->data[desc->depth - 1].maxiter)
break;
}
if (desc->depth == 0)
@ -330,17 +331,17 @@ next (struct grub_btrfs_data *data,
err = grub_btrfs_read_logical (data, desc->data[desc->depth - 1].iter
* sizeof (node)
+ sizeof (struct btrfs_header)
+ desc->data[desc->depth - 1].addr, &node,
sizeof (node));
+ desc->data[desc->depth - 1].addr,
&node, sizeof (node));
if (err)
return -err;
err = grub_btrfs_read_logical (data, grub_le_to_cpu64 (node.addr), &head,
sizeof (head));
err = grub_btrfs_read_logical (data, grub_le_to_cpu64 (node.addr),
&head, sizeof (head));
if (err)
return -err;
save_ref (desc, grub_le_to_cpu64 (node.addr), 0,
save_ref (desc, grub_le_to_cpu64 (node.addr), 0,
grub_le_to_cpu32 (head.nitems), !head.level);
}
err = grub_btrfs_read_logical (data, desc->data[desc->depth - 1].iter
@ -354,15 +355,15 @@ next (struct grub_btrfs_data *data,
*outaddr = desc->data[desc->depth - 1].addr + sizeof (struct btrfs_header)
+ grub_le_to_cpu32 (leaf.offset);
*key_out = leaf.key;
return 1;
return 1;
}
static grub_err_t
lower_bound (struct grub_btrfs_data *data,
const struct grub_btrfs_key *key_in,
const struct grub_btrfs_key *key_in,
struct grub_btrfs_key *key_out,
grub_disk_addr_t root,
grub_disk_addr_t *outaddr, grub_size_t *outsize,
grub_disk_addr_t * outaddr, grub_size_t * outsize,
struct grub_btrfs_leaf_descriptor *desc)
{
grub_disk_addr_t addr = root;
@ -410,8 +411,9 @@ lower_bound (struct grub_btrfs_data *data,
grub_dprintf ("btrfs",
"internal node (depth %d) %" PRIxGRUB_UINT64_T
" %x %" PRIxGRUB_UINT64_T "\n", depth,
node.key.object_id, node.key.type, node.key.offset);
node.key.object_id, node.key.type,
node.key.offset);
if (key_cmp (&node.key, key_in) == 0)
{
err = GRUB_ERR_NONE;
@ -433,7 +435,7 @@ lower_bound (struct grub_btrfs_data *data,
err = GRUB_ERR_NONE;
if (desc)
err = save_ref (desc, addr - sizeof (head), i - 1,
grub_le_to_cpu32 (head.nitems), 0);
grub_le_to_cpu32 (head.nitems), 0);
if (err)
return err;
addr = grub_le_to_cpu64 (node_last.addr);
@ -457,7 +459,7 @@ lower_bound (struct grub_btrfs_data *data,
&leaf, sizeof (leaf));
if (err)
return err;
grub_dprintf ("btrfs",
"leaf (depth %d) %" PRIxGRUB_UINT64_T
" %x %" PRIxGRUB_UINT64_T "\n", depth,
@ -465,31 +467,31 @@ lower_bound (struct grub_btrfs_data *data,
if (key_cmp (&leaf.key, key_in) == 0)
{
grub_memcpy (key_out, &leaf.key, sizeof(*key_out));
grub_memcpy (key_out, &leaf.key, sizeof (*key_out));
*outsize = grub_le_to_cpu32 (leaf.size);
*outaddr = addr + grub_le_to_cpu32 (leaf.offset);
if (desc)
return save_ref (desc, addr - sizeof (head), i,
grub_le_to_cpu32 (head.nitems), 1);
return GRUB_ERR_NONE;
return GRUB_ERR_NONE;
}
if (key_cmp (&leaf.key, key_in) > 0)
break;
have_last = 1;
leaf_last = leaf;
}
if (have_last)
{
grub_memcpy (key_out, &leaf_last.key, sizeof(*key_out));
grub_memcpy (key_out, &leaf_last.key, sizeof (*key_out));
*outsize = grub_le_to_cpu32 (leaf_last.size);
*outaddr = addr + grub_le_to_cpu32 (leaf_last.offset);
if (desc)
return save_ref (desc, addr - sizeof (head), i - 1,
grub_le_to_cpu32 (head.nitems), 1);
return GRUB_ERR_NONE;
return GRUB_ERR_NONE;
}
*outsize = 0;
*outaddr = 0;
@ -503,8 +505,7 @@ lower_bound (struct grub_btrfs_data *data,
}
static grub_device_t
find_device (struct grub_btrfs_data *data, grub_uint64_t id,
int do_rescan)
find_device (struct grub_btrfs_data *data, grub_uint64_t id, int do_rescan)
{
grub_device_t dev_found = NULL;
auto int hook (const char *name);
@ -540,7 +541,7 @@ find_device (struct grub_btrfs_data *data, grub_uint64_t id,
grub_device_close (dev);
return 0;
}
dev_found = dev;
return 1;
}
@ -579,17 +580,16 @@ find_device (struct grub_btrfs_data *data, grub_uint64_t id,
}
static grub_err_t
grub_btrfs_read_logical (struct grub_btrfs_data *data,
grub_disk_addr_t addr,
grub_btrfs_read_logical (struct grub_btrfs_data *data, grub_disk_addr_t addr,
void *buf, grub_size_t size)
{
while (size > 0)
{
grub_uint8_t *ptr;
struct grub_btrfs_key *key;
struct grub_btrfs_chunk_item *chunk;
struct grub_btrfs_chunk_item *chunk;
grub_uint64_t csize;
grub_err_t err = 0;
grub_err_t err = 0;
struct grub_btrfs_key key_out;
int challoc = 0;
grub_device_t dev;
@ -601,20 +601,20 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data,
addr);
for (ptr = data->sblock.bootstrap_mapping;
ptr < data->sblock.bootstrap_mapping
+ sizeof (data->sblock.bootstrap_mapping)
- sizeof (struct grub_btrfs_key);
)
+ sizeof (data->sblock.bootstrap_mapping)
- sizeof (struct grub_btrfs_key);)
{
key = (struct grub_btrfs_key *) ptr;
if (key->type != GRUB_BTRFS_ITEM_TYPE_CHUNK)
break;
chunk = (struct grub_btrfs_chunk_item *) (key + 1);
grub_dprintf ("btrfs", "%" PRIxGRUB_UINT64_T " %" PRIxGRUB_UINT64_T " \n",
grub_dprintf ("btrfs",
"%" PRIxGRUB_UINT64_T " %" PRIxGRUB_UINT64_T " \n",
grub_le_to_cpu64 (key->offset),
grub_le_to_cpu64 (chunk->size));
if (grub_le_to_cpu64 (key->offset) <= addr
&& addr < grub_le_to_cpu64 (key->offset)
+ grub_le_to_cpu64 (chunk->size))
&& addr <
grub_le_to_cpu64 (key->offset) + grub_le_to_cpu64 (chunk->size))
goto chunk_found;
ptr += sizeof (*key) + sizeof (*chunk)
+ sizeof (struct grub_btrfs_chunk_stripe)
@ -704,11 +704,11 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data,
middle = grub_divmod64 (off,
grub_le_to_cpu64 (chunk->stripe_length),
&low);
high = grub_divmod64 (middle, grub_le_to_cpu16 (chunk->nstripes),
&stripen);
stripe_offset = low + grub_le_to_cpu64 (chunk->stripe_length)
* high;
stripe_offset =
low + grub_le_to_cpu64 (chunk->stripe_length) * high;
csize = grub_le_to_cpu64 (chunk->stripe_length) - low;
break;
}
@ -719,7 +719,7 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data,
middle = grub_divmod64 (off,
grub_le_to_cpu64 (chunk->stripe_length),
&low);
high = grub_divmod64 (middle,
grub_le_to_cpu16 (chunk->nsubstripes),
&stripen);
@ -759,7 +759,8 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data,
paddr = stripe->offset + stripe_offset;
grub_dprintf ("btrfs", "chunk 0x%" PRIxGRUB_UINT64_T
"+0x%" PRIxGRUB_UINT64_T " (%d stripes (%d substripes) of %"
"+0x%" PRIxGRUB_UINT64_T
" (%d stripes (%d substripes) of %"
PRIxGRUB_UINT64_T ") stripe %" PRIxGRUB_UINT64_T
" maps to 0x%" PRIxGRUB_UINT64_T "\n",
grub_le_to_cpu64 (key->offset),
@ -769,7 +770,7 @@ grub_btrfs_read_logical (struct grub_btrfs_data *data,
grub_le_to_cpu64 (chunk->stripe_length),
stripen, stripe->offset);
grub_dprintf ("btrfs", "reading paddr 0x%" PRIxGRUB_UINT64_T
" for laddr 0x%" PRIxGRUB_UINT64_T"\n", paddr,
" for laddr 0x%" PRIxGRUB_UINT64_T "\n", paddr,
addr);
dev = find_device (data, stripe->device_id, j);
@ -815,7 +816,7 @@ grub_btrfs_mount (grub_device_t dev)
}
data = grub_zalloc (sizeof (*data));
if (! data)
if (!data)
return NULL;
err = read_sblock (dev->disk, &data->sblock);
@ -866,8 +867,7 @@ grub_btrfs_read_inode (struct grub_btrfs_data *data,
key_in.type = GRUB_BTRFS_ITEM_TYPE_INODE_ITEM;
key_in.offset = 0;
err = lower_bound (data, &key_in, &key_out, tree,
&elemaddr, &elemsize, NULL);
err = lower_bound (data, &key_in, &key_out, tree, &elemaddr, &elemsize, NULL);
if (err)
return err;
if (num != key_out.object_id
@ -917,19 +917,17 @@ grub_btrfs_extent_read (struct grub_btrfs_data *data,
if (!data->extent)
return grub_errno;
err = grub_btrfs_read_logical (data, elemaddr,
data->extent, elemsize);
err = grub_btrfs_read_logical (data, elemaddr, data->extent,
elemsize);
if (err)
return err;
data->extend = data->extstart
+ grub_le_to_cpu64 (data->extent->size);
data->extend = data->extstart + grub_le_to_cpu64 (data->extent->size);
if (data->extent->type == GRUB_BTRFS_EXTENT_REGULAR
&& (char *) &data->extent + elemsize
>= (char *) &data->extent->filled
+ sizeof (data->extent->filled))
data->extend = data->extstart
+ grub_le_to_cpu64 (data->extent->filled);
&& (char *) &data->extent + elemsize >=
(char *) &data->extent->filled + sizeof (data->extent->filled))
data->extend =
data->extstart + grub_le_to_cpu64 (data->extent->filled);
grub_dprintf ("btrfs", "extent 0x%" PRIxGRUB_UINT64_T "+0x%"
PRIxGRUB_UINT64_T " (0x%"
@ -966,8 +964,7 @@ grub_btrfs_extent_read (struct grub_btrfs_data *data,
if (data->extent->encoding)
{
grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
"encoding not supported");
grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "encoding not supported");
return -1;
}
@ -1008,8 +1005,8 @@ grub_btrfs_extent_read (struct grub_btrfs_data *data,
grub_free (tmp);
return -1;
}
if (grub_zlib_decompress (tmp, zsize, extoff
+ grub_le_to_cpu64 (data->extent->offset),
if (grub_zlib_decompress (tmp, zsize, extoff +
grub_le_to_cpu64 (data->extent->offset),
buf, csize) != (grub_ssize_t) csize)
{
grub_free (tmp);
@ -1021,14 +1018,13 @@ grub_btrfs_extent_read (struct grub_btrfs_data *data,
err = grub_btrfs_read_logical (data,
grub_le_to_cpu64 (data->extent->laddr)
+ grub_le_to_cpu64 (data->extent->offset)
+ extoff,
buf, csize);
+ extoff, buf, csize);
if (err)
return -1;
break;
default:
grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
"unsupported extent type 0x%x", data->extent->type);
"unsupported extent type 0x%x", data->extent->type);
return -1;
}
buf += csize;
@ -1041,7 +1037,7 @@ grub_btrfs_extent_read (struct grub_btrfs_data *data,
static grub_err_t
find_path (struct grub_btrfs_data *data,
const char *path, struct grub_btrfs_key *key,
grub_uint64_t *tree, grub_uint8_t *type)
grub_uint64_t * tree, grub_uint8_t * type)
{
const char *slash = path;
grub_err_t err;
@ -1096,9 +1092,9 @@ find_path (struct grub_btrfs_data *data,
key->type = GRUB_BTRFS_ITEM_TYPE_DIR_ITEM;
key->offset = grub_cpu_to_le64 (~grub_getcrc32c (1, ctoken, ctokenlen));
err = lower_bound (data, key, &key_out, *tree,
&elemaddr, &elemsize, NULL);
err = lower_bound (data, key, &key_out, *tree, &elemaddr, &elemsize,
NULL);
if (err)
{
grub_free (direl);
@ -1140,7 +1136,7 @@ find_path (struct grub_btrfs_data *data,
for (cdirel = direl;
(grub_uint8_t *) cdirel - (grub_uint8_t *) direl
< (grub_ssize_t) elemsize;
< (grub_ssize_t) elemsize;
cdirel = (void *) ((grub_uint8_t *) (direl + 1)
+ grub_le_to_cpu16 (cdirel->n)
+ grub_le_to_cpu16 (cdirel->m)))
@ -1174,7 +1170,7 @@ find_path (struct grub_btrfs_data *data,
return grub_error (GRUB_ERR_SYMLINK_LOOP,
"too deep nesting of symlinks");
}
err = grub_btrfs_read_inode (data, &inode,
cdirel->key.object_id, *tree);
if (err)
@ -1205,7 +1201,7 @@ find_path (struct grub_btrfs_data *data,
grub_free (tmp);
return grub_errno;
}
grub_memcpy (tmp + grub_le_to_cpu64 (inode.size), path,
grub_memcpy (tmp + grub_le_to_cpu64 (inode.size), path,
grub_strlen (path) + 1);
grub_free (path_alloc);
grub_free (origpath);
@ -1247,8 +1243,7 @@ find_path (struct grub_btrfs_data *data,
grub_free (origpath);
return err;
}
err = grub_btrfs_read_logical (data, elemaddr,
&ri, sizeof (ri));
err = grub_btrfs_read_logical (data, elemaddr, &ri, sizeof (ri));
if (err)
{
grub_free (direl);
@ -1279,7 +1274,7 @@ find_path (struct grub_btrfs_data *data,
grub_free (path_alloc);
grub_free (origpath);
grub_free (direl);
return grub_error (GRUB_ERR_BAD_FS, "unrecognised object type 0x%x",
return grub_error (GRUB_ERR_BAD_FS, "unrecognised object type 0x%x",
cdirel->key.type);
}
}
@ -1290,8 +1285,8 @@ find_path (struct grub_btrfs_data *data,
static grub_err_t
grub_btrfs_dir (grub_device_t device, const char *path,
int (*hook) (const char *filename,
const struct grub_dirhook_info *info))
int (*hook) (const char *filename,
const struct grub_dirhook_info * info))
{
struct grub_btrfs_data *data = grub_btrfs_mount (device);
struct grub_btrfs_key key_in, key_out;
@ -1312,10 +1307,9 @@ grub_btrfs_dir (grub_device_t device, const char *path,
if (err)
return err;
if (type != GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
err = lower_bound (data, &key_in, &key_out, tree,
&elemaddr, &elemsize, &desc);
err = lower_bound (data, &key_in, &key_out, tree, &elemaddr, &elemsize, &desc);
if (err)
return err;
if (key_out.type != GRUB_BTRFS_ITEM_TYPE_DIR_ITEM
@ -1355,7 +1349,7 @@ grub_btrfs_dir (grub_device_t device, const char *path,
for (cdirel = direl;
(grub_uint8_t *) cdirel - (grub_uint8_t *) direl
< (grub_ssize_t) elemsize;
< (grub_ssize_t) elemsize;
cdirel = (void *) ((grub_uint8_t *) (direl + 1)
+ grub_le_to_cpu16 (cdirel->n)
+ grub_le_to_cpu16 (cdirel->m)))
@ -1384,7 +1378,7 @@ grub_btrfs_dir (grub_device_t device, const char *path,
}
while (r > 0);
out:
out:
grub_free (direl);
free_iterator (&desc);
@ -1456,7 +1450,7 @@ grub_btrfs_uuid (grub_device_t device, char **uuid)
*uuid = NULL;
data = grub_btrfs_mount (device);
if (! data)
if (!data)
return grub_errno;
*uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
@ -1482,7 +1476,7 @@ grub_btrfs_label (grub_device_t device, char **label)
*label = NULL;
data = grub_btrfs_mount (device);
if (! data)
if (!data)
return grub_errno;
*label = grub_strndup (data->sblock.label, sizeof (data->sblock.label));
@ -1492,26 +1486,25 @@ grub_btrfs_label (grub_device_t device, char **label)
return grub_errno;
}
static struct grub_fs grub_btrfs_fs =
{
.name = "btrfs",
.dir = grub_btrfs_dir,
.open = grub_btrfs_open,
.read = grub_btrfs_read,
.close = grub_btrfs_close,
.uuid = grub_btrfs_uuid,
.label = grub_btrfs_label,
static struct grub_fs grub_btrfs_fs = {
.name = "btrfs",
.dir = grub_btrfs_dir,
.open = grub_btrfs_open,
.read = grub_btrfs_read,
.close = grub_btrfs_close,
.uuid = grub_btrfs_uuid,
.label = grub_btrfs_label,
#ifdef GRUB_UTIL
.reserved_first_sector = 1,
.reserved_first_sector = 1,
#endif
};
};
GRUB_MOD_INIT(btrfs)
GRUB_MOD_INIT (btrfs)
{
grub_fs_register (&grub_btrfs_fs);
}
GRUB_MOD_FINI(btrfs)
GRUB_MOD_FINI (btrfs)
{
grub_fs_unregister (&grub_btrfs_fs);
}