Skip to content

Commit 3a39c18

Browse files
author
Li Zefan
committed
btrfs: Extract duplicate decompress code
Add a common function to copy decompressed data from working buffer to bio pages. Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
1 parent 1a419d8 commit 3a39c18

4 files changed

Lines changed: 115 additions & 194 deletions

File tree

‎fs/btrfs/compression.c‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,95 @@ void __exit btrfs_exit_compress(void)
904904
{
905905
free_workspaces();
906906
}
907+
908+
/*
909+
* Copy uncompressed data from working buffer to pages.
910+
*
911+
* buf_start is the byte offset we're of the start of our workspace buffer.
912+
*
913+
* total_out is the last byte of the buffer
914+
*/
915+
int btrfs_decompress_buf2page(char *buf, unsigned long buf_start,
916+
unsigned long total_out, u64 disk_start,
917+
struct bio_vec *bvec, int vcnt,
918+
unsigned long *page_index,
919+
unsigned long *pg_offset)
920+
{
921+
unsigned long buf_offset;
922+
unsigned long current_buf_start;
923+
unsigned long start_byte;
924+
unsigned long working_bytes = total_out - buf_start;
925+
unsigned long bytes;
926+
char *kaddr;
927+
struct page *page_out = bvec[*page_index].bv_page;
928+
929+
/*
930+
* start byte is the first byte of the page we're currently
931+
* copying into relative to the start of the compressed data.
932+
*/
933+
start_byte = page_offset(page_out) - disk_start;
934+
935+
/* we haven't yet hit data corresponding to this page */
936+
if (total_out <= start_byte)
937+
return 1;
938+
939+
/*
940+
* the start of the data we care about is offset into
941+
* the middle of our working buffer
942+
*/
943+
if (total_out > start_byte && buf_start < start_byte) {
944+
buf_offset = start_byte - buf_start;
945+
working_bytes -= buf_offset;
946+
} else {
947+
buf_offset = 0;
948+
}
949+
current_buf_start = buf_start;
950+
951+
/* copy bytes from the working buffer into the pages */
952+
while (working_bytes > 0) {
953+
bytes = min(PAGE_CACHE_SIZE - *pg_offset,
954+
PAGE_CACHE_SIZE - buf_offset);
955+
bytes = min(bytes, working_bytes);
956+
kaddr = kmap_atomic(page_out, KM_USER0);
957+
memcpy(kaddr + *pg_offset, buf + buf_offset, bytes);
958+
kunmap_atomic(kaddr, KM_USER0);
959+
flush_dcache_page(page_out);
960+
961+
*pg_offset += bytes;
962+
buf_offset += bytes;
963+
working_bytes -= bytes;
964+
current_buf_start += bytes;
965+
966+
/* check if we need to pick another page */
967+
if (*pg_offset == PAGE_CACHE_SIZE) {
968+
(*page_index)++;
969+
if (*page_index >= vcnt)
970+
return 0;
971+
972+
page_out = bvec[*page_index].bv_page;
973+
*pg_offset = 0;
974+
start_byte = page_offset(page_out) - disk_start;
975+
976+
/*
977+
* make sure our new page is covered by this
978+
* working buffer
979+
*/
980+
if (total_out <= start_byte)
981+
return 1;
982+
983+
/*
984+
* the next page in the biovec might not be adjacent
985+
* to the last page, but it might still be found
986+
* inside this working buffer. bump our offset pointer
987+
*/
988+
if (total_out > start_byte &&
989+
current_buf_start < start_byte) {
990+
buf_offset = start_byte - buf_start;
991+
working_bytes = total_out - start_byte;
992+
current_buf_start = buf_start + buf_offset;
993+
}
994+
}
995+
}
996+
997+
return 1;
998+
}

‎fs/btrfs/compression.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ int btrfs_decompress_biovec(int type, struct page **pages_in, u64 disk_start,
3434
struct bio_vec *bvec, int vcnt, size_t srclen);
3535
int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
3636
unsigned long start_byte, size_t srclen, size_t destlen);
37+
int btrfs_decompress_buf2page(char *buf, unsigned long buf_start,
38+
unsigned long total_out, u64 disk_start,
39+
struct bio_vec *bvec, int vcnt,
40+
unsigned long *page_index,
41+
unsigned long *pg_offset);
3742

3843
int btrfs_submit_compressed_write(struct inode *inode, u64 start,
3944
unsigned long len, u64 disk_start,

‎fs/btrfs/lzo.c‎

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -260,22 +260,17 @@ static int lzo_decompress_biovec(struct list_head *ws,
260260
size_t srclen)
261261
{
262262
struct workspace *workspace = list_entry(ws, struct workspace, list);
263-
int ret = 0;
263+
int ret = 0, ret2;
264264
char *data_in;
265-
unsigned long page_bytes_left;
266265
unsigned long page_in_index = 0;
267266
unsigned long page_out_index = 0;
268-
struct page *page_out;
269267
unsigned long total_pages_in = (srclen + PAGE_CACHE_SIZE - 1) /
270268
PAGE_CACHE_SIZE;
271269
unsigned long buf_start;
272270
unsigned long buf_offset = 0;
273271
unsigned long bytes;
274272
unsigned long working_bytes;
275273
unsigned long pg_offset;
276-
unsigned long start_byte;
277-
unsigned long current_buf_start;
278-
char *kaddr;
279274

280275
size_t in_len;
281276
size_t out_len;
@@ -295,8 +290,6 @@ static int lzo_decompress_biovec(struct list_head *ws,
295290
in_page_bytes_left = PAGE_CACHE_SIZE - LZO_LEN;
296291

297292
tot_out = 0;
298-
page_out = bvec[0].bv_page;
299-
page_bytes_left = PAGE_CACHE_SIZE;
300293
pg_offset = 0;
301294

302295
while (tot_in < tot_len) {
@@ -359,97 +352,15 @@ static int lzo_decompress_biovec(struct list_head *ws,
359352
break;
360353
}
361354

362-
/*
363-
* buf start is the byte offset we're of the start of
364-
* our workspace buffer
365-
*/
366355
buf_start = tot_out;
367-
368-
/* tot_out is the last byte of the workspace buffer */
369356
tot_out += out_len;
370357

371-
working_bytes = tot_out - buf_start;
372-
373-
/*
374-
* start_byte is the first byte of the page we're currently
375-
* copying into relative to the start of the compressed data.
376-
*/
377-
start_byte = page_offset(page_out) - disk_start;
378-
379-
if (working_bytes == 0) {
380-
/* we didn't make progress in this inflate
381-
* call, we're done
382-
*/
358+
ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
359+
tot_out, disk_start,
360+
bvec, vcnt,
361+
&page_out_index, &pg_offset);
362+
if (ret2 == 0)
383363
break;
384-
}
385-
386-
/* we haven't yet hit data corresponding to this page */
387-
if (tot_out <= start_byte)
388-
continue;
389-
390-
/*
391-
* the start of the data we care about is offset into
392-
* the middle of our working buffer
393-
*/
394-
if (tot_out > start_byte && buf_start < start_byte) {
395-
buf_offset = start_byte - buf_start;
396-
working_bytes -= buf_offset;
397-
} else {
398-
buf_offset = 0;
399-
}
400-
current_buf_start = buf_start;
401-
402-
/* copy bytes from the working buffer into the pages */
403-
while (working_bytes > 0) {
404-
bytes = min(PAGE_CACHE_SIZE - pg_offset,
405-
PAGE_CACHE_SIZE - buf_offset);
406-
bytes = min(bytes, working_bytes);
407-
kaddr = kmap_atomic(page_out, KM_USER0);
408-
memcpy(kaddr + pg_offset, workspace->buf + buf_offset,
409-
bytes);
410-
kunmap_atomic(kaddr, KM_USER0);
411-
flush_dcache_page(page_out);
412-
413-
pg_offset += bytes;
414-
page_bytes_left -= bytes;
415-
buf_offset += bytes;
416-
working_bytes -= bytes;
417-
current_buf_start += bytes;
418-
419-
/* check if we need to pick another page */
420-
if (page_bytes_left == 0) {
421-
page_out_index++;
422-
if (page_out_index >= vcnt) {
423-
ret = 0;
424-
goto done;
425-
}
426-
427-
page_out = bvec[page_out_index].bv_page;
428-
pg_offset = 0;
429-
page_bytes_left = PAGE_CACHE_SIZE;
430-
start_byte = page_offset(page_out) - disk_start;
431-
432-
/*
433-
* make sure our new page is covered by this
434-
* working buffer
435-
*/
436-
if (tot_out <= start_byte)
437-
break;
438-
439-
/* the next page in the biovec might not
440-
* be adjacent to the last page, but it
441-
* might still be found inside this working
442-
* buffer. bump our offset pointer
443-
*/
444-
if (tot_out > start_byte &&
445-
current_buf_start < start_byte) {
446-
buf_offset = start_byte - buf_start;
447-
working_bytes = tot_out - start_byte;
448-
current_buf_start = buf_start +
449-
buf_offset;
450-
}
451-
}
452-
}
453364
}
454365
done:
455366
if (data_in)

‎fs/btrfs/zlib.c‎

Lines changed: 12 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -218,24 +218,16 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in,
218218
size_t srclen)
219219
{
220220
struct workspace *workspace = list_entry(ws, struct workspace, list);
221-
int ret = 0;
221+
int ret = 0, ret2;
222222
int wbits = MAX_WBITS;
223223
char *data_in;
224224
size_t total_out = 0;
225-
unsigned long page_bytes_left;
226225
unsigned long page_in_index = 0;
227226
unsigned long page_out_index = 0;
228-
struct page *page_out;
229227
unsigned long total_pages_in = (srclen + PAGE_CACHE_SIZE - 1) /
230228
PAGE_CACHE_SIZE;
231229
unsigned long buf_start;
232-
unsigned long buf_offset;
233-
unsigned long bytes;
234-
unsigned long working_bytes;
235230
unsigned long pg_offset;
236-
unsigned long start_byte;
237-
unsigned long current_buf_start;
238-
char *kaddr;
239231

240232
data_in = kmap(pages_in[page_in_index]);
241233
workspace->inf_strm.next_in = data_in;
@@ -245,8 +237,6 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in,
245237
workspace->inf_strm.total_out = 0;
246238
workspace->inf_strm.next_out = workspace->buf;
247239
workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
248-
page_out = bvec[page_out_index].bv_page;
249-
page_bytes_left = PAGE_CACHE_SIZE;
250240
pg_offset = 0;
251241

252242
/* If it's deflate, and it's got no preset dictionary, then
@@ -268,100 +258,23 @@ static int zlib_decompress_biovec(struct list_head *ws, struct page **pages_in,
268258
ret = zlib_inflate(&workspace->inf_strm, Z_NO_FLUSH);
269259
if (ret != Z_OK && ret != Z_STREAM_END)
270260
break;
271-
/*
272-
* buf start is the byte offset we're of the start of
273-
* our workspace buffer
274-
*/
275-
buf_start = total_out;
276261

277-
/* total_out is the last byte of the workspace buffer */
262+
buf_start = total_out;
278263
total_out = workspace->inf_strm.total_out;
279264

280-
working_bytes = total_out - buf_start;
281-
282-
/*
283-
* start byte is the first byte of the page we're currently
284-
* copying into relative to the start of the compressed data.
285-
*/
286-
start_byte = page_offset(page_out) - disk_start;
287-
288-
if (working_bytes == 0) {
289-
/* we didn't make progress in this inflate
290-
* call, we're done
291-
*/
292-
if (ret != Z_STREAM_END)
293-
ret = -1;
265+
/* we didn't make progress in this inflate call, we're done */
266+
if (buf_start == total_out)
294267
break;
295-
}
296268

297-
/* we haven't yet hit data corresponding to this page */
298-
if (total_out <= start_byte)
299-
goto next;
300-
301-
/*
302-
* the start of the data we care about is offset into
303-
* the middle of our working buffer
304-
*/
305-
if (total_out > start_byte && buf_start < start_byte) {
306-
buf_offset = start_byte - buf_start;
307-
working_bytes -= buf_offset;
308-
} else {
309-
buf_offset = 0;
310-
}
311-
current_buf_start = buf_start;
312-
313-
/* copy bytes from the working buffer into the pages */
314-
while (working_bytes > 0) {
315-
bytes = min(PAGE_CACHE_SIZE - pg_offset,
316-
PAGE_CACHE_SIZE - buf_offset);
317-
bytes = min(bytes, working_bytes);
318-
kaddr = kmap_atomic(page_out, KM_USER0);
319-
memcpy(kaddr + pg_offset, workspace->buf + buf_offset,
320-
bytes);
321-
kunmap_atomic(kaddr, KM_USER0);
322-
flush_dcache_page(page_out);
323-
324-
pg_offset += bytes;
325-
page_bytes_left -= bytes;
326-
buf_offset += bytes;
327-
working_bytes -= bytes;
328-
current_buf_start += bytes;
329-
330-
/* check if we need to pick another page */
331-
if (page_bytes_left == 0) {
332-
page_out_index++;
333-
if (page_out_index >= vcnt) {
334-
ret = 0;
335-
goto done;
336-
}
337-
338-
page_out = bvec[page_out_index].bv_page;
339-
pg_offset = 0;
340-
page_bytes_left = PAGE_CACHE_SIZE;
341-
start_byte = page_offset(page_out) - disk_start;
342-
343-
/*
344-
* make sure our new page is covered by this
345-
* working buffer
346-
*/
347-
if (total_out <= start_byte)
348-
goto next;
349-
350-
/* the next page in the biovec might not
351-
* be adjacent to the last page, but it
352-
* might still be found inside this working
353-
* buffer. bump our offset pointer
354-
*/
355-
if (total_out > start_byte &&
356-
current_buf_start < start_byte) {
357-
buf_offset = start_byte - buf_start;
358-
working_bytes = total_out - start_byte;
359-
current_buf_start = buf_start +
360-
buf_offset;
361-
}
362-
}
269+
ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
270+
total_out, disk_start,
271+
bvec, vcnt,
272+
&page_out_index, &pg_offset);
273+
if (ret2 == 0) {
274+
ret = 0;
275+
goto done;
363276
}
364-
next:
277+
365278
workspace->inf_strm.next_out = workspace->buf;
366279
workspace->inf_strm.avail_out = PAGE_CACHE_SIZE;
367280

0 commit comments

Comments
 (0)