Skip to content

Commit d937c81

Browse files
committed
Merge patch series "Squashfs: Update code to not use page->index"
Phillip Lougher <phillip@squashfs.org.uk> says: In the near future page->index will be removed [1]. Any code which still uses page->index needs to be updated. This patch-set contains 4 patches which updates most of the code in Squashfs. The exceptions are functions which have been fixed in recent patches [2] & [3]. [1]: https://lore.kernel.org/all/Zp8fgUSIBGQ1TN0D@casper.infradead.org/ [2]: https://lore.kernel.org/all/20240817101146.2347378-1-lizetao1@huawei.com/ [3]: https://lore.kernel.org/all/20240817101146.2347378-2-lizetao1@huawei.com/ * patchesf from https://lore.kernel.org/r/20240818235847.170468-1-phillip@squashfs.org.uk: Squashfs: Rewrite and update squashfs_readahead_fragment() to not use page->index Squashfs: Update squashfs_readpage_block() to not use page->index Squashfs: Update squashfs_readahead() to not use page->index Squashfs: Update page_actor to not use page->index Link: https://lore.kernel.org/r/20240818235847.170468-1-phillip@squashfs.org.uk Signed-off-by: Christian Brauner <brauner@kernel.org>
2 parents 424f8cb + fd54fa6 commit d937c81

File tree

4 files changed

+76
-39
lines changed

4 files changed

+76
-39
lines changed

fs/squashfs/file.c

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -494,39 +494,73 @@ static int squashfs_read_folio(struct file *file, struct folio *folio)
494494
}
495495

496496
static int squashfs_readahead_fragment(struct page **page,
497-
unsigned int pages, unsigned int expected)
497+
unsigned int pages, unsigned int expected, loff_t start)
498498
{
499499
struct inode *inode = page[0]->mapping->host;
500500
struct squashfs_cache_entry *buffer = squashfs_get_fragment(inode->i_sb,
501501
squashfs_i(inode)->fragment_block,
502502
squashfs_i(inode)->fragment_size);
503503
struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
504-
unsigned int n, mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
505-
int error = buffer->error;
504+
int i, bytes, copied;
505+
struct squashfs_page_actor *actor;
506+
unsigned int offset;
507+
void *addr;
508+
struct page *last_page;
509+
510+
if (buffer->error)
511+
goto out;
506512

507-
if (error)
513+
actor = squashfs_page_actor_init_special(msblk, page, pages,
514+
expected, start);
515+
if (!actor)
508516
goto out;
509517

510-
expected += squashfs_i(inode)->fragment_offset;
518+
squashfs_actor_nobuff(actor);
519+
addr = squashfs_first_page(actor);
520+
521+
for (copied = offset = 0; offset < expected; offset += PAGE_SIZE) {
522+
int avail = min_t(int, expected - offset, PAGE_SIZE);
523+
524+
if (!IS_ERR(addr)) {
525+
bytes = squashfs_copy_data(addr, buffer, offset +
526+
squashfs_i(inode)->fragment_offset, avail);
527+
528+
if (bytes != avail)
529+
goto failed;
530+
}
531+
532+
copied += avail;
533+
addr = squashfs_next_page(actor);
534+
}
511535

512-
for (n = 0; n < pages; n++) {
513-
unsigned int base = (page[n]->index & mask) << PAGE_SHIFT;
514-
unsigned int offset = base + squashfs_i(inode)->fragment_offset;
536+
last_page = squashfs_page_actor_free(actor);
515537

516-
if (expected > offset) {
517-
unsigned int avail = min_t(unsigned int, expected -
518-
offset, PAGE_SIZE);
538+
if (copied == expected) {
539+
/* Last page (if present) may have trailing bytes not filled */
540+
bytes = copied % PAGE_SIZE;
541+
if (bytes && last_page)
542+
memzero_page(last_page, bytes, PAGE_SIZE - bytes);
519543

520-
squashfs_fill_page(page[n], buffer, offset, avail);
544+
for (i = 0; i < pages; i++) {
545+
flush_dcache_page(page[i]);
546+
SetPageUptodate(page[i]);
521547
}
548+
}
522549

523-
unlock_page(page[n]);
524-
put_page(page[n]);
550+
for (i = 0; i < pages; i++) {
551+
unlock_page(page[i]);
552+
put_page(page[i]);
525553
}
526554

555+
squashfs_cache_put(buffer);
556+
return 0;
557+
558+
failed:
559+
squashfs_page_actor_free(actor);
560+
527561
out:
528562
squashfs_cache_put(buffer);
529-
return error;
563+
return 1;
530564
}
531565

532566
static void squashfs_readahead(struct readahead_control *ractl)
@@ -551,7 +585,6 @@ static void squashfs_readahead(struct readahead_control *ractl)
551585
return;
552586

553587
for (;;) {
554-
pgoff_t index;
555588
int res, bsize;
556589
u64 block = 0;
557590
unsigned int expected;
@@ -570,26 +603,21 @@ static void squashfs_readahead(struct readahead_control *ractl)
570603
if (readahead_pos(ractl) >= i_size_read(inode))
571604
goto skip_pages;
572605

573-
index = pages[0]->index >> shift;
574-
575-
if ((pages[nr_pages - 1]->index >> shift) != index)
576-
goto skip_pages;
577-
578-
if (index == file_end && squashfs_i(inode)->fragment_block !=
579-
SQUASHFS_INVALID_BLK) {
606+
if (start >> msblk->block_log == file_end &&
607+
squashfs_i(inode)->fragment_block != SQUASHFS_INVALID_BLK) {
580608
res = squashfs_readahead_fragment(pages, nr_pages,
581-
expected);
609+
expected, start);
582610
if (res)
583611
goto skip_pages;
584612
continue;
585613
}
586614

587-
bsize = read_blocklist(inode, index, &block);
615+
bsize = read_blocklist(inode, start >> msblk->block_log, &block);
588616
if (bsize == 0)
589617
goto skip_pages;
590618

591619
actor = squashfs_page_actor_init_special(msblk, pages, nr_pages,
592-
expected);
620+
expected, start);
593621
if (!actor)
594622
goto skip_pages;
595623

@@ -602,7 +630,7 @@ static void squashfs_readahead(struct readahead_control *ractl)
602630

603631
/* Last page (if present) may have trailing bytes not filled */
604632
bytes = res % PAGE_SIZE;
605-
if (index == file_end && bytes && last_page)
633+
if (start >> msblk->block_log == file_end && bytes && last_page)
606634
memzero_page(last_page, bytes,
607635
PAGE_SIZE - bytes);
608636

@@ -616,6 +644,8 @@ static void squashfs_readahead(struct readahead_control *ractl)
616644
unlock_page(pages[i]);
617645
put_page(pages[i]);
618646
}
647+
648+
start += readahead_batch_length(ractl);
619649
}
620650

621651
kfree(pages);

fs/squashfs/file_direct.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
2323
int expected)
2424

2525
{
26+
struct folio *folio = page_folio(target_page);
2627
struct inode *inode = target_page->mapping->host;
2728
struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
28-
2929
loff_t file_end = (i_size_read(inode) - 1) >> PAGE_SHIFT;
3030
int mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
31-
loff_t start_index = target_page->index & ~mask;
31+
loff_t start_index = folio->index & ~mask;
3232
loff_t end_index = start_index | mask;
3333
int i, n, pages, bytes, res = -ENOMEM;
34-
struct page **page;
34+
struct page **page, *last_page;
3535
struct squashfs_page_actor *actor;
3636
void *pageaddr;
3737

@@ -46,7 +46,7 @@ int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
4646

4747
/* Try to grab all the pages covered by the Squashfs block */
4848
for (i = 0, n = start_index; n <= end_index; n++) {
49-
page[i] = (n == target_page->index) ? target_page :
49+
page[i] = (n == folio->index) ? target_page :
5050
grab_cache_page_nowait(target_page->mapping, n);
5151

5252
if (page[i] == NULL)
@@ -67,14 +67,15 @@ int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
6767
* Create a "page actor" which will kmap and kunmap the
6868
* page cache pages appropriately within the decompressor
6969
*/
70-
actor = squashfs_page_actor_init_special(msblk, page, pages, expected);
70+
actor = squashfs_page_actor_init_special(msblk, page, pages, expected,
71+
start_index << PAGE_SHIFT);
7172
if (actor == NULL)
7273
goto out;
7374

7475
/* Decompress directly into the page cache buffers */
7576
res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor);
7677

77-
squashfs_page_actor_free(actor);
78+
last_page = squashfs_page_actor_free(actor);
7879

7980
if (res < 0)
8081
goto mark_errored;
@@ -86,8 +87,8 @@ int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
8687

8788
/* Last page (if present) may have trailing bytes not filled */
8889
bytes = res % PAGE_SIZE;
89-
if (page[pages - 1]->index == end_index && bytes) {
90-
pageaddr = kmap_local_page(page[pages - 1]);
90+
if (end_index == file_end && last_page && bytes) {
91+
pageaddr = kmap_local_page(last_page);
9192
memset(pageaddr + bytes, 0, PAGE_SIZE - bytes);
9293
kunmap_local(pageaddr);
9394
}

fs/squashfs/page_actor.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ struct squashfs_page_actor *squashfs_page_actor_init(void **buffer,
6060
}
6161

6262
/* Implementation of page_actor for decompressing directly into page cache. */
63+
static loff_t page_next_index(struct squashfs_page_actor *actor)
64+
{
65+
return page_folio(actor->page[actor->next_page])->index;
66+
}
67+
6368
static void *handle_next_page(struct squashfs_page_actor *actor)
6469
{
6570
int max_pages = (actor->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
@@ -68,7 +73,7 @@ static void *handle_next_page(struct squashfs_page_actor *actor)
6873
return NULL;
6974

7075
if ((actor->next_page == actor->pages) ||
71-
(actor->next_index != actor->page[actor->next_page]->index)) {
76+
(actor->next_index != page_next_index(actor))) {
7277
actor->next_index++;
7378
actor->returned_pages++;
7479
actor->last_page = NULL;
@@ -103,7 +108,7 @@ static void direct_finish_page(struct squashfs_page_actor *actor)
103108
}
104109

105110
struct squashfs_page_actor *squashfs_page_actor_init_special(struct squashfs_sb_info *msblk,
106-
struct page **page, int pages, int length)
111+
struct page **page, int pages, int length, loff_t start_index)
107112
{
108113
struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
109114

@@ -125,7 +130,7 @@ struct squashfs_page_actor *squashfs_page_actor_init_special(struct squashfs_sb_
125130
actor->pages = pages;
126131
actor->next_page = 0;
127132
actor->returned_pages = 0;
128-
actor->next_index = page[0]->index & ~((1 << (msblk->block_log - PAGE_SHIFT)) - 1);
133+
actor->next_index = start_index >> PAGE_SHIFT;
129134
actor->pageaddr = NULL;
130135
actor->last_page = NULL;
131136
actor->alloc_buffer = msblk->decompressor->alloc_buffer;

fs/squashfs/page_actor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ extern struct squashfs_page_actor *squashfs_page_actor_init(void **buffer,
2929
int pages, int length);
3030
extern struct squashfs_page_actor *squashfs_page_actor_init_special(
3131
struct squashfs_sb_info *msblk,
32-
struct page **page, int pages, int length);
32+
struct page **page, int pages, int length,
33+
loff_t start_index);
3334
static inline struct page *squashfs_page_actor_free(struct squashfs_page_actor *actor)
3435
{
3536
struct page *last_page = actor->last_page;

0 commit comments

Comments
 (0)