Commit 2bdbc5d7 authored by Steven Whitehouse's avatar Steven Whitehouse

[GFS2] Directory code style changes

As per comments from Jan Engelhardt, remove redundant casts, redundant
endian conversions, add a smattering of const and rewrite the
dirent_next function in order to avoid as many casts as possible.

Cc: Jan Engelhardt <jengelh@linux01.gwdg.de>
Signed-off-by: default avatarSteven Whitehouse <swhiteho@redhat.com>
parent 5acd3967
...@@ -81,9 +81,10 @@ ...@@ -81,9 +81,10 @@
#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1) #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1)) #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
typedef int (*leaf_call_t) (struct gfs2_inode *dip, typedef int (*leaf_call_t) (struct gfs2_inode *dip, u32 index, u32 len,
u32 index, u32 len, u64 leaf_no, u64 leaf_no, void *data);
void *data); typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
const struct qstr *name, void *opaque);
int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block, int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
...@@ -348,17 +349,13 @@ static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, ...@@ -348,17 +349,13 @@ static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
return (copied) ? copied : error; return (copied) ? copied : error;
} }
typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
const struct qstr *name,
void *opaque);
static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent, static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
const struct qstr *name, int ret) const struct qstr *name, int ret)
{ {
if (dent->de_inum.no_addr != 0 && if (dent->de_inum.no_addr != 0 &&
be32_to_cpu(dent->de_hash) == name->hash && be32_to_cpu(dent->de_hash) == name->hash &&
be16_to_cpu(dent->de_name_len) == name->len && be16_to_cpu(dent->de_name_len) == name->len &&
memcmp((char *)(dent+1), name->name, name->len) == 0) memcmp(dent+1, name->name, name->len) == 0)
return ret; return ret;
return 0; return 0;
} }
...@@ -483,8 +480,7 @@ static int gfs2_dirent_offset(const void *buf) ...@@ -483,8 +480,7 @@ static int gfs2_dirent_offset(const void *buf)
return -1; return -1;
} }
static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
void *buf,
unsigned int len, gfs2_dscan_t scan, unsigned int len, gfs2_dscan_t scan,
const struct qstr *name, const struct qstr *name,
void *opaque) void *opaque)
...@@ -500,7 +496,7 @@ static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, ...@@ -500,7 +496,7 @@ static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
offset = ret; offset = ret;
prev = NULL; prev = NULL;
dent = (struct gfs2_dirent *)(buf + offset); dent = buf + offset;
size = be16_to_cpu(dent->de_rec_len); size = be16_to_cpu(dent->de_rec_len);
if (gfs2_check_dirent(dent, offset, size, len, 1)) if (gfs2_check_dirent(dent, offset, size, len, 1))
goto consist_inode; goto consist_inode;
...@@ -512,7 +508,7 @@ static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, ...@@ -512,7 +508,7 @@ static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
if (offset == len) if (offset == len)
break; break;
prev = dent; prev = dent;
dent = (struct gfs2_dirent *)(buf + offset); dent = buf + offset;
size = be16_to_cpu(dent->de_rec_len); size = be16_to_cpu(dent->de_rec_len);
if (gfs2_check_dirent(dent, offset, size, len, 0)) if (gfs2_check_dirent(dent, offset, size, len, 0))
goto consist_inode; goto consist_inode;
...@@ -567,6 +563,24 @@ static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh, ...@@ -567,6 +563,24 @@ static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
} }
} }
static int dirent_check_reclen(struct gfs2_inode *dip,
const struct gfs2_dirent *d, const void *end_p)
{
const void *ptr = d;
u16 rec_len = be16_to_cpu(d->de_rec_len);
if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
goto broken;
ptr += rec_len;
if (ptr < end_p)
return rec_len;
if (ptr == end_p)
return -ENOENT;
broken:
gfs2_consist_inode(dip);
return -EIO;
}
/** /**
* dirent_next - Next dirent * dirent_next - Next dirent
* @dip: the directory * @dip: the directory
...@@ -579,33 +593,18 @@ static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh, ...@@ -579,33 +593,18 @@ static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh, static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
struct gfs2_dirent **dent) struct gfs2_dirent **dent)
{ {
struct gfs2_dirent *tmp, *cur; struct gfs2_dirent *cur = *dent, *tmp;
char *bh_end; char *bh_end = bh->b_data + bh->b_size;
u16 cur_rec_len; int ret;
cur = *dent;
bh_end = bh->b_data + bh->b_size;
cur_rec_len = be16_to_cpu(cur->de_rec_len);
if ((char *)cur + cur_rec_len >= bh_end) {
if ((char *)cur + cur_rec_len > bh_end) {
gfs2_consist_inode(dip);
return -EIO;
}
return -ENOENT;
}
tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) { ret = dirent_check_reclen(dip, cur, bh_end);
gfs2_consist_inode(dip); if (ret < 0)
return -EIO; return ret;
}
if (cur_rec_len == 0) { tmp = (void *)cur + ret;
gfs2_consist_inode(dip); ret = dirent_check_reclen(dip, tmp, bh_end);
return -EIO; if (ret == -EIO)
} return ret;
/* Only the first dent could ever have de_inum.no_addr == 0 */ /* Only the first dent could ever have de_inum.no_addr == 0 */
if (!tmp->de_inum.no_addr) { if (!tmp->de_inum.no_addr) {
...@@ -614,7 +613,6 @@ static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh, ...@@ -614,7 +613,6 @@ static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
} }
*dent = tmp; *dent = tmp;
return 0; return 0;
} }
...@@ -821,9 +819,9 @@ static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, ...@@ -821,9 +819,9 @@ static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh,
gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF); gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
leaf = (struct gfs2_leaf *)bh->b_data; leaf = (struct gfs2_leaf *)bh->b_data;
leaf->lf_depth = cpu_to_be16(depth); leaf->lf_depth = cpu_to_be16(depth);
leaf->lf_entries = cpu_to_be16(0); leaf->lf_entries = 0;
leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE); leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
leaf->lf_next = cpu_to_be64(0); leaf->lf_next = 0;
memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved)); memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
dent = (struct gfs2_dirent *)(leaf+1); dent = (struct gfs2_dirent *)(leaf+1);
gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent); gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
...@@ -1152,14 +1150,14 @@ static int dir_double_exhash(struct gfs2_inode *dip) ...@@ -1152,14 +1150,14 @@ static int dir_double_exhash(struct gfs2_inode *dip)
static int compare_dents(const void *a, const void *b) static int compare_dents(const void *a, const void *b)
{ {
struct gfs2_dirent *dent_a, *dent_b; const struct gfs2_dirent *dent_a, *dent_b;
u32 hash_a, hash_b; u32 hash_a, hash_b;
int ret = 0; int ret = 0;
dent_a = *(struct gfs2_dirent **)a; dent_a = *(const struct gfs2_dirent **)a;
hash_a = be32_to_cpu(dent_a->de_hash); hash_a = be32_to_cpu(dent_a->de_hash);
dent_b = *(struct gfs2_dirent **)b; dent_b = *(const struct gfs2_dirent **)b;
hash_b = be32_to_cpu(dent_b->de_hash); hash_b = be32_to_cpu(dent_b->de_hash);
if (hash_a > hash_b) if (hash_a > hash_b)
...@@ -1175,9 +1173,7 @@ static int compare_dents(const void *a, const void *b) ...@@ -1175,9 +1173,7 @@ static int compare_dents(const void *a, const void *b)
else if (len_a < len_b) else if (len_a < len_b)
ret = -1; ret = -1;
else else
ret = memcmp((char *)(dent_a + 1), ret = memcmp(dent_a + 1, dent_b + 1, len_a);
(char *)(dent_b + 1),
len_a);
} }
return ret; return ret;
...@@ -1246,7 +1242,7 @@ static int do_filldir_main(struct gfs2_inode *dip, u64 *offset, ...@@ -1246,7 +1242,7 @@ static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
gfs2_inum_in(&inum, (char *)&dent->de_inum); gfs2_inum_in(&inum, (char *)&dent->de_inum);
error = filldir(opaque, (char *)(dent + 1), error = filldir(opaque, (const char *)(dent + 1),
be16_to_cpu(dent->de_name_len), be16_to_cpu(dent->de_name_len),
off, &inum, off, &inum,
be16_to_cpu(dent->de_type)); be16_to_cpu(dent->de_type));
...@@ -1298,7 +1294,7 @@ static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque, ...@@ -1298,7 +1294,7 @@ static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
return 0; return 0;
error = -ENOMEM; error = -ENOMEM;
larr = vmalloc((leaves + entries) * sizeof(void*)); larr = vmalloc((leaves + entries) * sizeof(void *));
if (!larr) if (!larr)
goto out; goto out;
darr = (const struct gfs2_dirent **)(larr + leaves); darr = (const struct gfs2_dirent **)(larr + leaves);
......
...@@ -46,7 +46,7 @@ int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block, ...@@ -46,7 +46,7 @@ int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
static inline u32 gfs2_disk_hash(const char *data, int len) static inline u32 gfs2_disk_hash(const char *data, int len)
{ {
return crc32_le(0xFFFFFFFF, data, len) ^ 0xFFFFFFFF; return crc32_le((u32)~0, data, len) ^ (u32)~0;
} }
...@@ -67,7 +67,7 @@ static inline void gfs2_qstr2dirent(const struct qstr *name, u16 reclen, struct ...@@ -67,7 +67,7 @@ static inline void gfs2_qstr2dirent(const struct qstr *name, u16 reclen, struct
dent->de_name_len = cpu_to_be16(name->len); dent->de_name_len = cpu_to_be16(name->len);
dent->de_type = cpu_to_be16(0); dent->de_type = cpu_to_be16(0);
memset(dent->__pad, 0, sizeof(dent->__pad)); memset(dent->__pad, 0, sizeof(dent->__pad));
memcpy((char*)(dent+1), name->name, name->len); memcpy(dent + 1, name->name, name->len);
} }
#endif /* __DIR_DOT_H__ */ #endif /* __DIR_DOT_H__ */
...@@ -190,8 +190,7 @@ static void gfs2_write_super_lockfs(struct super_block *sb) ...@@ -190,8 +190,7 @@ static void gfs2_write_super_lockfs(struct super_block *sb)
static void gfs2_unlockfs(struct super_block *sb) static void gfs2_unlockfs(struct super_block *sb)
{ {
struct gfs2_sbd *sdp = sb->s_fs_info; gfs2_unfreeze_fs(sb->s_fs_info);
gfs2_unfreeze_fs(sdp);
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment