Skip to content

Commit 83564b0

Browse files
committed
erofs: improve Zstd, LZMA and DEFLATE error strings
Enable better, more detailed, and unique error reporting. Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
1 parent 831faab commit 83564b0

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

fs/erofs/decompressor_deflate.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ static int z_erofs_load_deflate_config(struct super_block *sb,
9797
return -ENOMEM;
9898
}
9999

100-
static int __z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
101-
struct page **pgpl)
100+
static const char *__z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
101+
struct page **pgpl)
102102
{
103103
struct super_block *sb = rq->sb;
104104
struct z_erofs_stream_dctx dctx = { .rq = rq, .no = -1, .ni = 0 };
105105
struct z_erofs_deflate *strm;
106+
const char *reason = NULL;
106107
int zerr, err;
107108

108109
/* 1. get the exact DEFLATE compressed size */
@@ -111,7 +112,7 @@ static int __z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
111112
min(rq->inputsize, sb->s_blocksize - rq->pageofs_in));
112113
if (err) {
113114
kunmap_local(dctx.kin);
114-
return err;
115+
return ERR_PTR(err);
115116
}
116117

117118
/* 2. get an available DEFLATE context */
@@ -129,7 +130,7 @@ static int __z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
129130
/* 3. multi-call decompress */
130131
zerr = zlib_inflateInit2(&strm->z, -MAX_WBITS);
131132
if (zerr != Z_OK) {
132-
err = -EIO;
133+
err = -EINVAL;
133134
goto failed_zinit;
134135
}
135136

@@ -157,6 +158,9 @@ static int __z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
157158
break;
158159
if (zerr == Z_STREAM_END && !rq->outputsize)
159160
break;
161+
reason = (zerr == Z_DATA_ERROR ?
162+
"corrupted compressed data" :
163+
"unexpected end of stream");
160164
err = -EFSCORRUPTED;
161165
break;
162166
}
@@ -173,7 +177,7 @@ static int __z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
173177
z_erofs_deflate_head = strm;
174178
spin_unlock(&z_erofs_deflate_lock);
175179
wake_up(&z_erofs_deflate_wq);
176-
return err;
180+
return reason ?: ERR_PTR(err);
177181
}
178182

179183
static const char *z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
@@ -189,7 +193,7 @@ static const char *z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
189193

190194
}
191195
#endif
192-
return ERR_PTR(__z_erofs_deflate_decompress(rq, pgpl));
196+
return __z_erofs_deflate_decompress(rq, pgpl);
193197
}
194198

195199
const struct z_erofs_decompressor z_erofs_deflate_decomp = {

fs/erofs/decompressor_lzma.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ static const char *z_erofs_lzma_decompress(struct z_erofs_decompress_req *rq,
154154
struct xz_buf buf = {};
155155
struct z_erofs_lzma *strm;
156156
enum xz_ret xz_err;
157+
const char *reason = NULL;
157158
int err;
158159

159160
/* 1. get the exact LZMA compressed size */
@@ -207,7 +208,9 @@ static const char *z_erofs_lzma_decompress(struct z_erofs_decompress_req *rq,
207208
if (xz_err != XZ_OK) {
208209
if (xz_err == XZ_STREAM_END && !rq->outputsize)
209210
break;
210-
err = -EFSCORRUPTED;
211+
reason = (xz_err == XZ_DATA_ERROR ?
212+
"corrupted compressed data" :
213+
"unexpected end of stream");
211214
break;
212215
}
213216
} while (1);
@@ -221,7 +224,7 @@ static const char *z_erofs_lzma_decompress(struct z_erofs_decompress_req *rq,
221224
z_erofs_lzma_head = strm;
222225
spin_unlock(&z_erofs_lzma_lock);
223226
wake_up(&z_erofs_lzma_wq);
224-
return ERR_PTR(err);
227+
return reason ?: ERR_PTR(err);
225228
}
226229

227230
const struct z_erofs_decompressor z_erofs_lzma_decomp = {

fs/erofs/decompressor_zstd.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ static const char *z_erofs_zstd_decompress(struct z_erofs_decompress_req *rq,
143143
zstd_in_buffer in_buf = { NULL, 0, 0 };
144144
zstd_out_buffer out_buf = { NULL, 0, 0 };
145145
struct z_erofs_zstd *strm;
146+
const char *reason = NULL;
146147
zstd_dstream *stream;
147148
int zerr, err;
148149

@@ -161,7 +162,7 @@ static const char *z_erofs_zstd_decompress(struct z_erofs_decompress_req *rq,
161162
/* 3. multi-call decompress */
162163
stream = zstd_init_dstream(z_erofs_zstd_max_dictsize, strm->wksp, strm->wkspsz);
163164
if (!stream) {
164-
err = -EIO;
165+
err = -ENOMEM;
165166
goto failed_zinit;
166167
}
167168

@@ -191,7 +192,8 @@ static const char *z_erofs_zstd_decompress(struct z_erofs_decompress_req *rq,
191192
if (zstd_is_error(zerr) ||
192193
((rq->outputsize + dctx.avail_out) && (!zerr || (zerr > 0 &&
193194
!(rq->inputsize + in_buf.size - in_buf.pos))))) {
194-
err = -EFSCORRUPTED;
195+
reason = zstd_is_error(zerr) ? zstd_get_error_name(zerr) :
196+
"unexpected end of stream";
195197
break;
196198
}
197199
} while (rq->outputsize + dctx.avail_out);
@@ -206,7 +208,7 @@ static const char *z_erofs_zstd_decompress(struct z_erofs_decompress_req *rq,
206208
z_erofs_zstd_head = strm;
207209
spin_unlock(&z_erofs_zstd_lock);
208210
wake_up(&z_erofs_zstd_wq);
209-
return ERR_PTR(err);
211+
return reason ?: ERR_PTR(err);
210212
}
211213

212214
const struct z_erofs_decompressor z_erofs_zstd_decomp = {

0 commit comments

Comments
 (0)