Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) 2018 HUAWEI, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *             https://www.huawei.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Created by Gao Xiang <gaoxiang25@huawei.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "zdata.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "compress.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/prefetch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <trace/events/erofs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * since pclustersize is variable for big pcluster feature, introduce slab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * pools implementation for different pcluster sizes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) struct z_erofs_pcluster_slab {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 	struct kmem_cache *slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 	unsigned int maxpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 	char name[48];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define _PCLP(n) { .maxpages = n }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) static struct z_erofs_pcluster_slab pcluster_pool[] __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 	_PCLP(1), _PCLP(4), _PCLP(16), _PCLP(64), _PCLP(128),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	_PCLP(Z_EROFS_PCLUSTER_MAX_PAGES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) static void z_erofs_destroy_pcluster_pool(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 		if (!pcluster_pool[i].slab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 		kmem_cache_destroy(pcluster_pool[i].slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 		pcluster_pool[i].slab = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) static int z_erofs_create_pcluster_pool(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	struct z_erofs_pcluster_slab *pcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	struct z_erofs_pcluster *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	for (pcs = pcluster_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	     pcs < pcluster_pool + ARRAY_SIZE(pcluster_pool); ++pcs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 		size = struct_size(a, compressed_pages, pcs->maxpages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 		sprintf(pcs->name, "erofs_pcluster-%u", pcs->maxpages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 		pcs->slab = kmem_cache_create(pcs->name, size, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 					      SLAB_RECLAIM_ACCOUNT, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 		if (pcs->slab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 		z_erofs_destroy_pcluster_pool();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static struct z_erofs_pcluster *z_erofs_alloc_pcluster(unsigned int nrpages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 		struct z_erofs_pcluster *pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		if (nrpages > pcs->maxpages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		pcl = kmem_cache_zalloc(pcs->slab, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		if (!pcl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 			return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		pcl->pclusterpages = nrpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 		return pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) static void z_erofs_free_pcluster(struct z_erofs_pcluster *pcl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	for (i = 0; i < ARRAY_SIZE(pcluster_pool); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		struct z_erofs_pcluster_slab *pcs = pcluster_pool + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 		if (pcl->pclusterpages > pcs->maxpages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		kmem_cache_free(pcs->slab, pcl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * a compressed_pages[] placeholder in order to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * being filled with file pages for in-place decompression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) #define PAGE_UNALLOCATED     ((void *)0x5F0E4B1D)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) /* how to allocate cached pages for a pcluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) enum z_erofs_cache_alloctype {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	DONTALLOC,	/* don't allocate any cached pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	DELAYEDALLOC,	/* delayed allocation (at the time of submitting io) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	 * try to use cached I/O if page allocation succeeds or fallback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	 * to in-place I/O instead to avoid any direct reclaim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	TRYALLOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  * tagged pointer with 1-bit tag for all compressed pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  * tag 0 - the page is just found with an extra page reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) typedef tagptr1_t compressed_page_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) #define tag_compressed_page_justfound(page) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	tagptr_fold(compressed_page_t, page, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) static struct workqueue_struct *z_erofs_workqueue __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) void z_erofs_exit_zip_subsystem(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	destroy_workqueue(z_erofs_workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	z_erofs_destroy_pcluster_pool();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) static inline int z_erofs_init_workqueue(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	const unsigned int onlinecpus = num_possible_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	 * no need to spawn too many threads, limiting threads could minimum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	 * scheduling overhead, perhaps per-CPU threads should be better?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	z_erofs_workqueue = alloc_workqueue("erofs_unzipd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 					    WQ_UNBOUND | WQ_HIGHPRI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 					    onlinecpus + onlinecpus / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	return z_erofs_workqueue ? 0 : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) int __init z_erofs_init_zip_subsystem(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	int err = z_erofs_create_pcluster_pool();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	err = z_erofs_init_workqueue();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		z_erofs_destroy_pcluster_pool();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) enum z_erofs_collectmode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	COLLECT_SECONDARY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	COLLECT_PRIMARY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	 * The current collection was the tail of an exist chain, in addition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	 * that the previous processed chained collections are all decided to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	 * be hooked up to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	 * A new chain will be created for the remaining collections which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	 * not processed yet, therefore different from COLLECT_PRIMARY_FOLLOWED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	 * the next collection cannot reuse the whole page safely in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	 * the following scenario:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	 *  ________________________________________________________________
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	 * |      tail (partial) page     |       head (partial) page       |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	 * |   (belongs to the next cl)   |   (belongs to the current cl)   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	 * |_______PRIMARY_FOLLOWED_______|________PRIMARY_HOOKED___________|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	COLLECT_PRIMARY_HOOKED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	 * a weak form of COLLECT_PRIMARY_FOLLOWED, the difference is that it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	 * could be dispatched into bypass queue later due to uptodated managed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	 * pages. All related online pages cannot be reused for inplace I/O (or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	 * pagevec) since it can be directly decoded without I/O submission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	COLLECT_PRIMARY_FOLLOWED_NOINPLACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	 * The current collection has been linked with the owned chain, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	 * could also be linked with the remaining collections, which means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	 * if the processing page is the tail page of the collection, thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	 * the current collection can safely use the whole page (since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	 * the previous collection is under control) for in-place I/O, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	 * illustrated below:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	 *  ________________________________________________________________
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	 * |  tail (partial) page |          head (partial) page           |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	 * |  (of the current cl) |      (of the previous collection)      |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	 * |  PRIMARY_FOLLOWED or |                                        |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	 * |_____PRIMARY_HOOKED___|____________PRIMARY_FOLLOWED____________|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	 * [  (*) the above page can be used as inplace I/O.               ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	COLLECT_PRIMARY_FOLLOWED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) struct z_erofs_collector {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	struct z_erofs_pagevec_ctor vector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	struct z_erofs_pcluster *pcl, *tailpcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	struct z_erofs_collection *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	/* a pointer used to pick up inplace I/O pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	struct page **icpage_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	z_erofs_next_pcluster_t owned_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	enum z_erofs_collectmode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) struct z_erofs_decompress_frontend {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	struct inode *const inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	struct z_erofs_collector clt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	struct erofs_map_blocks map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	bool readahead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	/* used for applying cache strategy on the fly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	bool backmost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	erofs_off_t headoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) #define COLLECTOR_INIT() { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	.owned_head = Z_EROFS_PCLUSTER_TAIL, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	.mode = COLLECT_PRIMARY_FOLLOWED }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) #define DECOMPRESS_FRONTEND_INIT(__i) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	.inode = __i, .clt = COLLECTOR_INIT(), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	.backmost = true, }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) static struct page *z_pagemap_global[Z_EROFS_VMAP_GLOBAL_PAGES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) static DEFINE_MUTEX(z_pagemap_global_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) static void preload_compressed_pages(struct z_erofs_collector *clt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 				     struct address_space *mc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 				     enum z_erofs_cache_alloctype type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 				     struct list_head *pagepool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	struct z_erofs_pcluster *pcl = clt->pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	bool standalone = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	gfp_t gfp = (mapping_gfp_mask(mc) & ~__GFP_DIRECT_RECLAIM) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	struct page **pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	pgoff_t index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	pages = pcl->compressed_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	index = pcl->obj.index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	for (; index < pcl->obj.index + pcl->pclusterpages; ++index, ++pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		compressed_page_t t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		struct page *newpage = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		/* the compressed page was loaded before */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		if (READ_ONCE(*pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		page = find_get_page(mc, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 			t = tag_compressed_page_justfound(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 			/* I/O is needed, no possible to decompress directly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			standalone = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 			switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 			case DELAYEDALLOC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 				t = tagptr_init(compressed_page_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 						PAGE_UNALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 			case TRYALLOC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 				newpage = erofs_allocpage(pagepool, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 				if (!newpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 				set_page_private(newpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 						 Z_EROFS_PREALLOCATED_PAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 				t = tag_compressed_page_justfound(newpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 			default:        /* DONTALLOC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		if (!cmpxchg_relaxed(pages, NULL, tagptr_cast_ptr(t)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		} else if (newpage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 			set_page_private(newpage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 			list_add(&newpage->lru, pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	 * don't do inplace I/O if all compressed pages are available in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	 * managed cache since it can be moved to the bypass queue instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	if (standalone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		clt->mode = COLLECT_PRIMARY_FOLLOWED_NOINPLACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) /* called by erofs_shrinker to get rid of all compressed_pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 				       struct erofs_workgroup *grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	struct z_erofs_pcluster *const pcl =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		container_of(grp, struct z_erofs_pcluster, obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	struct address_space *const mapping = MNGD_MAPPING(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	 * refcount of workgroup is now freezed as 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	 * therefore no need to worry about available decompression users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	for (i = 0; i < pcl->pclusterpages; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		struct page *page = pcl->compressed_pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		/* block other users from reclaiming or migrating the page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		if (!trylock_page(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		if (page->mapping != mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		/* barrier is implied in the following 'unlock_page' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		WRITE_ONCE(pcl->compressed_pages[i], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		detach_page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) int erofs_try_to_free_cached_page(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 				  struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	struct z_erofs_pcluster *const pcl = (void *)page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	int ret = 0;	/* 0 - busy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	if (erofs_workgroup_try_to_freeze(&pcl->obj, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		for (i = 0; i < pcl->pclusterpages; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 			if (pcl->compressed_pages[i] == page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 				WRITE_ONCE(pcl->compressed_pages[i], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 				ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		erofs_workgroup_unfreeze(&pcl->obj, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 			detach_page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) /* page_type must be Z_EROFS_PAGE_TYPE_EXCLUSIVE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) static bool z_erofs_try_inplace_io(struct z_erofs_collector *clt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 				   struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	struct z_erofs_pcluster *const pcl = clt->pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	while (clt->icpage_ptr > pcl->compressed_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		if (!cmpxchg(--clt->icpage_ptr, NULL, page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) /* callers must be with collection lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) static int z_erofs_attach_page(struct z_erofs_collector *clt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 			       struct page *page, enum z_erofs_page_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 			       bool pvec_safereuse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	/* give priority for inplaceio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	if (clt->mode >= COLLECT_PRIMARY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	    type == Z_EROFS_PAGE_TYPE_EXCLUSIVE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	    z_erofs_try_inplace_io(clt, page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	ret = z_erofs_pagevec_enqueue(&clt->vector, page, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 				      pvec_safereuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	clt->cl->vcnt += (unsigned int)ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	return ret ? 0 : -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) static enum z_erofs_collectmode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) try_to_claim_pcluster(struct z_erofs_pcluster *pcl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		      z_erofs_next_pcluster_t *owned_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	/* let's claim these following types of pclusters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	if (pcl->next == Z_EROFS_PCLUSTER_NIL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		/* type 1, nil pcluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_NIL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 			    *owned_head) != Z_EROFS_PCLUSTER_NIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		*owned_head = &pcl->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		/* lucky, I am the followee :) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		return COLLECT_PRIMARY_FOLLOWED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	} else if (pcl->next == Z_EROFS_PCLUSTER_TAIL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		 * type 2, link to the end of a existing open chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		 * be careful that its submission itself is governed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		 * by the original owned chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		if (cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 			    *owned_head) != Z_EROFS_PCLUSTER_TAIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		*owned_head = Z_EROFS_PCLUSTER_TAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		return COLLECT_PRIMARY_HOOKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	return COLLECT_PRIMARY;	/* :( better luck next time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) static int z_erofs_lookup_collection(struct z_erofs_collector *clt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 				     struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 				     struct erofs_map_blocks *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	struct z_erofs_pcluster *pcl = clt->pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	struct z_erofs_collection *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	unsigned int length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	/* to avoid unexpected loop formed by corrupted images */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	if (clt->owned_head == &pcl->next || pcl == clt->tailpcl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	cl = z_erofs_primarycollection(pcl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (cl->pageofs != (map->m_la & ~PAGE_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	length = READ_ONCE(pcl->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	if (length & Z_EROFS_PCLUSTER_FULL_LENGTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		if ((map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) > length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 			DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		unsigned int llen = map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		if (map->m_flags & EROFS_MAP_FULL_MAPPED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			llen |= Z_EROFS_PCLUSTER_FULL_LENGTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		while (llen > length &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		       length != cmpxchg_relaxed(&pcl->length, length, llen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 			length = READ_ONCE(pcl->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	mutex_lock(&cl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	/* used to check tail merging loop due to corrupted images */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		clt->tailpcl = pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	clt->mode = try_to_claim_pcluster(pcl, &clt->owned_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	/* clean tailpcl if the current owned_head is Z_EROFS_PCLUSTER_TAIL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		clt->tailpcl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	clt->cl = cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) static int z_erofs_register_collection(struct z_erofs_collector *clt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 				       struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 				       struct erofs_map_blocks *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	struct z_erofs_pcluster *pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	struct z_erofs_collection *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	struct erofs_workgroup *grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	/* no available pcluster, let's allocate one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	pcl = z_erofs_alloc_pcluster(map->m_plen >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	if (IS_ERR(pcl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		return PTR_ERR(pcl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	atomic_set(&pcl->obj.refcount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	pcl->obj.index = map->m_pa >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	pcl->length = (map->m_llen << Z_EROFS_PCLUSTER_LENGTH_BIT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		(map->m_flags & EROFS_MAP_FULL_MAPPED ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			Z_EROFS_PCLUSTER_FULL_LENGTH : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	if (map->m_flags & EROFS_MAP_ZIPPED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		pcl->algorithmformat = Z_EROFS_COMPRESSION_LZ4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		pcl->algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	/* new pclusters should be claimed as type 1, primary and followed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	pcl->next = clt->owned_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	clt->mode = COLLECT_PRIMARY_FOLLOWED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	cl = z_erofs_primarycollection(pcl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	cl->pageofs = map->m_la & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	 * lock all primary followed works before visible to others
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	 * and mutex_trylock *never* fails for a new pcluster.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	mutex_init(&cl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	DBG_BUGON(!mutex_trylock(&cl->lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	grp = erofs_insert_workgroup(inode->i_sb, &pcl->obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	if (IS_ERR(grp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		err = PTR_ERR(grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	if (grp != &pcl->obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	/* used to check tail merging loop due to corrupted images */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	if (clt->owned_head == Z_EROFS_PCLUSTER_TAIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		clt->tailpcl = pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	clt->owned_head = &pcl->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	clt->pcl = pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	clt->cl = cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	mutex_unlock(&cl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	z_erofs_free_pcluster(pcl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) static int z_erofs_collector_begin(struct z_erofs_collector *clt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 				   struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 				   struct erofs_map_blocks *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	struct erofs_workgroup *grp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	DBG_BUGON(clt->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	/* must be Z_EROFS_PCLUSTER_TAIL or pointed to previous collection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_NIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	DBG_BUGON(clt->owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	if (!PAGE_ALIGNED(map->m_pa)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	grp = erofs_find_workgroup(inode->i_sb, map->m_pa >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	if (grp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		clt->pcl = container_of(grp, struct z_erofs_pcluster, obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		ret = z_erofs_register_collection(clt, inode, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		if (ret != -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	ret = z_erofs_lookup_collection(clt, inode, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		erofs_workgroup_put(&clt->pcl->obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	z_erofs_pagevec_ctor_init(&clt->vector, Z_EROFS_NR_INLINE_PAGEVECS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 				  clt->cl->pagevec, clt->cl->vcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	/* since file-backed online pages are traversed in reverse order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	clt->icpage_ptr = clt->pcl->compressed_pages + clt->pcl->pclusterpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587)  * keep in mind that no referenced pclusters will be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588)  * only after a RCU grace period.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) static void z_erofs_rcu_callback(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	struct z_erofs_collection *const cl =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		container_of(head, struct z_erofs_collection, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	z_erofs_free_pcluster(container_of(cl, struct z_erofs_pcluster,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 					   primary_collection));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) void erofs_workgroup_free_rcu(struct erofs_workgroup *grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	struct z_erofs_pcluster *const pcl =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		container_of(grp, struct z_erofs_pcluster, obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	struct z_erofs_collection *const cl = z_erofs_primarycollection(pcl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	call_rcu(&cl->rcu, z_erofs_rcu_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) static void z_erofs_collection_put(struct z_erofs_collection *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	struct z_erofs_pcluster *const pcl =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		container_of(cl, struct z_erofs_pcluster, primary_collection);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	erofs_workgroup_put(&pcl->obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) static bool z_erofs_collector_end(struct z_erofs_collector *clt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	struct z_erofs_collection *cl = clt->cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	if (!cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	z_erofs_pagevec_ctor_exit(&clt->vector, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	mutex_unlock(&cl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	 * if all pending pages are added, don't hold its reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	 * any longer if the pcluster isn't hosted by ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	if (clt->mode < COLLECT_PRIMARY_FOLLOWED_NOINPLACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		z_erofs_collection_put(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	clt->cl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) static bool should_alloc_managed_pages(struct z_erofs_decompress_frontend *fe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 				       unsigned int cachestrategy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 				       erofs_off_t la)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (cachestrategy <= EROFS_ZIP_CACHE_DISABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	if (fe->backmost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	return cachestrategy >= EROFS_ZIP_CACHE_READAROUND &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		la < fe->headoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 				struct page *page, struct list_head *pagepool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	struct inode *const inode = fe->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	struct erofs_map_blocks *const map = &fe->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	struct z_erofs_collector *const clt = &fe->clt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	const loff_t offset = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	bool tight = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	enum z_erofs_cache_alloctype cache_strategy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	enum z_erofs_page_type page_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	unsigned int cur, end, spiltted, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	/* register locked file pages as online pages in pack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	z_erofs_onlinepage_init(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	spiltted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	end = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	cur = end - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	/* lucky, within the range of the current map_blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	if (offset + cur >= map->m_la &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	    offset + cur < map->m_la + map->m_llen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		/* didn't get a valid collection previously (very rare) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		if (!clt->cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 			goto restart_now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		goto hitted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	/* go ahead the next map_blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	erofs_dbg("%s: [out-of-range] pos %llu", __func__, offset + cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	if (z_erofs_collector_end(clt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		fe->backmost = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	map->m_la = offset + cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	map->m_llen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	err = z_erofs_map_blocks_iter(inode, map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) restart_now:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	if (!(map->m_flags & EROFS_MAP_MAPPED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		goto hitted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	err = z_erofs_collector_begin(clt, inode, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	/* preload all compressed pages (maybe downgrade role if necessary) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	if (should_alloc_managed_pages(fe, sbi->ctx.cache_strategy, map->m_la))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		cache_strategy = TRYALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		cache_strategy = DONTALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	preload_compressed_pages(clt, MNGD_MAPPING(sbi),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 				 cache_strategy, pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) hitted:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	 * Ensure the current partial page belongs to this submit chain rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	 * than other concurrent submit chains or the noio(bypass) chain since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	 * those chains are handled asynchronously thus the page cannot be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	 * for inplace I/O or pagevec (should be processed in strict order.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	tight &= (clt->mode >= COLLECT_PRIMARY_HOOKED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		  clt->mode != COLLECT_PRIMARY_FOLLOWED_NOINPLACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	cur = end - min_t(unsigned int, offset + end - map->m_la, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	if (!(map->m_flags & EROFS_MAP_MAPPED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		zero_user_segment(page, cur, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		goto next_part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	/* let's derive page type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	page_type = cur ? Z_EROFS_VLE_PAGE_TYPE_HEAD :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		(!spiltted ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 			(tight ? Z_EROFS_PAGE_TYPE_EXCLUSIVE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 				Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	if (cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		tight &= (clt->mode >= COLLECT_PRIMARY_FOLLOWED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	err = z_erofs_attach_page(clt, page, page_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 				  clt->mode >= COLLECT_PRIMARY_FOLLOWED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	/* should allocate an additional staging page for pagevec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (err == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		struct page *const newpage =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		err = z_erofs_attach_page(clt, newpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 					  Z_EROFS_PAGE_TYPE_EXCLUSIVE, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	index = page->index - (map->m_la >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	z_erofs_onlinepage_fixup(page, index, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	/* bump up the number of spiltted parts of a page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	++spiltted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	/* also update nr_pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	clt->cl->nr_pages = max_t(pgoff_t, clt->cl->nr_pages, index + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) next_part:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	/* can be used for verification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	map->m_llen = offset + cur - map->m_la;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	end = cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (end > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	z_erofs_onlinepage_endio(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	erofs_dbg("%s, finish page: %pK spiltted: %u map->m_llen %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		  __func__, page, spiltted, map->m_llen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	/* if some error occurred while processing this page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) static void z_erofs_decompressqueue_work(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) static void z_erofs_decompress_kickoff(struct z_erofs_decompressqueue *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 				       bool sync, int bios)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	struct erofs_sb_info *const sbi = EROFS_SB(io->sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	/* wake up the caller thread for sync decompression */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	if (sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		spin_lock_irqsave(&io->u.wait.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		if (!atomic_add_return(bios, &io->pending_bios))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 			wake_up_locked(&io->u.wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		spin_unlock_irqrestore(&io->u.wait.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	if (atomic_add_return(bios, &io->pending_bios))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	/* Use workqueue and sync decompression for atomic contexts only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if (in_atomic() || irqs_disabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		queue_work(z_erofs_workqueue, &io->u.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		sbi->ctx.readahead_sync_decompress = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	z_erofs_decompressqueue_work(&io->u.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) static bool z_erofs_page_is_invalidated(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	return !page->mapping && !z_erofs_is_shortlived_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) static void z_erofs_decompressqueue_endio(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	tagptr1_t t = tagptr_init(tagptr1_t, bio->bi_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	struct z_erofs_decompressqueue *q = tagptr_unfold_ptr(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	blk_status_t err = bio->bi_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	struct bio_vec *bvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	struct bvec_iter_all iter_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	bio_for_each_segment_all(bvec, bio, iter_all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		struct page *page = bvec->bv_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		DBG_BUGON(PageUptodate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		DBG_BUGON(z_erofs_page_is_invalidated(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		if (erofs_page_is_managed(EROFS_SB(q->sb), page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 				SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	z_erofs_decompress_kickoff(q, tagptr_unfold_tags(t), -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) static int z_erofs_decompress_pcluster(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 				       struct z_erofs_pcluster *pcl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 				       struct list_head *pagepool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	struct z_erofs_pagevec_ctor ctor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	unsigned int i, inputsize, outputsize, llen, nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	struct page *pages_onstack[Z_EROFS_VMAP_ONSTACK_PAGES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	struct page **pages, **compressed_pages, *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	enum z_erofs_page_type page_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	bool overlapped, partial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	struct z_erofs_collection *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	cl = z_erofs_primarycollection(pcl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	DBG_BUGON(!READ_ONCE(cl->nr_pages));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	mutex_lock(&cl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	nr_pages = cl->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	if (nr_pages <= Z_EROFS_VMAP_ONSTACK_PAGES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		pages = pages_onstack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	} else if (nr_pages <= Z_EROFS_VMAP_GLOBAL_PAGES &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		   mutex_trylock(&z_pagemap_global_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		pages = z_pagemap_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		gfp_t gfp_flags = GFP_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		if (nr_pages > Z_EROFS_VMAP_GLOBAL_PAGES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			gfp_flags |= __GFP_NOFAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		pages = kvmalloc_array(nr_pages, sizeof(struct page *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 				       gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		/* fallback to global pagemap for the lowmem scenario */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		if (!pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			mutex_lock(&z_pagemap_global_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			pages = z_pagemap_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	for (i = 0; i < nr_pages; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		pages[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	z_erofs_pagevec_ctor_init(&ctor, Z_EROFS_NR_INLINE_PAGEVECS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 				  cl->pagevec, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	for (i = 0; i < cl->vcnt; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		unsigned int pagenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		page = z_erofs_pagevec_dequeue(&ctor, &page_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		/* all pages in pagevec ought to be valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		DBG_BUGON(!page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		DBG_BUGON(z_erofs_page_is_invalidated(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		if (z_erofs_put_shortlivedpage(pagepool, page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		if (page_type == Z_EROFS_VLE_PAGE_TYPE_HEAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 			pagenr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			pagenr = z_erofs_onlinepage_index(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		DBG_BUGON(pagenr >= nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		 * currently EROFS doesn't support multiref(dedup),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		 * so here erroring out one multiref page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (pages[pagenr]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			SetPageError(pages[pagenr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			z_erofs_onlinepage_endio(pages[pagenr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 			err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		pages[pagenr] = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	z_erofs_pagevec_ctor_exit(&ctor, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	overlapped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	compressed_pages = pcl->compressed_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	for (i = 0; i < pcl->pclusterpages; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		unsigned int pagenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		page = compressed_pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		/* all compressed pages ought to be valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		DBG_BUGON(!page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		DBG_BUGON(z_erofs_page_is_invalidated(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		if (!z_erofs_is_shortlived_page(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 			if (erofs_page_is_managed(sbi, page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 				if (!PageUptodate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 					err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 			 * only if non-head page can be selected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 			 * for inplace decompression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			pagenr = z_erofs_onlinepage_index(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			DBG_BUGON(pagenr >= nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			if (pages[pagenr]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 				DBG_BUGON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 				SetPageError(pages[pagenr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 				z_erofs_onlinepage_endio(pages[pagenr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 				err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			pages[pagenr] = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			overlapped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		/* PG_error needs checking for all non-managed pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		if (PageError(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			DBG_BUGON(PageUptodate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	llen = pcl->length >> Z_EROFS_PCLUSTER_LENGTH_BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (nr_pages << PAGE_SHIFT >= cl->pageofs + llen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		outputsize = llen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		partial = !(pcl->length & Z_EROFS_PCLUSTER_FULL_LENGTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		outputsize = (nr_pages << PAGE_SHIFT) - cl->pageofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		partial = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	inputsize = pcl->pclusterpages * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	err = z_erofs_decompress(&(struct z_erofs_decompress_req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 					.sb = sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 					.in = compressed_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 					.out = pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 					.pageofs_out = cl->pageofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 					.inputsize = inputsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 					.outputsize = outputsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 					.alg = pcl->algorithmformat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 					.inplace_io = overlapped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 					.partial_decoding = partial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 				 }, pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	/* must handle all compressed pages before ending pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	for (i = 0; i < pcl->pclusterpages; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		page = compressed_pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		if (erofs_page_is_managed(sbi, page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		/* recycle all individual short-lived pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		(void)z_erofs_put_shortlivedpage(pagepool, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		WRITE_ONCE(compressed_pages[i], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	for (i = 0; i < nr_pages; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		page = pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		DBG_BUGON(z_erofs_page_is_invalidated(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		/* recycle all individual short-lived pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		if (z_erofs_put_shortlivedpage(pagepool, page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		z_erofs_onlinepage_endio(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	if (pages == z_pagemap_global)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		mutex_unlock(&z_pagemap_global_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	else if (pages != pages_onstack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		kvfree(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	cl->nr_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	cl->vcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	/* all cl locks MUST be taken before the following line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_NIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	/* all cl locks SHOULD be released right now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	mutex_unlock(&cl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	z_erofs_collection_put(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) static void z_erofs_decompress_queue(const struct z_erofs_decompressqueue *io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 				     struct list_head *pagepool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	z_erofs_next_pcluster_t owned = io->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	while (owned != Z_EROFS_PCLUSTER_TAIL_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		struct z_erofs_pcluster *pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		/* no possible that 'owned' equals Z_EROFS_WORK_TPTR_TAIL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_TAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		/* no possible that 'owned' equals NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		DBG_BUGON(owned == Z_EROFS_PCLUSTER_NIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		pcl = container_of(owned, struct z_erofs_pcluster, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		owned = READ_ONCE(pcl->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		z_erofs_decompress_pcluster(io->sb, pcl, pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) static void z_erofs_decompressqueue_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	struct z_erofs_decompressqueue *bgq =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		container_of(work, struct z_erofs_decompressqueue, u.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	LIST_HEAD(pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	DBG_BUGON(bgq->head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	z_erofs_decompress_queue(bgq, &pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	put_pages_list(&pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	kvfree(bgq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) static struct page *pickup_page_for_submission(struct z_erofs_pcluster *pcl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 					       unsigned int nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 					       struct list_head *pagepool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 					       struct address_space *mc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 					       gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	const pgoff_t index = pcl->obj.index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	bool tocache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	struct address_space *mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	struct page *oldpage, *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	compressed_page_t t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	int justfound;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	page = READ_ONCE(pcl->compressed_pages[nr]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	oldpage = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		goto out_allocpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	 * the cached page has not been allocated and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	 * an placeholder is out there, prepare it now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	if (page == PAGE_UNALLOCATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		tocache = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		goto out_allocpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	/* process the target tagged pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	t = tagptr_init(compressed_page_t, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	justfound = tagptr_unfold_tags(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	page = tagptr_unfold_ptr(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	 * preallocated cached pages, which is used to avoid direct reclaim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	 * otherwise, it will go inplace I/O path instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	if (page->private == Z_EROFS_PREALLOCATED_PAGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		WRITE_ONCE(pcl->compressed_pages[nr], page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		set_page_private(page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		tocache = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		goto out_tocache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	mapping = READ_ONCE(page->mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	 * file-backed online pages in plcuster are all locked steady,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	 * therefore it is impossible for `mapping' to be NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	if (mapping && mapping != mc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		/* ought to be unmanaged pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	/* directly return for shortlived page as well */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	if (z_erofs_is_shortlived_page(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	/* only true if page reclaim goes wrong, should never happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	DBG_BUGON(justfound && PagePrivate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	/* the page is still in manage cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	if (page->mapping == mc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		WRITE_ONCE(pcl->compressed_pages[nr], page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		ClearPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		if (!PagePrivate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 			 * impossible to be !PagePrivate(page) for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 			 * the current restriction as well if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			 * the page is already in compressed_pages[].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 			DBG_BUGON(!justfound);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 			justfound = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 			set_page_private(page, (unsigned long)pcl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 			SetPagePrivate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		/* no need to submit io if it is already up-to-date */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		if (PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 			page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	 * the managed page has been truncated, it's unsafe to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	 * reuse this one, let's allocate a new cache-managed page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	DBG_BUGON(page->mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	DBG_BUGON(!justfound);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	tocache = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) out_allocpage:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	page = erofs_allocpage(pagepool, gfp | __GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	if (oldpage != cmpxchg(&pcl->compressed_pages[nr], oldpage, page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		list_add(&page->lru, pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) out_tocache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	if (!tocache || add_to_page_cache_lru(page, mc, index + nr, gfp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		/* turn into temporary page if fails (1 ref) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		set_page_private(page, Z_EROFS_SHORTLIVED_PAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	attach_page_private(page, pcl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	/* drop a refcount added by allocpage (then we have 2 refs here) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) out:	/* the only exit (for tracing and debugging) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) static struct z_erofs_decompressqueue *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) jobqueue_init(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	      struct z_erofs_decompressqueue *fgq, bool *fg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	struct z_erofs_decompressqueue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	if (fg && !*fg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		q = kvzalloc(sizeof(*q), GFP_KERNEL | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		if (!q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			*fg = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			goto fg_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		INIT_WORK(&q->u.work, z_erofs_decompressqueue_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) fg_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		q = fgq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		init_waitqueue_head(&fgq->u.wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		atomic_set(&fgq->pending_bios, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	q->sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	q->head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	return q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) /* define decompression jobqueue types */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	JQ_BYPASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	JQ_SUBMIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	NR_JOBQUEUES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) static void *jobqueueset_init(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			      struct z_erofs_decompressqueue *q[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			      struct z_erofs_decompressqueue *fgq, bool *fg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	 * if managed cache is enabled, bypass jobqueue is needed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	 * no need to read from device for all pclusters in this queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	q[JQ_BYPASS] = jobqueue_init(sb, fgq + JQ_BYPASS, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	q[JQ_SUBMIT] = jobqueue_init(sb, fgq + JQ_SUBMIT, fg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	return tagptr_cast_ptr(tagptr_fold(tagptr1_t, q[JQ_SUBMIT], *fg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) static void move_to_bypass_jobqueue(struct z_erofs_pcluster *pcl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 				    z_erofs_next_pcluster_t qtail[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 				    z_erofs_next_pcluster_t owned_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	z_erofs_next_pcluster_t *const submit_qtail = qtail[JQ_SUBMIT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	z_erofs_next_pcluster_t *const bypass_qtail = qtail[JQ_BYPASS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	if (owned_head == Z_EROFS_PCLUSTER_TAIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		owned_head = Z_EROFS_PCLUSTER_TAIL_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	WRITE_ONCE(pcl->next, Z_EROFS_PCLUSTER_TAIL_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	WRITE_ONCE(*submit_qtail, owned_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	WRITE_ONCE(*bypass_qtail, &pcl->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	qtail[JQ_BYPASS] = &pcl->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) static void z_erofs_submit_queue(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 				 struct z_erofs_decompress_frontend *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 				 struct list_head *pagepool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 				 struct z_erofs_decompressqueue *fgq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 				 bool *force_fg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	z_erofs_next_pcluster_t qtail[NR_JOBQUEUES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	struct z_erofs_decompressqueue *q[NR_JOBQUEUES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	void *bi_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	z_erofs_next_pcluster_t owned_head = f->clt.owned_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	/* since bio will be NULL, no need to initialize last_index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	pgoff_t last_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	unsigned int nr_bios = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	struct bio *bio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	bi_private = jobqueueset_init(sb, q, fgq, force_fg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	qtail[JQ_BYPASS] = &q[JQ_BYPASS]->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	qtail[JQ_SUBMIT] = &q[JQ_SUBMIT]->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	/* by default, all need io submission */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	q[JQ_SUBMIT]->head = owned_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		struct z_erofs_pcluster *pcl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		pgoff_t cur, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		unsigned int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		bool bypass = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		/* no possible 'owned_head' equals the following */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_TAIL_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		DBG_BUGON(owned_head == Z_EROFS_PCLUSTER_NIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		pcl = container_of(owned_head, struct z_erofs_pcluster, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		cur = pcl->obj.index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		end = cur + pcl->pclusterpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		/* close the main owned chain at first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		owned_head = cmpxchg(&pcl->next, Z_EROFS_PCLUSTER_TAIL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 				     Z_EROFS_PCLUSTER_TAIL_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 			struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 			page = pickup_page_for_submission(pcl, i++, pagepool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 							  MNGD_MAPPING(sbi),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 							  GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 			if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 			if (bio && cur != last_index + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) submit_bio_retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 				submit_bio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 				bio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 			if (!bio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 				bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 				bio->bi_end_io = z_erofs_decompressqueue_endio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 				bio_set_dev(bio, sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 				bio->bi_iter.bi_sector = (sector_t)cur <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 					LOG_SECTORS_PER_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 				bio->bi_private = bi_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 				bio->bi_opf = REQ_OP_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 				if (f->readahead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 					bio->bi_opf |= REQ_RAHEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 				++nr_bios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 				goto submit_bio_retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 			last_index = cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 			bypass = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		} while (++cur < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		if (!bypass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 			qtail[JQ_SUBMIT] = &pcl->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			move_to_bypass_jobqueue(pcl, qtail, owned_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	} while (owned_head != Z_EROFS_PCLUSTER_TAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	if (bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		submit_bio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	 * although background is preferred, no one is pending for submission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	 * don't issue workqueue for decompression but drop it directly instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	if (!*force_fg && !nr_bios) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		kvfree(q[JQ_SUBMIT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	z_erofs_decompress_kickoff(q[JQ_SUBMIT], *force_fg, nr_bios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) static void z_erofs_runqueue(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			     struct z_erofs_decompress_frontend *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 			     struct list_head *pagepool, bool force_fg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	struct z_erofs_decompressqueue io[NR_JOBQUEUES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	if (f->clt.owned_head == Z_EROFS_PCLUSTER_TAIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	z_erofs_submit_queue(sb, f, pagepool, io, &force_fg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	/* handle bypass queue (no i/o pclusters) immediately */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	z_erofs_decompress_queue(&io[JQ_BYPASS], pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	if (!force_fg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	/* wait until all bios are completed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	io_wait_event(io[JQ_SUBMIT].u.wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		      !atomic_read(&io[JQ_SUBMIT].pending_bios));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	/* handle synchronous decompress queue in the caller context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	z_erofs_decompress_queue(&io[JQ_SUBMIT], pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) static int z_erofs_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	struct inode *const inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	LIST_HEAD(pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	trace_erofs_readpage(page, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	f.headoffset = (erofs_off_t)page->index << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	err = z_erofs_do_read_page(&f, page, &pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	(void)z_erofs_collector_end(&f.clt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	/* if some compressed cluster ready, need submit them anyway */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		erofs_err(inode->i_sb, "failed to read, err [%d]", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	if (f.map.mpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		put_page(f.map.mpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	/* clean up the remaining free pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	put_pages_list(&pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) static void z_erofs_readahead(struct readahead_control *rac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	struct inode *const inode = rac->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	unsigned int nr_pages = readahead_count(rac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	bool sync = (sbi->ctx.readahead_sync_decompress &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			nr_pages <= sbi->ctx.max_sync_decompress_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	struct z_erofs_decompress_frontend f = DECOMPRESS_FRONTEND_INIT(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	struct page *page, *head = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	LIST_HEAD(pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	trace_erofs_readpages(inode, readahead_index(rac), nr_pages, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	f.readahead = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	f.headoffset = readahead_pos(rac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	while ((page = readahead_page(rac))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		prefetchw(&page->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		 * A pure asynchronous readahead is indicated if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		 * a PG_readahead marked page is hitted at first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		 * Let's also do asynchronous decompression for this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		sync &= !(PageReadahead(page) && !head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		set_page_private(page, (unsigned long)head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		head = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	while (head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		struct page *page = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		/* traversal in reverse order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 		head = (void *)page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		err = z_erofs_do_read_page(&f, page, &pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 			erofs_err(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 				  "readahead error at page %lu @ nid %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 				  page->index, EROFS_I(inode)->nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	(void)z_erofs_collector_end(&f.clt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	z_erofs_runqueue(inode->i_sb, &f, &pagepool, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	if (f.map.mpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		put_page(f.map.mpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	/* clean up the remaining free pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	put_pages_list(&pagepool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) const struct address_space_operations z_erofs_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	.readpage = z_erofs_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	.readahead = z_erofs_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)