Skip to content

Commit 2a810ea

Browse files
committed
erofs: simplify z_erofs_load_compact_lcluster()
- Get rid of unpack_compacted_index() and fold it into z_erofs_load_compact_lcluster(); - Avoid a goto. Reviewed-by: Chao Yu <[email protected]> Signed-off-by: Gao Xiang <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent db90298 commit 2a810ea

File tree

1 file changed

+36
-53
lines changed

1 file changed

+36
-53
lines changed

fs/erofs/zmap.c

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,48 @@ static int get_compacted_la_distance(unsigned int lobits,
9797
return d1;
9898
}
9999

100-
static int unpack_compacted_index(struct z_erofs_maprecorder *m,
101-
unsigned int amortizedshift,
102-
erofs_off_t pos, bool lookahead)
100+
static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
101+
unsigned long lcn, bool lookahead)
103102
{
104-
struct erofs_inode *const vi = EROFS_I(m->inode);
103+
struct inode *const inode = m->inode;
104+
struct erofs_inode *const vi = EROFS_I(inode);
105+
const erofs_off_t ebase = sizeof(struct z_erofs_map_header) +
106+
ALIGN(erofs_iloc(inode) + vi->inode_isize + vi->xattr_isize, 8);
105107
const unsigned int lclusterbits = vi->z_logical_clusterbits;
108+
const unsigned int totalidx = erofs_iblks(inode);
109+
unsigned int compacted_4b_initial, compacted_2b, amortizedshift;
106110
unsigned int vcnt, lo, lobits, encodebits, nblk, bytes;
107-
bool big_pcluster;
111+
bool big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
112+
erofs_off_t pos;
108113
u8 *in, type;
109114
int i;
110115

116+
if (lcn >= totalidx || lclusterbits > 14)
117+
return -EINVAL;
118+
119+
m->lcn = lcn;
120+
/* used to align to 32-byte (compacted_2b) alignment */
121+
compacted_4b_initial = ((32 - ebase % 32) / 4) & 7;
122+
compacted_2b = 0;
123+
if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) &&
124+
compacted_4b_initial < totalidx)
125+
compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
126+
127+
pos = ebase;
128+
amortizedshift = 2; /* compact_4b */
129+
if (lcn >= compacted_4b_initial) {
130+
pos += compacted_4b_initial * 4;
131+
lcn -= compacted_4b_initial;
132+
if (lcn < compacted_2b) {
133+
amortizedshift = 1;
134+
} else {
135+
pos += compacted_2b * 2;
136+
lcn -= compacted_2b;
137+
}
138+
}
139+
pos += lcn * (1 << amortizedshift);
140+
141+
/* figure out the lcluster count in this pack */
111142
if (1 << amortizedshift == 4 && lclusterbits <= 14)
112143
vcnt = 2;
113144
else if (1 << amortizedshift == 2 && lclusterbits <= 12)
@@ -122,7 +153,6 @@ static int unpack_compacted_index(struct z_erofs_maprecorder *m,
122153
/* it doesn't equal to round_up(..) */
123154
m->nextpackoff = round_down(pos, vcnt << amortizedshift) +
124155
(vcnt << amortizedshift);
125-
big_pcluster = vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1;
126156
lobits = max(lclusterbits, ilog2(Z_EROFS_LI_D0_CBLKCNT) + 1U);
127157
encodebits = ((vcnt << amortizedshift) - sizeof(__le32)) * 8 / vcnt;
128158
bytes = pos & ((vcnt << amortizedshift) - 1);
@@ -207,53 +237,6 @@ static int unpack_compacted_index(struct z_erofs_maprecorder *m,
207237
return 0;
208238
}
209239

210-
static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
211-
unsigned long lcn, bool lookahead)
212-
{
213-
struct inode *const inode = m->inode;
214-
struct erofs_inode *const vi = EROFS_I(inode);
215-
const erofs_off_t ebase = sizeof(struct z_erofs_map_header) +
216-
ALIGN(erofs_iloc(inode) + vi->inode_isize + vi->xattr_isize, 8);
217-
unsigned int totalidx = erofs_iblks(inode);
218-
unsigned int compacted_4b_initial, compacted_2b;
219-
unsigned int amortizedshift;
220-
erofs_off_t pos;
221-
222-
if (lcn >= totalidx || vi->z_logical_clusterbits > 14)
223-
return -EINVAL;
224-
225-
m->lcn = lcn;
226-
/* used to align to 32-byte (compacted_2b) alignment */
227-
compacted_4b_initial = (32 - ebase % 32) / 4;
228-
if (compacted_4b_initial == 32 / 4)
229-
compacted_4b_initial = 0;
230-
231-
if ((vi->z_advise & Z_EROFS_ADVISE_COMPACTED_2B) &&
232-
compacted_4b_initial < totalidx)
233-
compacted_2b = rounddown(totalidx - compacted_4b_initial, 16);
234-
else
235-
compacted_2b = 0;
236-
237-
pos = ebase;
238-
if (lcn < compacted_4b_initial) {
239-
amortizedshift = 2;
240-
goto out;
241-
}
242-
pos += compacted_4b_initial * 4;
243-
lcn -= compacted_4b_initial;
244-
245-
if (lcn < compacted_2b) {
246-
amortizedshift = 1;
247-
goto out;
248-
}
249-
pos += compacted_2b * 2;
250-
lcn -= compacted_2b;
251-
amortizedshift = 2;
252-
out:
253-
pos += lcn * (1 << amortizedshift);
254-
return unpack_compacted_index(m, amortizedshift, pos, lookahead);
255-
}
256-
257240
static int z_erofs_load_lcluster_from_disk(struct z_erofs_maprecorder *m,
258241
unsigned int lcn, bool lookahead)
259242
{

0 commit comments

Comments
 (0)